Skip to content

Commit 0d3898e

Browse files
dbettiniMiroDojkic
andauthored
Clarify integration and API test distinction (#11)
* Clarify integration and API test distinction - Removed the conflation of integration and API tests - Added infrastructure and entry points integration subsections - Added a note to API tests to mention the distinction from integraiton * Update wording Co-authored-by: Miro Dojkic <[email protected]> * Expand the glossary Also reworded Entry Points slightly. --------- Co-authored-by: Miro Dojkic <[email protected]>
1 parent 7ef23a3 commit 0d3898e

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

recipes/automated-testing.md

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Automated Testing
22
## Glossary
3-
**Confidence** - describes a degree to which passing tests guarantee that the app is working
4-
**Determinism** - describes how easy it is to determine where the problem is based on the failing test
3+
**Confidence** - describes a degree to which passing tests guarantee that the app is working.
4+
**Determinism** - describes how easy it is to determine where the problem is based on the failing test.
5+
**Use Case** - a potential scenario in which a system receives external input and responds to it. It defines the interactions between a role (user or another system) and a system to achieve a goal.
6+
**Combinatiorial Explosion** - the fast growth in the number of combinations that need to be tested when multiple business rules are involved.
57

68
## Testing best practices
79

@@ -89,10 +91,26 @@ changes, we can make minimal changes to the test suite and/or mocked data.
8991

9092
### Integration Tests
9193

92-
With these tests, we test the application API endpoints and assert that they are
93-
actually working as expected.
94+
With these tests, we test how multiple components of the system behave together.
9495

95-
**TODO**: do we want to add that we should run full backend for these type of tests?
96+
#### Infrastructure
97+
98+
Running the tests on test infrastructure should be preferred to mocking, unlike in unit tests. Ideally, a full application instance would be run, to mimic real application behavior as close as possible.
99+
This usually includes running the application connected to a test database, inserting fake data into it during the test setup, and doing assertions on the current state of the database. This also means integration test code should have full access to the test infrastructure for querying.
100+
> [!NOTE]
101+
> Regardless of whether using raw queries or the ORM, simple queries should be used to avoid introducing business logic within tests.
102+
103+
However, mocking can still be used when needed, for example when expecting side-effects that call third party services.
104+
105+
#### Entry points
106+
107+
Integration test entry points can vary depending on the application use cases. These include services, controllers, or the API. These are not set in stone and should be taken into account when making a decision. For example:
108+
- A use case that can be invoked through multiple different protocols can be tested separately from them, to avoid duplication. A tradeoff in this case is the need to write some basic tests for each of the protocols.
109+
- A use case that will always be invokeable through a single protocol might benefit enough from only being tested using that protocol. E.g. a HTTP API route test might eliminate the need for a lower level, controller/service level test. This would also enable testing the auth layer integration within these tests, which might not have been possible otherwise depending on the technology used.
110+
111+
Multiple approaches can be used within the same application depending on the requirements, to provide sufficient coverage.
112+
113+
#### Testing surface
96114

97115
**TODO**: do we want to write anything about mocking the DB data/seeds?
98116

@@ -114,13 +132,16 @@ time decoupling logic testing and endpoint testing.
114132
- To verify the API endpoint performs authentication and authorization.
115133
- To verify user permissions for that endpoint.
116134
- To verify that invalid input is correctly handled.
135+
- To verify the basic business logic is handled correctly, both in expected success and failure cases.
136+
- To verify infrastructure related side-effects, e.g. database changes or calls to third party services.
117137

118138
#### When **not** to use
119-
- For testing of specific function logic. We should use unit tests for those.
139+
- For extensive testing of business logic permutations beyond fundamental scenarios. Integration tests contain more overhead to write compared to unit tests and can easily lead to a combinatorial explosion. Instead, unit tests should be used for thorough coverage of these permutations.
120140
- For testing third party services. We should assume they work as expected.
121141

122142
#### Best practices
123-
- Test basic API functionality and keep the tests simple.
143+
- Test basic functionality and keep the tests simple.
144+
- Prefer test infrastructure over mocking.
124145
- If the tested endpoint makes database changes, verify that the changes were
125146
actually made.
126147
- Assert that output data is correct.
@@ -135,9 +156,11 @@ With these tests, we want to make sure our API contract is valid and the API
135156
returns the expected data. That means we write tests for the publically
136157
available endpoints.
137158

138-
Depending on the project setup, API tests can be covered with integration tests.
139-
For example, if the application only has public APIs and more devs than QAs, it
140-
might be a better option to add API testing in integration tests.
159+
> [!NOTE]
160+
> As mentioned in the Integration Tests section, API can be the entry point to the integration tests, meaning API tests are a subtype of integration tests. However, when we talk about API tests here, we are specifically referring to the public API contract tests, which don't have access to the internals of the application.
161+
162+
In the cases where API routes are covered extensively with integration tests, API tests might not be needed, leaving more time for QA to focus on E2E tests.
163+
However, in more complex architectures (e.g. integration tested microservices behind an API gateway), API tests can be very useful.
141164

142165
#### When to use
143166
- To make sure the API signature is valid.

0 commit comments

Comments
 (0)