views:

107

answers:

1

I'm Writing a simple pong game in C# and XNA 4.0 to learn unit testing. The tools used are TestDriven.NET and NUnit, all newest versions. The problem is, if I test the code with VS2010 internal debugger, everything runs fine, but when I use "Run Test(s)" from menu, the application chokes with error:

Test 'WindowsGame1.Game1.TestGameMenu' failed: Microsoft.Xna.Framework.Content.ContentLoadException : Error loading "SpaceBackground". File not found.
----> System.IO.FileNotFoundException : Error loading "Content\SpaceBackground.xnb". File not found.
(some more below...)

So it stops when the first textre is going to be loaded. What's going on?

If you want to check the code out, download it here http://www.mediafire.com/?qwnkmyqheum

+1  A: 

The obvious question here is what the working directory is, when TD.NET runs the test. If your files are in C:\game1\content\*, and TD.NET runs the tests while operating in C:\TDNET (or whatever), then obviously trying to load Content\Whatever.jpg is going to try and open C:\TDNET\Content\Whatever.jpg, which doesn't exist. The simple workaround would be to set an absolute path for your content files (if only temporarily) to verify that this is the problem.

The other question is, what version of TestDriven.net are you using? Their release notes from 2.7 say this:

966: Problems with XNA tests (working directory issues)

See here and here for workaround.

The first site links to the second one, which offers this workaround:

Step 1.

Download AssemblyUtilities.zip and unpack into your project and add it to your solution.

Step 2.

For every ad hoc test you wish to run, use the command:

Testdriven.NET.AssemblyUtilities.SetEntryAssembly();

This needs to be called before content loading, such as in the first line.

An example of an ad hoc test can be seen here:

public static void myAdHocTest()

{
    Testdriven.NET.AssemblyUtilities.SetEntryAssembly();
    //Do your testing here
}

Step 3.

Run the ad hoc tests like normal, by putting the caret in the test, right-click and choose “run test(s)”.

Mark Rushakoff
It worked! Thanks million times
Quang Anh