Do you write one test per function/method, with multiple checks in the test, or a test for each check?
The answer to the more general question is yes, you should unit test everything you can. Doing so creates a legacy for later so changes down the road can be done with peace of mind. It ensures that your code works as expected.
This guideline is much more aggressive and recommended if you work in a test driven manner rather than write the tests after the code has been written. The main goal here is better code coverage or test coverage.
It isn't realistic -- or necessary -- to expect 100% code coverage through unit tests. The unit tests you create depend on business needs and the application or applications' complexity. Aim for 95% or higher coverage with unit tests for new application code.
A best practice is to run unit tests as part of a nightly build process. Show activity on this post. Ideally the unit tests should be run as part of every build and prior to every check in of changed code. However, with a large number of tests this can take a significant time, so you need to be able to manage that.
One test per check and super descriptive names, per instance:
@Test public void userCannotVoteDownWhenScoreIsLessThanOneHundred() { ... }
Both only one assertion and using good names gives me a better report when a test fails. They scream to me: "You broke THAT rule!".
I have a test per capability the function is offering. Each test may have several assertions, however. The name of the testcase indicates the capability being tested.
Generally, for one function, I have several "sunny day" tests and one or a few "rainy day" scenario, depending of its complexity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With