I\'m looking to start a project targeting Windows Phone, but I\'m slightly put off by the lack of unit testing support[1].
I\'m used to using NUnit/ XUnit for most o
Update: Unit testing for Windows Phone 8 is supported now.
http://blogs.msdn.com/b/visualstudioalm/archive/2013/01/31/windows-phone-unit-tests-in-visual-studio-2012-update-2.aspx
I managed to unit test my viewmodels used in a VS2013 WP8.0 application, using Moq. Here is my solution structure: - WP8 (Silverlight) with a home made Prism library - WP8 Portable Library as my Presentation layer - WP8 Unit tests project
Go here and download the latest Moq version. In the the Silverlight folder, you'll find 2 dlls, one for Moq and one for Castle Core. Use them as reference in your unit test project. Note that VS2013 won't allow you to directly add those DLL as reference. So unload your project, edit the .csproj, manually add the 2 references and reload your project
Not a very clean way, but it's the only way i found to unit test my viewmodels.
Here's what we do:
If you do this, then you can use the the build-in Visual Studio unit testing framework, to test Windows Phone projects (not just Portable Class Libraries) with a bit of hackery.
1) First step is to disable strong name validation , this can be done with following Powershell script:
reg DELETE "HKLM\Software\Microsoft\StrongName\Verification" /f
reg ADD "HKLM\Software\Microsoft\StrongName\Verification\*,*" /f
if ($env:PROCESSOR_ARCHITECTURE -eq "AMD64")
{
reg DELETE "HKLM\Software\Wow6432Node\Microsoft\StrongName\Verification" /f
reg ADD "HKLM\Software\Wow6432Node\Microsoft\StrongName\Verification\*,*" /f
}
Restart-Service msiserver
Notice that it creates a security vulnerability on the machine this is applied to!
2) Second step is to create a Visual Studio Unit Test Project, a regular one, not a Windows Store Unit Test Project.
3) Third step is to include the Windows Phone 8 project as a reference. This works, and you can now use Microsoft Fakes to make mscorlib and System.dll , allowing you to fake types like DateTime
in your unit tests, a very handy feature.
4) Fourth step is to get a local copy of the following Windows Phone assemblies (and any additional you need):
They can be found in the C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\Tools\MDILXAPCompile\Framework folder.
Copy them locally to a /lib/ folder, and then unload and edit the MSBUILD xml of your Unit Test Project.
Change:
<Reference Include="System.Windows" />
<Reference Include="Microsoft.Phone" />
To
<Reference Include="System.Windows, Version=2.0.6.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, processorArchitecture=MSIL">
<HintPath>lib\System.Windows.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Phone, Version=8.0.0.0, Culture=neutral, PublicKeyToken=24eec0d8c86cda1e, processorArchitecture=MSIL">
<HintPath>lib\Microsoft.Phone.dll</HintPath>
</Reference>
Additionally to avoid a warning when compiling, add following element to the first <PropertyGroup>
<ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
Reload, compile, run the tests. Voilà, and you're done. You can even integrate this with the Microsoft Code Coverage functionality.
NB: Unit Testing is not officially supported by the Windows Phone Division, this is a workaround found by my team and I at the Skype Division.
Bonus answer:
Does anyone know if you can target Windows Phone 7.x using the WP8 SDK?
I've done some fairly basic tests and found that a Portable Library Class project and a normal Class Library for the tests work well.
The caveat to this is that I've yet to find an MVVM framework built for portable class libraries, making the View Models untestable...unless...you use file linking (and assuming the View Models inherit from an MVVM framework specific base class). P While its not ideal, it's the best way forward I've found.
It's not, at the time of writing at least, on NuGet in either stable or pre-release, but I'll do some investigation into its usage and update.
I've also had word that the Windows Phone 8 SDK will be able to target Windows Phone 7.