writing good unit tests
-
Hi all, I'm just getting into writing unit tests - can see the benefits of them for sure. I get the impression that there is quite a bit of skill and know-how in writing decent tests that are worth their salt. The tests are only as good as the coder writing them, eh? ;) It would be great to see some examples beyond the example of the bank account that is given with nunit. I wondered if anyone here knows of any good examples of some unit tests on a more involved real world application? Or if anyone has any thought or advice on this matter it would be great to hear too. Sorry it's a bit of a vague question - :) But very much looking forward to your replies. Best wishes Steven
-
Hi all, I'm just getting into writing unit tests - can see the benefits of them for sure. I get the impression that there is quite a bit of skill and know-how in writing decent tests that are worth their salt. The tests are only as good as the coder writing them, eh? ;) It would be great to see some examples beyond the example of the bank account that is given with nunit. I wondered if anyone here knows of any good examples of some unit tests on a more involved real world application? Or if anyone has any thought or advice on this matter it would be great to hear too. Sorry it's a bit of a vague question - :) But very much looking forward to your replies. Best wishes Steven
Not really got any examples I could share but some ideas to help you out: * Test with good input * Test with bad input * Test with input at the edge of valid ranges. e.g. If your method can accept an integer in the range 1 to 100 test that 0 and 101 fail properly (throwing the correct exception, or returning the appropriate success indicator), and test that 1 and 100 work. * Test different combinations of data. e.g. if it doesn't matter which order two parameters are given, then check that by testing with these parameters in a different order. * Look at the possible code paths inside your method and try and construct tests that will take the code through each of the possible paths. Probably other people will reply also with other ideas, but these are some that immediately spring to mind.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way! My Blog
-
Not really got any examples I could share but some ideas to help you out: * Test with good input * Test with bad input * Test with input at the edge of valid ranges. e.g. If your method can accept an integer in the range 1 to 100 test that 0 and 101 fail properly (throwing the correct exception, or returning the appropriate success indicator), and test that 1 and 100 work. * Test different combinations of data. e.g. if it doesn't matter which order two parameters are given, then check that by testing with these parameters in a different order. * Look at the possible code paths inside your method and try and construct tests that will take the code through each of the possible paths. Probably other people will reply also with other ideas, but these are some that immediately spring to mind.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way! My Blog
Thanks Colin, That's great.:) Makes sense and I'll give it a go. Cheers Steven
-
Hi all, I'm just getting into writing unit tests - can see the benefits of them for sure. I get the impression that there is quite a bit of skill and know-how in writing decent tests that are worth their salt. The tests are only as good as the coder writing them, eh? ;) It would be great to see some examples beyond the example of the bank account that is given with nunit. I wondered if anyone here knows of any good examples of some unit tests on a more involved real world application? Or if anyone has any thought or advice on this matter it would be great to hear too. Sorry it's a bit of a vague question - :) But very much looking forward to your replies. Best wishes Steven
steven shingler wrote: It would be great to see some examples beyond the example of the bank account that is given with nunit. Ever heard of google? I've written several articles on unit testing, and that's just me! :rolleyes: http://www.google.com/search?hl=en&ie=UTF-8&q=unit+test+site%3Acodeproject.com[^] Marc Microsoft MVP, Visual C# MyXaml MyXaml Blog