views:

31

answers:

4

I am new to unit testing. I have created various tests and when I run test each one by one, all tests passing. However, when I run run on the whole as a batch, some tests failing. Why is that so? How can I correct that?

+1  A: 

Sometimes final conditions of one test have an impact on initial conditions of the next.

Manual run and batch run may have different behaviour regarding how initial conditions of each test are set.

mouviciel
+1  A: 

Probably some of your tests are dependent on the prior state of the machine. Unit tests should not depend on the previous state of the process/machine, so you should look at the failing tests and work out what they are depending on.

Douglas Leeder
+2  A: 

To resolve this issue it is important to follow certain rules when writing unit tests. Some of the rules are easy to follow and apply while other may need further considerations depending your circumstances.

Each test should set up a unique set of data. This is in particular important when you work with persistent data, e.g. in a database. When the test creates a user with a particular user id, then write the test so that it uses a different user id each time. For example (C#):

var user = new User(Guid.NewGuid());

At the end of each test, cleanup the data that the test has created. For example in the tear down method remove the data that you created (C#, NUnit):

[TearDown]
public void TheTearDownMethod() {
    _repository.Delete(_user);
}

Variations are possible, e.g. when you test against a database you may choose to load a backup just before you run the test suite. If you craft your tests carefully you don't need to cleanup the database after each test (or subset of tests).

To get from where you are now (each test passes when run in isolation) to where you would like to be start with running the first two tests in sequence, make them pass. Then run three in sequence, make them pass, etc. In each iteration identify what previous test causes the added test to fail. Resolve that dependency. This way you learn a lot about your tests but also how to avoid writing tests that depend on each other.

Once the suite passes in one patch run it frequently so that you detect dependency as early as possible.

This doesn't cover all scenarios and variations but hopefully gives you a guideline for building on what you already have.

John
Thanks a lot for the explanation. I have written 5 tests ad all of them passing, and when I write the last one it`s making the others failing. How can I make it run as the last test?
Conceptually they should be able to run in any sequence. To answer in your comment: Some unit testing frameworks allow you to specify an order on the tests or set a dependency of one test on another. In some cases it helps to change the position of the test method within the test fixture (= test class).
John
I am using NUnit and NCoverExplorer, is there a way to specify the order in any one of these two? In case you have used them?
In the case of NUnit it depends on the test runner you are using. I'm using ReSharper which executes tests in the order they appear in the source code. Other runners may behave differently. I think the GUI that comes with NUnit also executes it in that order. I am not familiar with NCoverExplorer. - Don't rely on that, however. Future implementations may change that behavior and execute the tests in a different order.
John
+1  A: 

You obviously have side effects from some tests that create unintended dependencies. Debug.

Good unit tests are atomic and with zero dependencies to other tests (important). A good practice is that each test creates (removes) everything it is dependent on before running the test. Cleaning up afterwards is also a good practice, it really helps and is recommended but not 100 % necessary.

Mahol25