Good Unit Test CheckList

Writing unit test? but don’t know if you covered everything? Here is a simply good unit test checklist.

Good Unit Test Checklist are green light for refactoring

Here are a few items I try to respect every time I write unit tests. It’s a solid and good unit test checklist. At least good for most of my code 🙂

Edit: a more extended and detailed list I wrote for DZone.

  • My test class is testing one and only one class
  • My methods are testing one and only one method at a time
  • My variables and methods names are explicit
  • My test cases are easy to read by human
  • My tests are also testing expected exception with @Test(expected=MyException.class)
  • My tests don’t need access to database
  • My tests don’t need access to network resources
  • My tests respect the usual clean code standards (length of lines, cyclomatic complexity,…)
  • My tests control side effects, limit values (max, min) and null variables (even if it throws an exception)
  • My tests can be run any time on any place without needing configuration
  • My tests are concrete (ex. dates are hardwired, not computed every time, strings too…)
  • My tests use mock to simulate/stub complex class structure or methods

Maybe you’ve already done a great deal  by appointing a Test Supervisor that checks every aspect of the finished product. That’s terrific. Congratulations.

But there are still bugs ?  Your team is telling you that this little change you asked took the all system down ? How comes ?

For more details about unit tests, see my series:  Test Culture Episode 1. The 101 Unit Testing Guide For Busy Managers.

I’m pretty sure there are more point to this unit test checklist.  Any idea ?

Author: Jean-Baptiste Rieu

Trained software engineer and now product manager. I ❤️ #space #architecture #typography #books #games #verticalfarming. I do #productmanagement #software #abtesting #data. I work on #payment @sundayapp_ Blogging mostly to practice writing, and to engage with others on life in Korea, products, engineering, books and anything worth geeking about.

15 thoughts on “Good Unit Test CheckList”

  1. I would add:

    • my tests are through, 100% coverage.
    • my tests check all likely method inputs
  2. The test case should get pass even if is executed in random order.The test case result should not depend on the previous test case. All test cases should be independent.

  3. I dream of a day when you could add to the checklist; “My tests are automatically generated for me by product XXX.”

  4. Regarding the 100% code coverage rule – I do not think it is that important, because it is rather easy to fake your unit tests in order to pass such a constraint. What code coverage is good for is to tell you what has NOT been covered.

  5. >My methods are testing one and only one method at a time
    This is not a good advice. Do not test methods, test features.


    Cheers,
    Tomek

  6. @Ramon

    Generating tests is no longer fiction, but perhaps Java is a bad candinate for property based testing. See QuickCheck for Haskell/Erlang as an example (I think Scala check is inspired from it)

Comments are closed.