首页 > .NET > How to install and run NUnit in .Net

How to install and run NUnit in .Net

Being new to the agile world, all of these new tools can be overwhelming. One of the first things you may want to do as an developer is write a test. As developers, we are curious about how good our code is, but how do you write a test? What tools do you use in the .Net environment? By the end of this tutorial, I will show you how to install, setup, and use NUnit to write your very first test. Once you learn how to write these tests, you will realize just how easy it is to test your code.

Prerequisites:

In order to follow along in this tutorial, you will need the following

  • Visual C# 2010 Express (any version Visual Studio 2010 should do)
  • NUnit (Please note that in order to get the NUnit client to run, you need to have .Net Framework 2.0 installed. It won’t matter if you are using a different version of the framework for your development.)

NUnit Installation:

To install NUnit click on the “.msi” file from the download page, link above, and double click the file and open the file once it’s been downloaded. The “Complete” setup type will be fine. Everything else is pretty much your standard installation. Once the installation is complete, the necessary DLLs will be on your computer and you will be ready to incorporate them into your test project.

Writing NUnit Tests:

Writing a test with NUnit is fairly straight forward. Lets start with the following method called “AddTwoNumbers”.

namespace NUnitTutorial
{
   public class MathObject
   {
      public int AddTwoNumbers(int num1, int num2)
      {
         int result = num1 + num2;
         return result;
      }
   }
}

What you can see is that the method simply adds two numbers together and returns the result. Lets write an NUnit test to make sure this method is returning what we expect. We need to do the following to write a test:

  1. Add a new class library project to your solution. This new project should have the following naming conventions “ProjectUnderTest_Test” where “ProjectUnderTest” is the name of the project that contains the methods you are writing the tests against. image This project will contain all of your tests. Above you will notice that I have a project called “NUnitTutorial” that contains the “AddTwoNumbers” method and a project called “NUnitTutorial_Tests” that will contain the unit tests for that method.
  2. Next add the following DLL to the test project “nunit.framework” image
  3. Write our test.

To write the test we first need to add a using for the project under test (NUnitTutorial) as well as the NUnit framework (NUnit.Framework).

using NUnit.Framework;
using NUnitTutorial;

Now lets add our test to the MathObject_Tests.cs file. We first need to let NUnit know that this class contains tests that need to be ran. To do this we mark the class with the following attribute “[TestFixture]”.

using NUnit.Framework;
using NUnitTutorial;

namespace NUnitTutorial_Tests
{
     [TestFixture]
     public class MathObject_Tests
     {
     }
}

Now, lets add the test method “AddTwoNumbers_returns_3_when_given_1_and_2”. (I always like to name my test methods this way so that I know what method they are testing as well as what the expected inputs and outputs are.) Once we add this method we need to add the attribute “[Test]” to it so that NUnit knows that this is a test to be ran. Note, that all test methods should be void methods, not return anything.

using NUnit.Framework;
using NUnitTutorial;

namespace NUnitTutorial_Tests
{
  [TestFixture]
  public class MathObject_Tests
  {
       [Test]
       public void AddTwoNumbers_returns_3_when_given_1_and_2()
       {
       }
  }
}

All that’s left is to call the method with our two inputs and assert that the result from the method is what we expect.

using NUnit.Framework;
using NUnitTutorial;

namespace NUnitTutorial_Tests
{
  [TestFixture]
  public class MathObject_Tests
  {
    [Test]
    public void AddTwoNumbers_returns_3_when_given_1_and_2()
    {
       MathObject mObject = new MathObject();
       int calculatedResult = mObject.AddTwoNumbers(1, 2);
       int exptResult = 3;
       Assert.AreEqual(exptResult, calculatedResult);
    }
  }
}
The above test simply calls the “AddTwoNumbers” method, giving it a 1 and a 2 and asserts that the result from the method is what we are expecting, which is a 3. Now, that we finished writing the test, we need to run it. Build your solution and generate those DLLs.

Running NUnit:

Start up the NUnit client by going to Program Files –> NUnit x.x.x –> NUnit. Once it is up and running, click on File->Open Project and navigate to where the DLLs generated from the test project are located. You want to load the test project DLL into NUnit. image Once the DLL is loaded, it will show you the tests that are in that DLL. image From here all you need to do is click the “Run” button and NUnit will run your tests and output the results. image As you can see, our test passed. Now what would have happen if the test had failed? Let’s find out. Lets change our expected result to be 4 and see what happens. image There you go all the information you need to get NUnit installed and on your way to creating tests and rock solid code.

转载请标明出处:萝卜根

原文地址请标明:原文地址

  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.
*

:yund: :yun: :yes: :xiaxia: :xiaom: :xiaohan: :wuyu: :wuxiao: :woshou: :woquan: :wink: :tiaodou: :tiaod: :sikao: :pa: :oops: :ok: :no: :mad: :lihai: :leihua: :lei: :ku: :konghe: :kbu: :jiwai: :jiong: :jiay: :huo: :huaixiao: :hanxiao: :han: :haha: :guolai: :guan: :guai: :ganga: :eek: :dou: :diao: :deng: :buli: :bizui: :bishi: :biggrin: :arrow: