Unit and Integration Guidelines and Standards
First of all, it is better to write unit tests to test logic without touching any external resources. Unit tests are the responsibility of the development team.
But, at some point, we need to check that units works as expected with real dependencies, and this is where we need integration tests.
Rules and Standards:
- Separate unit tests from integration tests:
Reason:Unit tests are safety net for developers. They must provide quick feedback about expected behavior of system units after last code changes (bug fixes, new features). If they are run frequently, then developer can quickly and easily identify piece of code, that broke the system. Nobody wants to run slow unit tests. Therefore, we should test as much logic as possible with unit tests.
Integration tests are generally slower than unit tests. But they have different purpose. They check that units works as expected with real dependencies.
- Rule of thumb for need of integration test:
- A test uses the database
- A test uses the network
- A test uses an external system (e.g. a queue, service-bus or a mail server)
- A test reads/writes files or performs other I/O
- Comparison between unit test and integration test
- Integration test scenarios:
- Happy path tests: test when everything is OK
- Boundary tests: testing logic in boundary cases
- CI/CD test integration process:
- Developers run unit test during development
- Developers run unit test before any commit
- Developers run integration test before a major commit with many side effects
- Build server compiles code and runs the unit test every 15-30 minutes (main build)
- Build server compiles code and runs the integration-test every day (integration build)
- Build server compiles code and runs the integration-test before a release to QA
Goals:
Clear standards: Make the purpose of each test absolutely clear.
Automation: The ability to re-run tests quickly and without too much manual tweaking.
Repeat ability: A test-situation that you can "reset", so you can re-run tests quickly, with only slight variations.