You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: recipes/automated-testing.md
+90-6Lines changed: 90 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,12 @@
2
2
3
3
## Types of Automated Tests
4
4
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:
7
7
8
8
-**Unit Tests**
9
9
-**Integration Tests**
10
+
-**API Tests**
10
11
-**E2E Tests**
11
12
-**Load/Performance Tests**
12
13
-**Visual Tests**
@@ -25,12 +26,26 @@ Unit testing is recommended for functions that contain a lot of logic and/or bra
25
26
It is convenient to test a specific function at the lowest level so if the logic
26
27
changes, we can make minimal changes to the test suite and/or mocked data.
27
28
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
28
32
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")
30
35
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.
34
49
35
50
**TODO**: do we want to add that we should run full backend for these type of tests?
36
51
@@ -43,12 +58,51 @@ can access this endpoint
43
58
contains correct data
44
59
-**failure** - if we send incorrect data, the endpoint should handle the exception
45
60
and return appropriate error status
61
+
-**successful change** - successful request should make the appropriate change
46
62
47
63
If the endpoint contains a lot of logic where we need to mock a lot of different
48
64
inputs, it might be a good idea to cover that logic with unit tests. Unit tests
49
65
will require less overhead and will provide better performance while at the same
50
66
time decoupling logic testing and endpoint testing.
51
67
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
+
52
106
### E2E Tests
53
107
54
108
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
64
118
run. If we need to test edge cases, we should try to implement those at a lower
65
119
level (integration or unit tests).
66
120
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
+
67
133
### Performance Tests
68
134
69
135
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
74
140
of the application. They can expose bottlenecks and identify endpoints that need
75
141
optimization.
76
142
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
+
77
153
78
154
### Visual Tests
79
155
@@ -83,3 +159,11 @@ and then compares the future screenshots with the reference screenshot.
83
159
These types of tests will cover a lot of ground with the least effort and can
84
160
easily indicate a change in the app. The downside is that they're not very precise
85
161
and the engineer needs to spend some time to determine the cause of the error.
0 commit comments