Unit Testing · UT-01
Unit tests must be written to test application code · UT-01.1 · MUST · DEV
These basic rules should be followed:
- Unit tests should cover core business logic and validation, including edge cases
- Unit tests generally are not needed for simple CRUD functionality
- Communication is needed with test engineers to ensure effort is not duplicated among different kinds of automated test
- Unit testing is not a substitute for manual developer testing
Unit tests within a system must use a consistent naming convention · UT-01.2 · MUST · DEV
All unit test methods should follow the same naming convention. Each project can have it’s own conventions so long as it’s adequately documented.
Unit tests must have a descriptive name that documents the business logic under test · UT-01.3 · MUST · DEV
Writing unit tests as documentation helps to ensure that when the project is handed over to support, or when different teams are delivering new phases of work, there is clear, reliable, and up-to-date documentation of the code’s intended functionality. This facilitates smoother transitions and reduces onboarding time for new team members.
Unit tests must assert against the behaviour in the method name · UT-01.4 · MUST · DEV
In other words, don’t have a test method name that says one thing, and then assert on something else within the method.
Unit tests must be atomic · UT-01.5 · MUST · DEV
Each test must be independent of other tests, i.e. a test should not rely on any other test already having run.
Build validation must be in place to fail the build if unit tests fail · UT-01.6 · MUST · DEV
There should be a step in any pipeline that builds the software to also run unit tests.
Unit tests must not use data that may change between test runs · UT-01.7 · MUST · DEV
For example:
- Randomly generated data
DateTime.Now/DateTime..Today
EXCEPTION Random data can be used if it does not impact the outcome of the test, e.g. a mandatory property that is not needed by the behaviour under test.
Unit tests should not mock objects that are under the control of the system under test · UT-01.8 · SHOULD · DEV
For example, you should generally not mock other classes within your codebase, but should mock things like external APIs, message queues, and email clients.
Unit tests should not re-use domain logic within tests · UT-01.9 · SHOULD · DEV
Generally speaking, when business logic code is changed, unit tests that test that logic should fail. Re-using domain logic in a test can mean that happen, and therefore should be avoided.
Unit tests should use realistic test data · UT-01.10 · SHOULD · DEV
Using realistic data in unit tests can help to document the behaviour under test and ensures that the tests are as close as possible to real-world usage.
NOTE The Audacia.Seed library can be used to help the structured, reusable generation of test data.