Part One – Questions about Unit Test
What is a unit test?
In theory …
“Unit testing is a method by which individual units of source code are tested to determine if they are fit for use. A unit is the smallest testable part of an application. Wikipedia“

Ouch, someone has forgot to test the boot method !
When you write code, you can write a bug. Even if you are a senior developer who knows very well the language and the existing application, when you “modify” or “add” code, you can create bugs and regressions.
A unit test is a code which is written to test a tiny part of a production code. In a lot of cases, the aim of a unit test is to test a single method. Some unit test can test a complete functionality of the application. We will see that later.
Quickly, Unit test are here to produce quality and satisfaction. (For us and for our clients)
In our job …
You could have wrote this class and method
public class Calculator
{
public static int Addition(int number1, int number2)
{
return number1 + number2;
}
}
And write this unit test to test the result of the Addition method.
[TestClass]
public class TestCalculator
{
public void CanAddition()
{
// Arrange
var firstNumber = 10;
var secondNumber = 9;
// Act
var result = Calculator.Addition(firstNumber, secondNumber);
// Assert
Assert.AreEqual(19, result);
}
}
I will detail how to write unit test in the second par of this article. Here, I just want you to show how we could test a method. First, I arrange my test data. Secondly I call the method I want to test with my data and save the result. Finally I make assertion to test if the expected result is what the method returns.
Do I need to unit test my code?
Yes you do ! Unfortunately if you just arrived in a developper team and ask this question …you may have another response !
I have a little experience in programming and I can say, unfortunately that there is a lot of technical team leaders who don’t understand the purpose of unit testing and why some people (like me) think that unit testing is the base of every line of code. (Now you have my opinion on the subject, right?).
If it’s the first time you read an article about unit test or doesn’t know yet the purpose, you could say :
“Hey man, are you going to lose your time to write a test for this stupid method ?”
Okay, now my answer:
“Thanks for your comment buddy. First, it’s not a stupid method; it’s an amazing method which gives us the result of the addition of two integer numbers. And yes, I will write a test. And perhaps two or three more just for this method…”
There are no strict rules about how many unit test you have to write for a method. It depends on the code. But, even if the method is pretty simple like the addition example, it’s important to test the result and limit.If you work in a team, you know that a lot of people can change the code…and make regression !
Why do I need to lose my time in such test?
Try to inverse the question … How could we possibly build an application WITHOUT test ? and ask you some question about the code:
- Even if the method is simple, sometime bug can appear. We are just humans.
- You can develop an application with other developer. Someone can change the behavior of your code.
- A simple method can become bigger when a lot of functionality needs to be added … And the behavior could change.
- When you leave the project, another developer have to develop on your code and sometime introduce bug …
A lot of problem could append in a code.A lot of problem could append in the project. We are humans and we can’t be sure of what we do.
Unit test are here to help us.
And yes, test the code isn’t there to annoy developers
Unit test are here to
- Verify that the code in the method do what we expect.
- Verify that, after some modification in the code, the method behavior is always correct.
- Help the developer to modify, add or delete code without regression.
- Produce an application without bug, without regression and with a correct behavior.
- …and a lot of another purpose …in particular when we use TDD (Test Driven Development) technique … I will talk about this later.
Unit test are the basic or every development. It’s my point of view. I know that.
With this article I would like to change the mind of developer who thinks that unit test are just here to annoy them. You have to keep in mind that unit test could handle and resolve a lot of developers problems and improve the quality of your development and your production.
Understand Unit Testing for dummies (and films lover).
- I love Matrix. I love Unit testing.
- Matrix talks about software? Unit test too!
- Matrix talks about bug? Unit test can kill them!
- Matrix talks about Oracle? Unit test are the Oracle!
- Matrix talks about Neo’s fight against Agent (or bug in the matrix …)? Unit test helps the “the one” (you) to fight bugs in your application.
Sequence 1 – The Final fight
Okay so this is you. A good developer who cares about the software he wants to develop.

The developer (You)
The problem with software are bugs, regressions and time lost to track them.

Mr Bug, Mr Regression and Mr Time Lost
To track them and, better, to avoid and destroy them you have an ally: The Oracle. We could call them “A unit test” too.

The Unit Test
You could remark that the unit test loves the Red and Green color … But we will why later in this article. You could always see that the Unit test is happy because it makes Neo (You) happy too.
Hey look, this is a photo of you when you stopped all the bugs …Whaoo you’re so strong with Unit Test!

The developer stops bug thanks to all the unit test
After that, if you’re not convinced that Unit Test are here to help you and make you feel better in your job, I can’t do more. Sorry.
I love book. Do you know books which talk about Unit Testing?
I love books too ! And about Unit Testing you have all you need :
The Art of Unit Testing: with Examples in .NET (Roy Osherove)
Unit testing, done right, can mean the difference between a failed project and a successful one, between a maintainable code base and a code base that no one dares touch, and between getting home at 2 AM or getting home in time for dinner, even before a release deadline.
The Art of Unit Testing builds on top of what’s already been written about this important topic. It guides you step by step from simple tests to tests that are maintainable, readable, and trustworthy. It covers advanced subjects like mocks, stubs, and frameworks such as Typemock Isolator and Rhino Mocks….
Clean Code: A Handbook of Agile Software Craftsmanship (Robert C.Martin)
Even bad code can function. But if code isn’t clean, it can bring a development organization to its knees. Every year, countless hours and significant resources are lost because of poorly written code. But it doesn’t have to be that way.
Noted software expert Robert C. Martin presents a revolutionary paradigm with Clean Code: A Handbook of Agile Software Craftsmanship. Martin has teamed up with his colleagues from Object Mentor to distill their best agile practice of cleaning code “on the fly” into a book that will instill within you the values of a software craftsman and make you a better programmer—but only if you work at it…
Test Driven Development: By Example (Kent Beck)
Quite simply, test-driven development is meant to eliminate fear in application development. While some fear is healthy (often viewed as a conscience that tells programmers to “be careful!”), the author believes that byproducts of fear include tentative, grumpy, and uncommunicative programmers who are unable to absorb constructive criticism. When programming teams buy into TDD, they immediately see positive results. They eliminate the fear involved in their jobs, and are better equipped to tackle the difficult challenges that face them. …
Okay, I want to learn more about Unit Test. Could you help me ?
Yes ! If you’re interested in Unit Testing, stay tuned on Coding-in.net, I will present, in the second part of this article, how to make Unit test.


Great article and great books. Never thought of unit testing and Matrix together.
The Art of Unit Testing is a perfect book. I just wanted to let you know that Typemock is giving away a free chapter to anyone who likes their Facebook page and likes them. Just go here: http://on.fb.me/pEatjv