Making Good Software

A blog by Alberto G (Alberto Gutierrez)

Written by Alberto Gutierrez

August 25th, 2009 at 7:22 am

How to write a good test case: 5 tips to write better test cases

with 12 comments

1.- Write a test case for every condition.

There’s a common misunderstanding- some developers translates “one test case for every condition” to “one test case for assertion”, that’s just plain wrong, a condition can contain one or more assertions, check the following test case.

public void testPawnCanMoveTwoSquaresAheadFromInitialRow (){
    //Test moving first a white pawn
    assertPawnCanMoveTwoSquaresAheadFromInitialRow ("a2", "a4");
    //Test moving later a black pawn
    assertPawnCanMoveTwoSquaresAheadFromInitialRow ("h7", "h5");

private void assertPawnCanMoveTwoSquaresAheadFromInitialRow (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, only one condition is tested and it’s composed of many assertions. When you are picking the condition to test, it is also a good idea to focus on the boundaries of your system, so if you are testing a range, try testing with the last element of your range, the first element…

2.- Test only one thing .

If you don’t write your tests to only test one thing, you may find the following problems.

  1. Over complicated tests. Tests which are difficult to understand.
  2. Overlapping tests. Different tests testing the same functionality, this will carry a lot of overhead when you will have to maintain them.
  3. Low code coverage. Is easier to have higher code coverage if you write simple test cases for all the elements of your code.
  4. Difficulty to track an error. When your test cases fail, ideally it should be pretty straight forward to tell where they are failing. When you are testing several things in every test case is hard to tell what the source of the errors is.

To test only one thing is sometimes hard because of the dependencies in your system. You may want to write a test case for a class that indirectly uses a database, or a third class, in this case, the best solution is to use a mock, stub or a fake object.

3.- Make the test case describe how your system works.

Test cases should be used as well as documentation on how your system works, they will tell the user how can you use the interfaces of the different classes and what kind of errors can you expect. A few tips to help creating test cases that self document your code are:

  1. Write your own asserts, for instance, you can see the custom assert I wrote for the chess test case example above.
  2. Make clear the 4 stages of every test case: Setup, Exercise, Verification and Tear down
  3. Make your test code as clean as your production code.

4.- Organize your test cases consistently.

There are several ways to organize your test cases, by class, functionality… Whatever the structure you pick, just keep the consistency so it will be easy to locate and add new test cases in the future.

5.- Make “FIRST class” test cases (Fast, Independent, Repeatable, Small and Transparent)

  • Fast. Your test cases should be very fast to execute, every time you want to run all of them it shouldn’t take more than a few seconds for an small application.
  • Independent. You should be able to run your test cases in any order.
  • Repeatable. The result of the test case should be always the same, no matter how many times you have executed it before.
  • Small. Small test cases are easy to understand and change, are also likely to be faster.
  • Transparent. It should be clear what the purpose of each test case is.

12 Responses to 'How to write a good test case: 5 tips to write better test cases'

Subscribe to comments with RSS or TrackBack to 'How to write a good test case: 5 tips to write better test cases'.

  1. It is simply fantastic information never seen before..thanks


    25 Aug 09 at 10:21 am

  2. “Test cases should be used as well as documentation on how your system works”

    Test cases should test how your system SHOULD work, not what it actually does.


    25 Aug 09 at 3:50 pm

  3. […] How to write a good test case: 5 tips to write better test cases | Making Good Software – view page – cached , 1.- Write a test case for every condition. There's a common misunderstanding- some developers translates one test case for every condition to one test case — From the page […]

  4. #1 in particular looks like a pretty naive pattern. There are good arguments on the other side of this discussion that in particular lead to specification style tests that offer better reading etc.

    With a specification I would move my code at the beginning of your method into a SetUp and then put each assertion into its own “Test” method.

    This in general can give me much more readable tests and I will tend to get more information when something fails (especially when multiple something fail because I don’t need to run the test n times to know that).

    Greg Young

    31 Aug 09 at 8:25 am

  5. […] due respect to the author of FIRST but I prefer my version.) Trackback URL: […]

  6. […] How to write effective test cases How to write a good test case var fbShare = {url: […]

  7. It is nice to go on with my journey of testing.

    thank u boss


    29 Dec 10 at 10:36 pm

  8. hi,

    agood test case is

    1.sipmle to understand
    2.user end prosepective repaet.
    4.No if loops
    6.all cover functionality
    7.un necessary.


    26 Jan 11 at 11:08 pm

  9. […] los puntos anteriores conforman el acrónimo inglés F.I.R.S.T con el que también pueden definirse las pruebas unitarias: Fast, Independent, Repeatable, Small y […]

  10. Hi, I really like your posts. Thank you for all of them!

    Unfortunately, your page is not printable. The social banner on the left hand site is still in the print.


    16 Sep 11 at 12:50 am

  11. Hi Jan,

    Thanks for the observation, I haven’t acutally ever thought about it, but you have a good point, I’ll see if I can do something to make the page more printable friendly.

    Alberto Gutierrez

    16 Sep 11 at 1:43 am

  12. produktentwicklung…

    […]How to write a good test case: 5 tips to write better test cases | Making Good Software[…]…


    2 Oct 11 at 12:29 am

Leave a Reply