Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 972
George2 Avatar asked Oct 20 '10 15:10

George2


People also ask

How do you use unit testing in NUnit?

Creating the first testSave this file and execute the dotnet test command to build the tests and the class library and run the tests. The NUnit test runner contains the program entry point to run your tests. dotnet test starts the test runner using the unit test project you've created.


3 Answers

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.

like image 175
pelazem Avatar answered Sep 29 '22 17:09

pelazem


  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.

like image 8
George Stocker Avatar answered Sep 29 '22 15:09

George Stocker


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.

like image 2
KeithS Avatar answered Sep 29 '22 17:09

KeithS