views:

55

answers:

4

I'm starting to use NUnit to write test cases in C# and Visual Studio 2010 and .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?

  1. Should I add UNnit code to the same class library project to test against? Or add a separate project in the same solution for NUnit test cases? What is the best practice?
  2. If I need to create a separate project for the NUnit test cases, shall I make it a class library project or make it an executable? If I made it a class library project, how to run it in order to test the class library project in 1?
  3. If I need to test against an executable, not a class library project, anything changes for the process and projects?
+3  A: 
  1. The Unit Tests should go in a separate Test project under the same solution.
  2. Yes, make it a class library project. If you want to run the tests in NUnit, this tutorial can show you how.

Regarding your comment: When you set NUnit.exe up as the default Start executable for the Test Class Library (the one that contains your Unit Tests), you tell NUnit which DLL you want to test; after the first time it will subsequently remember which project you're running the tests against.

You also want to make sure you have references in the Unit Test library that refer to the other project.

Again, the Tutorial I listed goes through all of this.

George Stocker
Thanks! If using another test project to test against the class library, how did the test project know it tests against the class library? Do I need to make reference from the test project to the class library project in order to make sure what projects the test project is testing against?
George2
This, and 3. No, nothing changes - your executable can be tested in exactly the same way as your library. And yes, your test project will need to reference the project it's testing.
Lunivore
+2  A: 

2: You should place all NUnit tests in a seperate project in the same solution. Building the project builds the tests, and vice versa, so as you're TDDing this project (you are doing that, right?) you can simply run the tests and it will build everything necessary to do so. 3: Class library. It doesn't need to be runnable by Windows if you're using NUnit; you just need to use the test runner.

KeithS
Thanks! If using another test project to test against the class library, how did the test project know it tests against the class library? Do I need to make reference from the test project to the class library project in order to make sure what projects the test project is testing against?
George2
You need to reference the namespace of the tested objects in code with a using statement, but as long as the project being tested and the project with the tests are in the same solution, you should be able to see the other class without having to add an actual reference; the compiler will create intra-solution references for you.
KeithS
+1  A: 

I would add a separate test project for the test code and reference NUnit and the library under test from that. It should be a class library, and it gets run by the NUnit test runner(s) e.g. nunit-console test_assembly.dll - see the documentation.

Lee
Thanks! If using another test project to test against the class library, how did the test project know it tests against the class library? Do I need to make reference from the test project to the class library project in order to make sure what projects the test project is testing against?
George2
+2  A: 

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:

  • open test case class library project properties, Debug tab
  • set to open with external program: point this to your nunit.exe
  • set command line arguments to the exact name of your test case dll: MyTests.dll
  • working directory: just click the ellipsis button and it will pre-select your test dll output directory for the current build config
  • set the test project to be the 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.

pelazem
Thanks, question answered!
George2