Thursday, January 31, 2008

Automated Builds running .Net CF Unit Tests!

Having the capability of run unit testing for the .Net Compact Framework within Visual Studio 2008 is a very nice feature, but it goes beyond than just launch test runs from the IDE. If we have an automated build process or even a build server, we can include the .Net CF unit tests as part of the automated process. This is a great feature for practices like continuous integration or just for automated build processes.

But it's not completely painless. Probably you cannot even build the test project using MSBuild in your first try. I'm including in this post a sample solution, obviously a VS 2008 solution, which does can be built using MSBuild. It also run all the tests first on Windows Mobile 6 Classic Emulator and then on Windows Mobile 5 Pocket PC Emulator. Pretty sweet! But here goes the question:

How to make the project build and run the tests using MSBuild?

The first problem I found trying to build the test project is a missing reference. Actually, it's a missing search path on the project, because it's prepared for run from Visual Studio and not using MSBuild. To fix this, we just need to add a new search path in the project when it's not building inside Visual Studio:

1) Edit the test project file (i.e. TestProject1.csproj):
    - Unload the project (Right click - Unload Project)
    - Edit TestProject1.csproj

2) Add the following PropertyGroup:

<PropertyGroup Condition="'$(BuildingInsideVisualStudio)'!='true' ">

3) Now the project can be built from the command line! But we need to launch "MSTest" after build in order to run the tests as part of the *automated* testproject1 build process:

<Target Name="AfterBuild" Condition="'$(BuildingInsideVisualStudio)'!='true' ">
<Exec Command="MSTest /testcontainer:bin\debug\testproject1.dll /runconfig:..\SmartDeviceTestRun.testrunconfig" />

4) We can save the changes and reload the project (Right click - Reload Project)

It's done! Now you can build TestProject1.csproj using the default target and it will run the tests on the emulator as part of the build process.

Additionally, in the provided sample, I've removed the test list file (.vsmdi) and added a new .testrunConfig file which will be launched on Windows Mobile 5 Pocket PC (the first one will be launched on WM6 Classic Emulator, and I renamed it to clarify), and added a second <Exec> MSTest on AfterBuild target pointing this configuration file. Doing this, the automated build process will run the tests on BOTH platforms.

I'll post soon about how to create those testrun configuration files to setup different target platform for each test run in detail and what other ways we have to use them. But now, if you have Visual Studio 2008, and the WM6 Professional SDK installed on your machine, you can download the sample solution, try and open a "Microsoft Visual Studio 2008 Command Prompt" on the solution folder and just type MSBuild...

This is what I got:

Here's the sourcecode:

It seems we finally have automated test runs on the device as part of the build process... Sweet!!

1 comment:

Jeffry @ CWR Mobility said...

Hi Jose,

Thanks a lot for this great post! Really appreciated.

Now I just have to find the time to sit down and try this myself :).