Developing solid unit tests can be a challenge for the uninitiated. It’s important that any test you develop is a genuine test of the code as well as being replicable multiple times during the development cycle. To help you with this we think you can use a simple checklist which allows you to ask the right questions during test development to deliver good unit tests.
Checklist for good unit tests
- Can the test be automated? To keep the amount of manual work to a minimum, your ideal unit test is one that can be run automatically over and over again. It’s important to develop your tests with discrete pass/fail outcomes that can be readily understood by the development team when you’re done.
- Have I got the right level of code coverage? You want to ensure that your unit tests protect your code against a range of problems. It might seem easiest just to develop tests that are focused on areas where issues always seem to arise, but the greater the level of code coverage, the less chance of problems being discovered post-release.
- Can the test be repeated? Unit-tests are supposed to deliver the same results whenever they are run. If they aren’t replicable – then your test design may have gone awry. The first place to look is the inputs for the test which need to be strictly managed for the right outputs of automated software testing.
- Can the test be delivered independently? A unit-test is best when it measures a single behavior. If your test fails it should indicate exactly where in the code there is a problem and what that problem might be. Test results should not depend on the execution of any prior tests. Ideally each unit-test should be delivered in isolation from any other and still produce the same results.
- Does your test design mirror your code design? Unit tests are ideally developed in the same manner as your code. That way any developer in your team should be able to tell at a glance what the purpose of a particular test is. You should also set a standard for test names. The more concise your test scripts are, the easier it is for someone else to follow them.
- Does the test run speedily? You want your developers to run unit tests on a regular basis – the only way to persuade them to do so is if the execution of test cases is relatively quick. Each individual test should take less than a second to execute. That way the entire test suite executes as fast as possible.