This is my first blog in years and I would like to share my experience on unit testing.
Lets start with the obvious, so what is a unit test?
According to wikipedia a unit test is a method by which individual units of software are tested to determine if they are fit for use. In my humble opinion some other characteristics of a good unit test are as follows:
Make sure your unit test case can be run multiple times without failing. It should not assume any special setup nor should it leave behind any state and always cleanup at the end.
Obviously if your unit test case is a part of ordered test suite where next in the order test case depends on the state created by previous tests you are allowed to leave state, however the test suite as whole should be repeatable and do cleanup towards the end.
Easy to debug and maintain
Ideally a short stdout message should indicate why the test failed however sometimes its recommended to have appropriate amount of logging built into your unit test. Maintainability of test is of huge importance. It should be easy to identify what part of code/feature a unit test case is testing so that if it fails, its easy for a developer to verify if the test is outdated or the code is wrong.
Fast
I hate slow tests. Slow tests usually indicate a badly written test which is depending on external data sources. Obviously if the operation your unit test is testing is slow, your test itself will be slow, but more often its not the reason.
Lets start with the obvious, so what is a unit test?
According to wikipedia a unit test is a method by which individual units of software are tested to determine if they are fit for use. In my humble opinion some other characteristics of a good unit test are as follows:
- Repeatable and Clean
- Easy to debug and maintain
- Fast
Make sure your unit test case can be run multiple times without failing. It should not assume any special setup nor should it leave behind any state and always cleanup at the end.
Obviously if your unit test case is a part of ordered test suite where next in the order test case depends on the state created by previous tests you are allowed to leave state, however the test suite as whole should be repeatable and do cleanup towards the end.
Easy to debug and maintain
Ideally a short stdout message should indicate why the test failed however sometimes its recommended to have appropriate amount of logging built into your unit test. Maintainability of test is of huge importance. It should be easy to identify what part of code/feature a unit test case is testing so that if it fails, its easy for a developer to verify if the test is outdated or the code is wrong.
Fast
I hate slow tests. Slow tests usually indicate a badly written test which is depending on external data sources. Obviously if the operation your unit test is testing is slow, your test itself will be slow, but more often its not the reason.
No comments:
Post a Comment