In this article I am going to talk about unit testing for Windows Phone 7.
First, there is no option for a windows phone test project within the IDE.
In our XING-App we use Silverlight Unit Test Framework and NUnit as the provider for test metadata.
We have an additional test project to run unit tests called TestRunner.
Reference these Microsoft.Silverlight.Testing.dll and Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.dll assemblies from the Silverlight Unit Test Framework to these project.
Note: You will get a warning about adding silverlight 3 assembly, it is fine and you can click yes to continue.
Set up the testpage
Hide the system tray to have more space if you like.
Register NUnit as the provider for test metadata (attributes etc). That tells the silverlight testing framework to use the NUnit test attributes instead of the default MSTest attributes.
tell the silverlight testing framework to use the NUnit test attributes
- UnitTestSystem.RegisterUnitTestProvider(
We use an additional project for the unit tests.
So we need to add assemblies, that contains the tests.
Add unit tests
Now any NUnit test fixture you add to the project should work.
Note that you can only have NUnit tests or MSTest tests in a project, not a mixed combination.
Create the testpage and let the unit testing framework take over.
Set the testpage
- var testPage = UnitTestSystem.CreateTestPage(settings)
- as MobileTestPage;
- BackKeyPress += (x, xe) => xe.Cancel =
- testPage.NavigateBack();
- (Application.Current.RootVisual
- as PhoneApplicationFrame).Content = testPage;
Using NUnit
Download the binaries from the nunit-silverlight project and add NUnit.Silverlight.Compatibility.dll and NUnit.Silverlight.Metadata.dll as reference to the test project.
Tag your unit tests to run specific subsets of tests. When you start your TestRunner-App you can choose to only run tests with a certain tag, a certain set of tags, or not run tests with specific tags
Tagging a testmethod
- [Test]
- [Tag("UnitTestTag")]
- public void AlwaysPass()
- {
- Assert.IsTrue(true, "method intended to always pass");
- }
Integration Tests
Now combine unit tests (modules) and test it!
For the integration tests we follow our created test plan to chose aggregates.
Id | XXXX | | |
Test Type | Explorative / Serversimulation | | |
Title | Logon is necessary after logout. | ||
Test Goal | Ensure that the user has to log on again after he performs a log off. | ||
Precondition | Same as XXXX | ||
Steps | Automated Test: IntegrationTests. | ||
Expected Result | The network news page is shown. | ||
Note | |
Our WP7-App has a lot of asynchronous method calls like web service calls.
To test asynchronous methods you will first have to make sure your TestClass inherits from SilverlightTest.
Next, you will need to mark the test method as asynchronous. Finally, you will have to call EnqueueTestCompleted() to tell the test when it has completed.
If you do not call this then the test will never complete.
The XING-App make use of a TestBase class. This class inherits from SilverlightTest and overrides EnqueueConditional.
To make sure all tests end, EnqueueConditional becomes a timeout mechanism. The test will fails and EnqueueTestCompleted is called and the test is really completed.
Asynchronous testmethod timeout
- ConditionalTimeout);
- timer.Tick += delegate
- {
- // remember
- // to stop timer or it'll tick again
- timer.Stop();
- };
- EnqueueCallback(timer.Start);
- base.EnqueueConditional(conditionalDelegate);
- EnqueueCallback(timer.Stop);
Fake test server
We are in the test process and we do not want to run the tests against the live XING-Server, so we need such as a fake XING server. This fake server also helped us in the development process.
During the development we got no test XING-Accounts. The only way to test was with our live accounts.
Our fake server is a normal web service and is similar to the XING API. We also have functions like:
- make many users
- make visit
- make many messages
- make number of contacts
It was also very useful to test functionality against this test server. For example, if you need some contact requests.