How to write a good test case: 5 tips to write better test cases
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.
- Over complicated tests. Tests which are difficult to understand.
- Overlapping tests. Different tests testing the same functionality, this will carry a lot of overhead when you will have to maintain them.
- Low code coverage. Is easier to have higher code coverage if you write simple test cases for all the elements of your code.
- 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:
- Write your own asserts, for instance, you can see the custom assert I wrote for the chess test case example above.
- Make clear the 4 stages of every test case: Setup, Exercise, Verification and Tear down
- 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.
Related posts:
![[Google]]( http://www.makinggoodsoftware.com/wp-content/plugins/easy-adsenser/google-light.gif)
It is simply fantastic information never seen before..thanks
SD
25 Aug 09 at 10:21 am
“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.
divclasshidden
25 Aug 09 at 3:50 pm
[...] How to write a good test case: 5 tips to write better test cases | Making Good Software http://www.makinggoodsoftware.com/2009/08/25/how-to-write-a-good-test-case5-tips-to-write-better-test-cases – 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 [...]
Twitter Trackbacks for How to write a good test case: 5 tips to write better test cases | Making Good Software [makinggoodsoftware.com] on Topsy.com
25 Aug 09 at 8:04 pm
#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
[...] due respect to the author of FIRST but I prefer my version.) Trackback URL: [...]
Are your unit tests on FIRE? | bigvisible.com
18 Nov 09 at 8:13 pm
[...] How to write effective test cases How to write a good test case var fbShare = {url: [...]
8 Tips To Create Complete Test Cases | Test And Try
25 Feb 10 at 1:14 am
It is nice to go on with my journey of testing.
thank u boss
subash
29 Dec 10 at 10:36 pm
hi,
agood test case is
1.sipmle to understand
2.user end prosepective
3.no repaet.
4.No if loops
5.excutable
6.all cover functionality
7.un necessary.
Madhu
26 Jan 11 at 11:08 pm
[...] 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 [...]
QUnit, testeando nuestras aplicaciones Javascript. | EtnasSoft
1 Feb 11 at 8:11 am
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.
Jan
16 Sep 11 at 12:50 am
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
produktentwicklung…
[...]How to write a good test case: 5 tips to write better test cases | Making Good Software[...]…
produkttester
2 Oct 11 at 12:29 am