views:

268

answers:

7

Possible Duplicate:
What is unit testing?

What is unit testing and unit testing libraries like xUnit? I understand it's testing specific code, so what's the difference between this and just opening a new project and test this specific code?

+2  A: 

Unit testing is about writing code that tests your application code.

The Unit part of the name is about the intention to test small units of code (one method for example) at a time.

xUnit is there to help with this testing - they are frameworks that assist with this. Part of that is automated test runners that tell you what test fail and which ones pass.

They also have facilities to setup common code that you need in each test before hand and tear it down when all tests have finished.

You can have a test to check that an expected exception has been thrown, without having to write the whole try catch block yourself.

Oded
How exactly does it help?
TTT
@Alon - expanded on my answer.
Oded
+13  A: 

The main difference of unit testing, as opposed to "just opening a new project and test this specific code" is that it's automated, thus repeatable.

If you test your code manually, it may convince you that the code is working perfectly - in its current state. But what about a week later, when you made a slight modification in it? Are you willing to retest it again by hand whenever anything changes in your code? Most probably not :-(

But if you can run your tests anytime, with a single click, exactly the same way, within a few seconds, then they will show you immediately whenever something is broken. And if you also integrate the unit tests into your automated build process, they will alert you to bugs even in cases where a seemingly completely unrelated change broke something in a distant part of the codebase - when it would not even occur to you that there is a need to retest that particular functionality.

This is the main advantage of unit tests over hand testing. But wait, there is more:

  • unit tests shorten the development feedback loop dramatically: with a separate testing department it may take weeks for you to know that there is a bug in your code, by which time you have already forgotten much of the context, thus it may take you hours to find and fix the bug; OTOH with unit tests, the feedback cycle is measured in seconds, and the bug fix process is typically along the lines of an "oh sh*t, I forgot to check for that condition here" :-)
  • unit tests effectively document (your understanding of) the behaviour of your code
  • unit testing forces you to reevaluate your design choices, which results in simpler, cleaner design

Unit testing frameworks, in turn, make it easy for you to write and run your tests.

Péter Török
+1 In addition, my favourite part about test code (especially when given a new codebase): It demonstrates the expected use of the code under test.
SnOrfus
+2  A: 

unit testing is a practice to make sure that the function or module which you are going to implement is going to behave as expected(requirements) and also to make sure how it behaves in scenarios like boundry conditions, invalid input

Xunit, Nunit, mbUnit etc.. are tools which help you in writing the tests

Rony
+2  A: 

Library like NUnit, xUnit or JUnit are just mandatory if you want to develop your projects using the TDD approach popularized by Kent Beck:

you can read this: http://www.agiledata.org/essays/tdd.html or Kent Beck's book

Then, if you want to be sure your tests cover a "good" part of your code, you can use sw like NCover, JCover, PartCover or whatever. They'll tell you the coverage percentage of your codes. Depending on how much you're an adept of TDD, you'll know if you've practiced it well enough :)

PierrOz
+1  A: 

First of all, whether speaking about Unit testing or any other kinds of automated testing (Integration, Load, UI testing etc.), the key difference from what you suggest is that it is automated, repeatable and it doesn't require any human resources to be consumed (= nobody has to perform the tests, they usually run at a press of a button).

Thomas Wanner
+2  A: 

I think the point that you don't understand is that unit testing frameworks like NUnit (and the like) will help you in automating small to medium-sized tests. Usually you can run the tests in a GUI (that's the case with NUnit, for instance) by simply clicking a button and then - hopefully - see the progress bar stay green. If it turns red, the framework shows you which test failed and what exactly went wrong. In a normal unit test, you often use assertions, e.g. Assert.AreEqual(expectedValue, actualValue, "some description") - so if the two values are unequal you will see an error saying "some description: expected <expectedValue> but was <actualValue>".

So as a conclusion unit testing will make testing faster and a lot more comfortable for developers. You can run all the unit tests before committing new code so that you don't break the build process of other developers on the same project.

AndiDog
+1  A: 

I would like to recommend the xUnit Testing Patterns book by Gerard Meszaros. It's large but is a great resource on unit testing. Here is a link to his web site where he discusses the basics of unit testing. http://xunitpatterns.com/XUnitBasics.html

Paul Hildebrandt