I’m starting to use NUnit to write test cases in C# with Visual Studio 2010 on .NET 4.0. I want to use NUnit to test against a .dll (a C# class library project)’s public functions. How do I set up NUnit to work with my project?
-
Should I add NUnit code to the same class library project to test against, or should I add a separate project in the same solution for NUnit test cases? Which is the best practice?
-
If I need to create a separate project for the NUnit test cases, should I make it a class library project or an executable? If I made it a class library project, how do I run it?
-
If I need to test against an executable and not a class library project, are there any changes to the process and/or projects?
Re 2
Generally, keep your [test case dll] separate from your [business logic dll]. Your business logic dll shouldn’t include any knowledge of NUnit, to separate concerns and simplify deployment/maintenance.
Your test case dll should include a reference to NUnit, and to your business logic dll.
You do not need to share a namespace. You can expose internal members of your business logic dll to your test case dll by modifying the business logic dll’s AssemblyInfo.cs file to expose internals to the test case dll. This lets you preserve your desired visibility in the business logic dll.
Re 3
Your test cases should go in a dll (i.e. class library project). You can load this directly into NUnit’s UI, or use NUnit’s console runner in an integration environment to run your tests automatically.
How I do it:
properties, Debug tab
point this to your nunit.exe
exact name of your test case dll:
MyTests.dll
ellipsis button and it will
pre-select your test dll output
directory for the current build
config
default start project in the
solution; this way, whenever you hit
F5 (or the “Play” button), NUnit will
come right up with your updated tests
preloaded – very convenient and
quick.
Best of luck – also try out the Test project type avl. in Visual Studio, it’s very similar to NUnit. I still prefer NUnit but while learning it’s good to try some various options.