Setting Up XnaMobileUnit to run unit tests in Windows Phone 7 XNA Games

Introducing XnaMobileUnit

I decided to roll my own unit test framework for Windows Phone 7 XNA games.   I have a couple of reasons why I decided to roll my own.  Among them was the fact that you cannot reference regular .Net assemblies from Windows Mobile Framework.  That means that referencing NUnit was out of the question.  Also using some of the other libraries that can be used for Silverlight Applications didn't quite feel right for me.

The framework that I have created, XnaMobileUnit, is very simple to use and provides all the necessary functionality to run unit tests.  I am still working on the framework to provide a few more capabilities and features, but for the most part it is ready to use out of the box.  XnaMobileUnit is open source licensed under the Apache 2.0 License and can be downloaded from  I recommend downloading the latest source and compiling it then referencing the new DLL. Otherwise you can simply download the latest DLL.

Download the Assembly

To use it you must first download the XnaMobileUnit DLL.  Once you do that create a new Windows Phone 7 XNA Game and reference the assembly.  The XNA Game that you create is the container for your unit tests, not the actual game that you are creating.  You can then reference the actual game that you are creating so that you can run unit tests agains those classes in your game.

Create the XNA Game for your unit tests

When you create your XNA Game you will have a file named Game1.cs.  In that file add a field of the type TestsUi.   You will instantiate that field in your Game1 constructor and add it to the components.  One thing that the TestsUi requires is a font so please create a SpriteFont in your Content project, load the font, and set your font in the TestsUi.

        public Game1()
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            _testsUI = new XnaMobileUnit.Api.TestsUi(this);


            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);

Once you do that you will need to run your tests from the LoadContent method in your Game1 class.  You don't have to add any additonal logic in your update or draw because TestsUi is a DrawableComponent and as such once you add it to the Components collection of your game, it automatically updates and draws.

        protected override void LoadContent()
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            SpriteFont font = Content.Load("SpriteFont1");


Once you have done that you can set your unit test game as your start up project and run your tests.

Create the Unit Tests

Did I mention that you have to add unit tests.  Of course this program will run no tests if there isn't any to run.  Organize your tests as you normally would. 

To create a test simply create a class like shown below.

    public MyTestFixture : TestFixture
         public override Context()


         public void TestFoo()
            bool someCondition = true;
            Assert.IsTrue(someCondition, "Should have returned true becase");

Run Your Tests

To run the tests simply run the unit test game and it will display all of the tests and the application will show you which ones failed and which ones passed.

You can visit the to get the latest information on using XnaMobileUnit.   By the time you read this, you should also be able to find links there to youtube videos showing you the setup process.

Happy coding.


Anton said…
Thanks for the library.

Doign exactly like you do on the video.
Just inserting the
"private TestsUi _testUi;" into Game class gets me the exception.

A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.Game.dll

Even w/o using the type. Seems smth with assembly load?
Fernando Zamora said…
Do you think you can send me your project to test it out. Maybe some screen shots.
Fernando Zamora said…
BTW you can email me at
Anonymous said…
This is a neat little framework
Anonymous said…
I am getting the same error as Anton, could you share the solution to this problem?

A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.Game.dll
Anonymous said…
I am getting the same error as Anton. Could you share how was this error fixed?

A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.Game.dll
Fernando Zamora said…
Anonymous can you please email me some screen shots. I just did a fresh download and ran it through like the instructions and had no problem.

I'd like to get to the bottom of it but I need more information.
If you can provide the exception details and also where it was thrown.
Unknown said…
Does this work for VS Express for WP?
Unknown said…
Is this available for VS Express for WP or just VS Pro?

Popular posts from this blog

Simple Example of Using Pipes with C#

Difference Between Adapter and Bridge Design Patterns

IoC Container Vs. Service Locator