tags:

views:

82

answers:

3

Can anyone tell me what is NUnit in .NET and how to use it? I have heard that using this tools improves design of an application. Is it true? Where can I get an absolute beginners tutorial on this topic?

+3  A: 

NUnit is a framework for conducting unit testing.

If you're not familiar with unit testing, "Test Infected" may be a good introduction for you. It's about jUnit (Java) rather than NUnit, but I don't think that should be too much of a problem for you.

There are lots of books on unit testing - Roy Osherove's "The art of unit testing" comes pretty highly recommended.

Jon Skeet
Thank you very much, jon
Night Shade
+1  A: 

What you're asking about is unit testing. In Layman's terms, instead of testing your application by running it, you instead write code that tests your application in small chunks. An example of a test would be something like this:

[Test]
public void Get_All_People_Returns_Correct_Number_Of_People()
{
    // setup
    database.Add(new Person());
    database.Add(new Person());
    database.Add(new Person());

    // test
    Assert.That(database.GetAllPeople().Count(), Is.EqualTo(3));
}

You should definitely read more about unit testing in general, separation of concerns, and good application design before you get started with NUnit. Unit testing is only really effective when your application is designed in small, testable blocks that don't interfere with each other.

Daniel T.
Thanks to you too, Daniel
Night Shade
+2  A: 

NUnit is a unit testing framework - rather than using it directly in your application, it allows you to write seperate suites of tests with which to test the components of your framework.

Unit testing is a test approach whereby you are testing small individual units of your application, rather than testing the whole thing as one.

The reason this helps improve the design of an application is that it encourages you to write your application in small units, each of which is decoupled from the others and so can be tested. This enforces the notion of "seperation of concerns"; making sure that each type is responsible for one task only.

Furthermore, lots of people approach development using a "unit test first" philosphy, whereby you write your test for some code before you write the actual code. The idea here is the following sequence:

  • Write your test. This is a failing (RED) test as there is no code to test.
  • Write your code so that the test works (GREEN).
  • Now you have a test to verify your code, you can easily re-write and improve your code, with a high level of confidence that you are not breaking it. (REFACTOR)

You will here people refer to this RED-GREEN-REFACTOR approach

There is lots of information on how to approach unit testing on the web. Just Goole "unit testing C#".

However, have a look at the documentation on NUnit's site (down as I write this for some reason), and look at other SO posts related to unit testing.

Also, here is a pretty extensive code project article, although I haven't dug deep into it so I don't know if it is any good.

Maybe others with good references can add them as comments to this article.

Rob Levine
Thank you very much, Rob. Your answer helped me a lot :)
Night Shade