Using TestDriven.NET with XNA Game Studio Express

Henning Degn recently contacted me with a problem he was having running XNA code from TestDriven.NET. The code would run fine when executed from the application's 'Main' method. However when executing it as an ad-hoc test from TestDriven.NET he was seeing the following exception:

Microsoft.Xna.Framework.Content.ContentLoadException: Error loading "Content\myTexture". File not found. ---> System.IO.DirectoryNotFoundException: Could not find a part of the path  'C:\WINDOWS\assembly\GAC_32\Microsoft.Xna.Framework\1.0.0.0__6d5c3888ef60e27d\Content\myTexture.xnb'

It turns out that XNA tries to load its content files from a directory relative to the 'Assembly.GetEntryAssembly()' code base. The problem occurs when no 'Main' method is called (which happens when an ad-hoc or unit test executed) and no entry assembly is set. When this happens XNA defaults to using 'Assembly.GetCallingAssembly()' instead. Unfortunately the calling assembly is 'Microsoft.Xna.Framework' which is located in the GAC.

What we needed to do was find a way to set the entry assembly. Henning has documented our solution here. It basically involves calling a utility method 'AssemblyUtilities.SetEntryAssembly()' the beginning of each test.

Happy game development. :-)

Note: You will need TestDriven.NET 2.5 Beta or greater for XNA Game Studio Express support.

No Comments