views:

142

answers:

3

Should I do

Test1(){
  Assert(TestCase1);
  Assert(TestCase1);
  Assert(TestCase3);
  ...
}

or

Test1(){
  Assert(TestCase1);
}
Test2(){
  Assert(TestCase2);
}
Test3(){
  Assert(TestCase3);
}
...

Note: All test cases are intimately related. There is only boolean differences between them.

A: 

You should use the second scenario. If you use the first scenario and Test1 fails, you don't know where the problem lies... it could be in any number of things being tested. With the second scenario, you know precisely what single thing is being tested.

ahockley
A: 

The second scenario helps you detect exactly what went wrong, and you'll always know the result of all assertions.

You may be tempted to think the first scenario have an advantage that you can use the same arrangement for all the assertions. But, in case your first assertion fails, you'll be missing whether the other two would pass.

I'd suggest the second one, once most testing frameworks allows you to do a one-time arrangement for many tests.

Samuel Carrijo
+8  A: 

One test case = one condition to be tested, some people translates condition to assert, that's wrong, a condition can be composed of one or more asserts

Example: Imagine that you are developing a chess game, and you have just implemented the movement functionality and you want to test it, check the following test case.

public void testPawnCanMoveTwoSquaresAheadFromInitialRow (){
    [...]
    //Test moving first a white pawn
    assertPawnCanMoveTwoSquaersAheadFromInitialRow ("a2", "a4");
    //Test moving fater a black pawn
    assertPawnCanMoveTwoSquaersAheadFromInitialRow ("h7", "h5");
}

private void assertPawnCanMoveTwoSquaersAheadFromInitialRow (String from, String to){
    [...]
    Piece movingPiece = board.getSquareContent(from);
    assertTrue (movingPiece.isPawn ());
    assertTrue (board.move (from, to));
    assertTrue (board.getSquareContent(from).isEmpty());
    assertTrue (board.getSquareContent(to).isPawn());
    [...]
}

As you can see this example is very clear, if it fails you will know exactly where your application is failing, makes very easy to add new test cases, and as you can see tests only one condition, but uses many asserts.

You may want to check this recent article I have wrote on my blog: How to write good tests

Alberto Gutierrez