unit_testing : granularity of tests
-
Hi ! I'm new to unit testing and I need some clarification. I want to test a class. When I create an instance of this class with the default constructor, I need to check 3 values and make sure they are initialised to the correct default values (for instance : a name property). Do I have to code three tests (one for each value to test) or can I create one test for the default construction of the instance and check, in the same test, the 3 values ? Hope my question is understandable ! Thanks for your help ! Jerome
-
Hi ! I'm new to unit testing and I need some clarification. I want to test a class. When I create an instance of this class with the default constructor, I need to check 3 values and make sure they are initialised to the correct default values (for instance : a name property). Do I have to code three tests (one for each value to test) or can I create one test for the default construction of the instance and check, in the same test, the 3 values ? Hope my question is understandable ! Thanks for your help ! Jerome
There is no good reason to test each value separately. Your "unit" consists of the class itself, I assume, and your unit test should verify the class as a whole. If there is something unique about the way values are accessed, then the test should take that into account. But if they're accessible as a group, go ahead. Your test also should include verification of the methods within the class, and should be structured so that all private methods are utilized during the test. If the class has any dependencies on other classes, make sure that you exercise them thoroughly. Ideally, you should also try testing error conditions; try things you never intended the class to do and check how gracefully it fails. Use out of range values, incorrect variable types, and edge-of-limit values if there are any limit tests required. As a general guide, try to imagine what the dumbest consumer of your class might attempt to do to it, then duplicate that in your test. We used to call that "dummy-proofing" before there was such a thing as formal unit testing, and it worked remarkably well.:-D "...putting all your eggs in one basket along with your bowling ball and gym clothes only gets you scrambled eggs and an extra laundry day... " - Jeffry J. Brickley
-
There is no good reason to test each value separately. Your "unit" consists of the class itself, I assume, and your unit test should verify the class as a whole. If there is something unique about the way values are accessed, then the test should take that into account. But if they're accessible as a group, go ahead. Your test also should include verification of the methods within the class, and should be structured so that all private methods are utilized during the test. If the class has any dependencies on other classes, make sure that you exercise them thoroughly. Ideally, you should also try testing error conditions; try things you never intended the class to do and check how gracefully it fails. Use out of range values, incorrect variable types, and edge-of-limit values if there are any limit tests required. As a general guide, try to imagine what the dumbest consumer of your class might attempt to do to it, then duplicate that in your test. We used to call that "dummy-proofing" before there was such a thing as formal unit testing, and it worked remarkably well.:-D "...putting all your eggs in one basket along with your bowling ball and gym clothes only gets you scrambled eggs and an extra laundry day... " - Jeffry J. Brickley
Thanks Roger for your answer. It clarifies a lot what I have to do ! Jerome