How do I set up NUnit to run my project's unit tests?

27,979

Solution 1

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.

Solution 2

  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.

Solution 3

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.

Solution 4

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.

Share:
27,979
George2
Author by

George2

Updated on March 01, 2020

Comments

  • George2
    George2 about 4 years

    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?

    1. 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?

    2. 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?

    3. If I need to test against an executable and not a class library project, are there any changes to the process and/or projects?