Tuesday, July 26, 2005

NUnit - quick and easy


I searched for a quick and easy introduction to NUnit. There are some nice articles, but they all go into more detail than I want. I just want to take a look at something simple to see what all the fuss is about.


So, here's the smallest introduction to NUnit 2.2 you'll find. Hopefully after you've gone through this procedure you'll be able to see where you can use NUnit in your own projects. You'll then be ready to look at the more complex articles referenced at the end of this one.


Assumptions



The Procedure



  1. Download and install NUnit. This article was based on NUnit-2.2.0.

  2. Start Visual Studio.NET

  3. Create a blank solution.

  4. Within your blank solution, create a new project called "NUnitTesting".

  5. Within the "NUnitTesting" project, right click "References", then click on "Add Reference" in the context menu.

  6. Click the Browse button.

  7. Navigate to C:\Program Files\NUnit 2.2\bin

  8. Doubleclick nunit.framework.dll

  9. Click OK. You have just added the NUnit framework to your project.

  10. Modify the Class1.cs file so that it looks like this:

    using System;
    using NUnit.Framework;

    namespace NUnitTesting{
    public class CalcInt{
    public int add(int a,int b){
    return a+b;
    }
    }

    [TestFixture]
    public class CalcIntTestFixture{
    CalcInt testCalcInt;

    [SetUp] public void Init(){
    testCalcInt = new CalcInt();
    }

    [Test] public void AddSuccess(){
    Assert.AreEqual(2,testCalcInt.add(1,1));
    }

    [Test] public void AddFails(){
    Assert.AreEqual(3,testCalcInt.add(1,1)); // FAILS, 1+1 != 3
    }
    }
    }

  11. Press Shift-Control-B to save/compile the dll

  12. Start->All Programs->NUnit 2.2->NUnit-GUI

  13. File->Open

  14. Navigate to the DLL that was created when you compiled 3 steps ago, and open it.

  15. Click on the Run button.

  16. One test should succeed, and one should fail -- hopefully the tests are simple enough to not need explanation.

  17. That's it! There is a lot more to learn about NUnit, but this article should give you some tools to get started.


Resources



Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?