This project is read-only.
Steps to use the NUnit Test Runner for Windows Phone 7
1. Create a new standard Windows Phone 7 Application inside visual studio 2010
2. Delete all the generated files inside the project and keep only the App.xaml and App.xaml.cs files.
3. Add a reference to the NUnit Test Runner DLL to this project.
4. Navigate to the properties node, then to the WMAppManifest.xml file, double click it to edit its contents.
5. Once opened, scroll down to the <Tasks> node, then to the <DefaultTask Name ="_default" NavigationPage="MainPage.xaml"/> tag. This tag instructs the Silverlight Runtime in the Windows Phone Device (or Emulator) to loads the MainPage.xaml file after the application is started, by default, the App.xaml.cs file contains the logic that loads the application splash screen among with an instance of the PhoneApplicationFrame, this class is the root container that allows navigation inside the app, there is no code there that instructs the frame to load the MainPage.xaml page, instead, it’s declared in the Manifest file in the NavigationPage of the Default Task Tag. As our runner set’s the default page to be loaded from inside the NUnitTestRunner.Setup() method, we should remove the default value set in this attribute, this is done by removing the NavigationPage attribute. Please note that it’s forbidden to remove completely the DefaultTask Tag, then the resulting tag will look like this :
<DefaultTask Name ="_default"/>
6. After that, we will instruct the app class to load the main testing page inside the NUnitTestRunnerAssembly. To do so, first we will remove all the generated code and keep only the constructor. Then we should add an event handler of the Startup event using this sample code.
7. // Constructor
public App()
{
// Standard Silverlight initialization
InitializeComponent();

this.Startup += App_Startup;
}

void App_Startup(object sender, StartupEventArgs e)
{
}

8. In the App_Startup event handler, we make a call to the NUnitTestRunner.Setup() Method, this method has two overloads:
a. The parameter less version has the following code :
public static void Setup()
{
SetupRootVisual();
TestsAssemblies.Add(Assembly.GetCallingAssembly());
}
- In this version, the Setup method calls the SetupRootVisual Method, then adds the calling assembly to the local variable TestAssemblies, this list<Assembly> instance will be used to inform the runner framework about the test assemblies when the Run Button is clicked in the main page.
- The SetupRootVisual has the following code:
private static void SetupRootVisual()
{
var frame = new PhoneApplicationFrame();
Application.Current.RootVisual = frame;
frame.Source = new Uri("/NUnitTestRunnerWP7;component/MainPage.xaml", UriKind.Relative);
}
As you can see, the method creates a frame instance, assign it to the RootVisual of the current application instance, and set the default starting page to the MainPage of the runner. Note that the RootVisual property is set only once and can’t be assigned many times, so please don’t assign this property outside and remove any code that assigns it in the calling assembly.
b. The second version of the method receives a list of assemblies supposed to contain the test fixtures. It does the same thing; setup the root visual then adds the received list parameter to the local TestsAssemblies using the List.AddRange Method.
9. Back to the App_Startup event listener, there is two possibilities to setup the test runner :
a. Using the following prototype :
void App_Startup(object sender, StartupEventArgs e)
{
NUnitTestRunnerWP7.NUnitTestRunner.Setup();
}
This code calls the default parameter less Setup Method, please use this prototype when the test fixtures are inside the created project itself.
b. Or using these two equivalent prototypes :
void App_Startup(object sender, StartupEventArgs e)
{
var assembliesList = new List<Assembly>();
Assembly assembly = Assembly.Load("AssemblyNameWithoutExtension");
assembliesList.Add(assembly);
NUnitTestRunnerWP7.NUnitTestRunner.Setup(assembliesList);
}
- In this version, we call the Setup method and pass a list of assemblies supposed to contain the test fixtures, to do so, we should load these assemblies in the current app domain by using the Assembly.Load static method
- This method receives the logical name of the assembly (without the .dll extension).
- To be loaded, this assembly should be included in the .xap file resulting from the building process, thus a reference to the loaded assembly should be made to instruct the MSBuild engine to incorporate the assembly in the .xap file.
- Another way to load the assemblies that contains the test fixtures is by using the Type Class, here is a prototype :
void App_Startup(object sender, StartupEventArgs e)
{
var assembliesList = new List<Assembly>();
Assembly assembly = typeof(ATestFixtureInTheTestsAssembly).Assembly;
assembliesList.Add(assembly);
NUnitTestRunnerWP7.NUnitTestRunner.Setup(assembliesList);
}
In this version, we use the runtime type of a test fixture located inside the assembly that we’re going to load, then using the .Assembly property. Using this trick also requires a reference to the assembly that contains the text fixture.

Last edited Feb 26, 2011 at 3:47 PM by gfinzer, version 1

Comments

feihong Oct 2, 2011 at 11:41 AM 
I downloaded it ,then runned it. It always throws an exception if the assert is wrong. So,what should I do with it?