Using Testdriven.net with XNA

Hi people,
 
Since a long time of no writing, today I’m going to talk a little about Testdriven.Net.
 
Testdriven.Net is a unit test for .Net language used for a lot of people that help us to write fast and better code. It’s because we don’t need to fully design all stuff in advance, sometimes making mistakes about what we think about a game project. Instead, we design the game piece by piece, where at start of each piece, we first writte a test and after this test is complete, we do the code to accomplish this test. It’s very useful because first we think about the design, without worrying about decision code, performance and things like that. We only think about this stuff after the unit test is working.
 
Now, after the code for that we make the test is done and working, we can modify the design to get a better performance, speed and so on, refactorying the code. All the time you change the code, you can execute the test to verify if it is working.
 
You can write an ad hoc unit test like this one below:
 
public static void TestXnaInitialize()
{
XnaGame game = new XNAGame();
game.run();
}
 
It can be executed without any change in the main method. However, if the game load any asset like a model, an effect or texture, you will receive an error message like that:
  

Microsoft.Xna.Framework.Content.ContentLoadException:
Error loading “ContentmyTexture”. File not found. —> System.IO.DirectoryNotFoundException:
Could not find a part of the path ’C:WINDOWSassemblyGAC_32Microsoft.Xna.Framework1.0.0.0__6d5c3888ef60e27dContentmyTexture.xnb’
 
It’s because the content is not inside the GAC(Global Assembly Cache). The content manager is using the codebase of the assembly who’s main method called it as its root. Unfortunatly, Testdriven.net ad hoc function cannot suply this to the content manager, it default to is own location, inside the GAC.
To be able to to that, I discovered from Jamie Cansdale (credits for him) at Testdriven.NET, that you need to unpack this file into your project and add it to the solution. After that, in all ad hoc unit test using assets, you use this line below before the unit test:
 
Testdriven.NET.AssemblyUtilities.SetEntryAssembly();
 
So, as an example:
 
public static void MyTestWithAssets()
{
Testdriven.NET.AssemblyUtilities.SetEntryAssembly();
//Put your code here
}
 
Now, you can execute you test normally, without the error above!
 
Any comment, please send me an email.

Leave a Reply

Your email address will not be published. Required fields are marked *