As you know I haven’t been able to unit test my ViewModels since I updated to the beta tools. With the April CTP I was able to use a c# class library with standard NUnit, updated system references and ReSharper to run the tests. After updating to the beta tools I got the following errors:
When trying to update the system dlls to the beta:
“Module ‘System. Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e’ should be referenced.”
From ReSharper when attempting to run tests after manually editing the project file to update the system dlls:
“Unit Test Runner failed to load test assembly:
JetBrains.ReSharper.TaskRunnerFramework.TaskException: Could not load file or assembly ‘System.Device, Version=2.0.5.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e’ or one of it dependencies. The system cannot find the file specified.”
I scoured the web in search for the answer. At first, I found several references to the Silverlight Unit Test Framework from Jeff Wilcox which looks cool for xaml testing but I just want to test my ViewModels. I’m pretty sure I could have made it work but it was the wrong tool for the job.
Then I found Jamie Cansdale’s blog and his Silverlight NUnit Projects. This is what I was looking for, but would it work with a Wp7 app? Out of the box, no, but with some tweaks yes! Here is what I did.
- Installed the Silverlight NUnit Project templates (may require a VS restart)
- Added a Silverlight NUnit project to my solution – this project template comes with NUnit.Framework dll built with the Silverlight runtime in the lib folder (this is key)
- I also use Moq for mocking in my unit tests, so again I downloaded and referenced the dll compiled for Silverlight
-
At this point everything is built successfully and a sample test passed, but no references to the WP7 app yet. When I tried to reference the WP7 app I received the following warning –
“The Target Framework version for the project ‘RouteStats.Ui’ is higher than the current project Target Framework version. Would you like to add this reference to your project anyway?”:
“You can only add project references to other Silverlight projects in the solution”:
So I changed the target framework by opening the project properties (right click on project from solution explorer and choose properties) and changed the Target Silverlight Version from “Silverlight 3” to “Silverlight 4”. - Next I fixed the “You can only add project references to other Silverlight … “ error by altering the project file. This required unloading the project, then right clicking on the project and choosing “Edit [NameOfProjectFile].csproj”.
Changed the ProjectTypeGuids node to match the WP7 app, saved the file and then reloaded the project. Changed node to”:<ProjectTypeGuids>{C089C8C0-30E0-4E22-80C0-CE093F111A43};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
- Now I could successfully reference the WP7 app. Again everything built and sample test passed.
- At this point, I added my old tests and the references they require and everything worked great.
That wasn’t to bad right? I’m just glad its over and I can continue using TDD for my ViewModels (MVVM rocks!). I don’t think many developers have had problems with running unit tests against their Windows Phone apps because they haven’t had tests that require references to the phone system dlls. I was testing my geo-location code which requires a reference to System.Device.
Hopefully someone can use this post to get their tests running as well.
Please leave feedback in the comments so I know if any of this is confusing or isn’t working for you. I haven’t tested all areas of my app yet or used all the features of the phone so its possible that this solution won’t always work. I will cross that bridge if I find it exists.
Pingback: Tweets that mention Unit Testing (NUnit) a WP7 Project « dotnet Catch -- Topsy.com
all rightttt
that was needed…
great explanations and i dont think that i will get any errors
Thanks! That was exactly that I was looking for.
Pingback: 1st day of WinPhone dev « FrankMao.com
This is awesome! Thanks a lot!
You can also try this project on CodePlex:
http://nunitwindowsphone7.codeplex.com/
Pingback: Guillaume Collic » Unit Tests and TDD with Visual Studio Express for Windows Phone
Pingback: Guillaume Collic » Tests unitaires et TDD avec Visual Studio Express for Windows Phone
Great !~!
Is this run on desktop ?
Or emulator of WP7 ?
can i test on WP7 emulator ?
These tests run on the desktop or on your build server.
Check out the codeplex project in Greg’s comment for running nUnit tests on the phone.
Did you ever get this working at the command line (an/or CI server)?
The standard nunit console executable will not work. I did find the following comment on another blog which says it will work if you get the nunit source and recompile with the silverlight mscorlib.
http://greenicicleblog.com/2009/12/18/running-nunit-2-53-tests-for-silverlight/
Regarding configuring a project with NUnit tests. Probably the easiest way to create a new project is to create it from the Silverlight NUnit Project template (http://www.testdriven.net/downloads/SilverlightNUnitProject.zip)
See for more information:
http://weblogs.asp.net/nunitaddin/archive/2008/05/01/silverlight-nunit-projects.aspx
http://blog.clauskonrad.net/2011/05/wp7-how-to-unit-test-your-viewmodels.html
Pingback: Unit Testing WP7 Apps Day 1
Hello!
I got this working, but not exactly by the steps described in this post. You can check my step list @ http://juarola.wordpress.com/2011/09/06/unit-testing-windows-phone-7-mvvmlight-viewmodels-with-7-1-sdk-rc-nunit-and-moq/
Thank you for providing a tip about _that_ version of Moq that worked in my scenario!