views:

238

answers:

6

I am a college student that makes his programs here and there in c#, mostly scientific simulations. Is there any use in learning NUnit? I keep hearing people talking about it but I don't fully grasp what it is in the first place. Is it something that every programmer should use, or something that is more suited for big projects with lots of people?

Thanks

+6  A: 

Yes you should use it. (or any test runner)

Testing is quite an important part of programming (In my opinion), so if you (or someone else) changes a part of code, and it happens to break something down the line, your tests should pick this up.

Well, that's how I see it, I'm still new to unit testing.

An Example.

Lets say I have a method

Add(int a, int b)
{
  return a + b;
}

My Unit Test would be:

[Test]
public void Test001_TestAdd()
{
    Assert.IsTrue(Add(1,3) == 4);
}

If someone changed the Add Method to lets say:

Add(int a, int b)
{
  return a * b;
}

My Unit test would fail, and could potentially save a life (if you work in medical software, or for NASA), or stop a potential bug :)

Now if they had Unit Tests, this may not have happened, simple problem, missed.

PostMan
+2  A: 

Every programmer should learn it. Now, will every programmer use it? Likely not, but they should know it anyway.

Unit testing is very, very common nowadays, and understanding how it works is essential in any project other than the little hobby ones you're writing right now. Use the time you have now and get ready for your future. Also learn about Mock objects (related).

Eduardo Scoz
+3  A: 

Absolutely. Unit testing is one of the most important concepts to learn in modern software development.

It doesn't matter if your programs are scientific simulations, enterprise applications, etc. neither if you are in a big team or if you code alone (but in big teams is as important as compiling your code).

A good book to start on unit testing (for .net) is Pragmatic Unit Testing in C# with NUnit

Pablo Fernandez
+1  A: 

NUnit is an excellent tool if you're interested in Test-Driven Development.

See my answer here for some notes about TDD.

I've found that if you can get a ReSharper license (Visual Studio plug-in that does many things) that the running of unit tests becomes so quick and easy that writing them really does become a no-brainer, even for small pieces of work.

Richard Ev
It's a great tool even if you're not interested in TDD! Even if you write tests after you write code, you should be doing unit testing. Not that I'm saying you shouldn't do TDD.
Dennis Palmer
+1  A: 

Unit testing has many values, depending on what you do and what projects you work on different parts of unit testing will be important to you.

At very least you should learn the basics of unit testing to have a fully rounded sense of the development process. As time goes on and you work on larger projects you will find that unit testing is an integral part of maintaining and growing large codebases.

GrayWizardx
+20  A: 

Testing is a vital part of writing software - any kind of software. Whether you are writing a little routine to sort numbers, or a system to launch space ships to Mars.

Now it used to be that developers would write their code, and then exercise it by hand - often in a debugger - to verify that it did what they expected. This was a painful and tedious way to test their code. Not only was it tedious and painful - but it wasn't repeatable. This meant that every time you made a change you had to manually repeat the tests and hope you didn't forget anything or do things slightly differently. Quality suffered - and there was much sorrow and weeping.

Eventually, developers realized that they could write a separate program that would exercise their code by running it through the conditions that they expected it to handle, and having that code verify the results. Things started to get better. This approach to testing was often called a test harness.

Writing test harnesses helped, but there was a lot of repetitive code - there is quite a bit of infrastructure related to running and verifying tests. However, some really smart developers out their said: "We're not going to write boiler plate code over and over ... we'll make a library to do it for us." And so unit testing tools like JUnit and NUnit (and many others) were born.

These libraries (and tools) help you structure, organize, and execute your tests. They provide convenient methods to verify conditions, trap and trace exceptions, and report which tests passed and which ones failed. Finally, developers could focus on the important part: writing the tests and verification logic that would help assure that the code they wrote worked. These tests were repeatable. They could be run as part of an automated build process. They could be handed down through the generations and added to by others as new failure points were found and fixed. They could be re-run by the original developer months and years after the original code had been written to make sure things still work.

If you plan to be a successful software developer at any level, in any size organization (even if it's just yourself) - you should learn about - and embrace unit testing.

Here are some resources to help you on your way:

LBushkin
Plus one. Great thoughtful answer and excellent list of resources. I would add two more resources because I think if we're going to talk about testing we have to talk about one of the two great things to come out of XP: TDD. 1. TDD Starer Kit: http://codebetter.com/blogs/jeremy.miller/archive/2005/07/21/129659.aspx 2. Test Driven Development: By Example http://www.amazon.com/Test-Driven-Development-Kent-Beck/dp/0321146530
Jason
Plus one. One caveat, perhaps: I have seen in academic settings for people writing small, one-off scientific apps that the output (result, answer) of the app is, itself, the best test there is. So TDD might not be that useful. I've also seen that these apps tend to grow larger and more complicated, at which point you wish you had the tests. YMMV.
chadmyers