Skip to content

Commit 881efee

Browse files
committed
Split integration and API tests
1 parent 6717639 commit 881efee

File tree

1 file changed

+90
-6
lines changed

1 file changed

+90
-6
lines changed

recipes/automated-testing.md

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
## Types of Automated Tests
44

5-
There are different approaches to testing, and depending on the level of the
6-
entry point, we can split tests into following categories.
5+
There are different approaches to testing, and depending on boundaries of the
6+
test, we can split them into following categories:
77

88
- **Unit Tests**
99
- **Integration Tests**
10+
- **API Tests**
1011
- **E2E Tests**
1112
- **Load/Performance Tests**
1213
- **Visual Tests**
@@ -25,12 +26,26 @@ Unit testing is recommended for functions that contain a lot of logic and/or bra
2526
It is convenient to test a specific function at the lowest level so if the logic
2627
changes, we can make minimal changes to the test suite and/or mocked data.
2728

29+
#### When to use
30+
- Test a unit that implements the business logic, that's isolated from side effects such as database interaction or HTTP request processing
31+
- Test function or class method with multiple input-output permutations
2832

29-
### Integration Tests (API Tests)
33+
#### When **not** to use
34+
- To test unit that integrates different application layers, such as persistence layer (database) or HTTP layer (see "Integration Tests")
3035

31-
This is the broadest test category. With these tests, we want to make sure our
32-
API contract is valid and the API returns the expected data. That means we write
33-
tests for the publically available endpoints.
36+
#### Best practices
37+
- Unit tests should execute fast (<50ms)
38+
- Use mocks and stubs through dependency injection (method or constructor injection)
39+
40+
#### Antipatterns
41+
- Mocking infrastructure parts such as database I/O - instead, revert the control by using the `AppService`, `Command` or `Query` to integrate unit implementing business logic with the infrastructure layer of the application
42+
- Monkey-patching dependencies used by the unit - instead, pass the dependencies through the constructor or method, so that you can pass the mocks or stubs in the test
43+
44+
45+
### Integration Tests
46+
47+
With these tests, we test the application API endpoints and assert that they are
48+
actually working as expected.
3449

3550
**TODO**: do we want to add that we should run full backend for these type of tests?
3651

@@ -43,12 +58,51 @@ can access this endpoint
4358
contains correct data
4459
- **failure** - if we send incorrect data, the endpoint should handle the exception
4560
and return appropriate error status
61+
- **successful change** - successful request should make the appropriate change
4662

4763
If the endpoint contains a lot of logic where we need to mock a lot of different
4864
inputs, it might be a good idea to cover that logic with unit tests. Unit tests
4965
will require less overhead and will provide better performance while at the same
5066
time decoupling logic testing and endpoint testing.
5167

68+
#### When to use
69+
- To verify the API endpoint performs authentication and authorization.
70+
- To verify user permissions for that endpoint.
71+
- To verify that invalid input is correctly handled.
72+
73+
#### When **not** to use
74+
- For testing of specific function logic. We should use unit tests for those.
75+
76+
#### Best practices
77+
- Test basic API functionality and keep the tests simple.
78+
- If the tested endpoint makes database changes, verify that the changes were
79+
actually made.
80+
81+
#### Antipatterns
82+
83+
### API Tests
84+
85+
With these tests, we want to make sure our API contract is valid and the API
86+
returns the expected data. That means we write tests for the publically
87+
available endpoints.
88+
89+
Depending on the project setup, API tests can be covered with integration tests.
90+
For example, if the application only has public APIs and more devs than QAs, it
91+
might be a better option to add API testing in integration tests.
92+
93+
#### When to use
94+
- To make sure the API signature is valid.
95+
96+
#### When **not** to use
97+
- To test application logic.
98+
99+
#### Best practices
100+
- Write these tests with the tools which allow us to reuse the tests to write
101+
performance tests (K6).
102+
103+
#### Antipatterns
104+
105+
52106
### E2E Tests
53107

54108
These tests are executed within a browser environment (Playwright, Selenium, etc.).
@@ -64,6 +118,18 @@ These tests should not cover all of the use cases because they are the slowest t
64118
run. If we need to test edge cases, we should try to implement those at a lower
65119
level (integration or unit tests).
66120

121+
#### When to use
122+
- Test user interaction with the application UI.
123+
124+
#### When **not** to use
125+
- For data validation.
126+
127+
#### Best practices
128+
- Performance is key in these tests. We want to run tests as often as possible
129+
and good performance will allow that.
130+
131+
#### Antipatterns
132+
67133
### Performance Tests
68134

69135
These types of tests will reproduce a typical user scenario and then simulate a
@@ -74,6 +140,16 @@ They are typically used to stress test the infrastructure and measure the throug
74140
of the application. They can expose bottlenecks and identify endpoints that need
75141
optimization.
76142

143+
#### When to use
144+
- To stress test infrastructure.
145+
- To measure how increased traffic affects load speeds and overall app performance.
146+
147+
#### When **not** to use
148+
149+
#### Best practices
150+
151+
#### Antipatterns
152+
77153

78154
### Visual Tests
79155

@@ -83,3 +159,11 @@ and then compares the future screenshots with the reference screenshot.
83159
These types of tests will cover a lot of ground with the least effort and can
84160
easily indicate a change in the app. The downside is that they're not very precise
85161
and the engineer needs to spend some time to determine the cause of the error.
162+
163+
#### When to use
164+
165+
#### When **not** to use
166+
167+
#### Best practices
168+
169+
#### Antipatterns

0 commit comments

Comments
 (0)