Test data for NUnit tests in .Net and C#

P

P Chase

I have recently moved from the Java world to .Net and C#. I am writing
my first set of C# unit tests, using NUnit and Visual Studio 2005 Pro
(not Team with the integrated test stuff). I am so far always running
the tests in the NUnit GUI runner, invoked from VS Tools menu.

Some of my tests need to provide a substantial (100s of k) chunk of
binary data to the methods being tested. Clearly, it is not sensible
to try inlining this in my test classes. So my question is what is the
nicest way to make the test data available to the test classes?

I can add test files to my VS projects, but how can I ensure that
NUnit can find them? I want it to be able to find them even if someone
checks out a copy of my project into a different directory.

All suggestions appreciated.
 
L

Lasse Vågsæther Karlsen

P said:
I have recently moved from the Java world to .Net and C#. I am writing
my first set of C# unit tests, using NUnit and Visual Studio 2005 Pro
(not Team with the integrated test stuff). I am so far always running
the tests in the NUnit GUI runner, invoked from VS Tools menu.

Some of my tests need to provide a substantial (100s of k) chunk of
binary data to the methods being tested. Clearly, it is not sensible
to try inlining this in my test classes. So my question is what is the
nicest way to make the test data available to the test classes?

I can add test files to my VS projects, but how can I ensure that
NUnit can find them? I want it to be able to find them even if someone
checks out a copy of my project into a different directory.

All suggestions appreciated.

Add them to the test project as resources, and reference them that way.
Right-click the project node and select Properties, then go to the
resource tab and add files there, they'll then be available through the
Resource class in your project.
 
J

Jon Skeet [C# MVP]

I have recently moved from the Java world to .Net and C#. I am writing
my first set of C# unit tests, using NUnit and Visual Studio 2005 Pro
(not Team with the integrated test stuff). I am so far always running
the tests in the NUnit GUI runner, invoked from VS Tools menu.

Some of my tests need to provide a substantial (100s of k) chunk of
binary data to the methods being tested. Clearly,  it is not sensible
to try inlining this in my test classes. So my question is what is the
nicest way to make the test data available to the test classes?

I can add test files to my VS projects, but how can I ensure that
NUnit can find them? I want it to be able to find them even if someone
checks out a copy of my project into a different directory.

I personally just add them as files but make sure they are copied to
the output directory (look at the file properties to check this). Find
out the directory by finding the location of the first module in your
test type's assembly (I don't have MSDN to hand, but something like
typeof(MyTest).Assembly.Modules[0].FileBase).

I prefer this to the approach of using resources because (in my view)
regular files are easier to work with, and there's also less for the
build process to do.

Jon
 
P

P Chase

I have recently moved from the Java world to .Net and C#. I am writing
my first set of C# unit tests, using NUnit and Visual Studio 2005 Pro
(not Team with the integrated test stuff). I am so far always running
the tests in the NUnit GUI runner, invoked from VS Tools menu.
Some of my tests need to provide a substantial (100s of k) chunk of
binary data to the methods being tested. Clearly, it is not sensible
to try inlining this in my test classes. So my question is what is the
nicest way to make the test data available to the test classes?
I can add test files to my VS projects, but how can I ensure that
NUnit can find them? I want it to be able to find them even if someone
checks out a copy of my project into a different directory.

I personally just add them as files but make sure they are copied to
the output directory (look at the file properties to check this). Find
out the directory by finding the location of the first module in your
test type's assembly (I don't have MSDN to hand, but something like
typeof(MyTest).Assembly.Modules[0].FileBase).

I prefer this to the approach of using resources because (in my view)
regular files are easier to work with, and there's also less for the
build process to do.

Jon

Thanks for the two suggestions.

In fact, I had independently found the "embed file as resource"
option, and it is working for me. I can see that there are pros and
cons to this, compared to the "copy to output dir" option, but at the
moment I am happy with what I have.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top