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
+71-1Lines changed: 71 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,48 @@
1
1
# Automated Testing
2
2
3
+
## Testing best practices
4
+
5
+
Writing tests can be hard because there are a lot of things that can be tested.
6
+
7
+
Starting out can be overwhelming. But since writing tests is easy, we can write
8
+
a lot of them in no time. Don't do this. Do not focus on code coverage number as
9
+
it can lead to false sense of security. Remember that code with 100% test coverage
10
+
can still have bugs.
11
+
12
+
Focus on test quality and test performance. Make sure the test is not asserting
13
+
unimportant things. Make sure the test is as quick as possible. Quick tests will
14
+
be run often. Running tests often means more early bug detection which means less
15
+
production errors.
16
+
17
+
---
18
+
19
+
Deal with flaky tests immediately. Flaky tests ruin test suite confidence. A failed
20
+
test should raise alarm immediately. If the test suite contains flaky tests, disable
21
+
them and refactor as soon as possible.
22
+
23
+
---
24
+
25
+
Be careful with tests that alter database state. We want to be able to run tests
26
+
in parallel so do not write tests that depend on each other. Each test should be
27
+
independent of the test suite.
28
+
29
+
---
30
+
31
+
Test for behavior and not implementation. Rather focus on writing tests that
32
+
follow the business logic instead of programming logic. Avoid writing parts of
33
+
the function implementation in the actual test assertion. This will lead to tight
34
+
coupling of tests with internal implementation and the tests will have to be fixed
35
+
each time the logic changes.
36
+
37
+
---
38
+
39
+
Writing quality tests is hard and it's easy to fall into common pitfalls of testing
40
+
that the database update function actually updates the database. Start off simple
41
+
and as the application grows in complexity, it will be easier to determine what
42
+
should be tested more thoroughly. It is perfectly fine to have a small test suite
43
+
that covers the critical code and the essentials. Small suites will run faster
44
+
which means they will be run more often.
45
+
3
46
## Types of Automated Tests
4
47
5
48
There are different approaches to testing, and depending on boundaries of the
@@ -31,7 +74,7 @@ changes, we can make minimal changes to the test suite and/or mocked data.
31
74
- Test function or class method with multiple input-output permutations
32
75
33
76
#### When **not** to use
34
-
- To test unit that integrates different application layers, such as persistence layer (database) or HTTP layer (see "Integration Tests")
77
+
- To test unit that integrates different application layers, such as persistence layer (database) or HTTP layer (see "Integration Tests") or performs disk I/O or communicates with external system
35
78
36
79
#### Best practices
37
80
- Unit tests should execute fast (<50ms)
@@ -72,13 +115,17 @@ time decoupling logic testing and endpoint testing.
72
115
73
116
#### When **not** to use
74
117
- For testing of specific function logic. We should use unit tests for those.
118
+
- For testing third party services. We should assume they work as expected.
75
119
76
120
#### Best practices
77
121
- Test basic API functionality and keep the tests simple.
78
122
- If the tested endpoint makes database changes, verify that the changes were
79
123
actually made.
124
+
- Assert that output data is correct.
80
125
81
126
#### Antipatterns
127
+
- Aiming for code coverage percentage number. An app with 100% code coverage can
128
+
have bugs. Instead, focus on writing meaningful, quality tests.
82
129
83
130
### API Tests
84
131
@@ -127,6 +174,8 @@ level (integration or unit tests).
127
174
#### Best practices
128
175
- Performance is key in these tests. We want to run tests as often as possible
129
176
and good performance will allow that.
177
+
- Flaky tests should be immediately disabled and refactored. Flaky tests will
178
+
cause the team to ignore or bypass the tests and these should be dealt with immediately.
130
179
131
180
#### Antipatterns
132
181
@@ -140,15 +189,29 @@ They are typically used to stress test the infrastructure and measure the throug
140
189
of the application. They can expose bottlenecks and identify endpoints that need
141
190
optimization.
142
191
192
+
Performance tests are supposed to be run on actual production environment since
193
+
they test the performance of code **and** infrastructure. Keep in mind actual
194
+
users when running performance tests. Best approach is to spin up a production
195
+
clone and run tests against that environment.
196
+
143
197
#### When to use
144
198
- To stress test infrastructure.
145
199
- To measure how increased traffic affects load speeds and overall app performance.
146
200
147
201
#### When **not** to use
202
+
- To test if the application works according to specs.
203
+
- To test a specific user scenario.
148
204
149
205
#### Best practices
206
+
- These tests should mimic actual human user in terms of click frequency and page
207
+
navigation.
208
+
- There should be multiple tests that test different paths in the system, not a
209
+
single performance test.
150
210
151
211
#### Antipatterns
212
+
- Running these tests locally or on an environment that doesn't match production
213
+
in terms of infrastructure performance. (tests should be developed on a local
214
+
instance, but the actual measurements should be performed live)
152
215
153
216
154
217
### Visual Tests
@@ -161,9 +224,16 @@ easily indicate a change in the app. The downside is that they're not very preci
161
224
and the engineer needs to spend some time to determine the cause of the error.
162
225
163
226
#### When to use
227
+
- When we want to cover broad range of features.
228
+
- When we want to increase test coverage with least effort.
229
+
- When we want to make sure there are no changes in the UI.
164
230
165
231
#### When **not** to use
232
+
- To test a specific feature or business logic.
233
+
- To test a specific user scenario.
166
234
167
235
#### Best practices
236
+
- Have deterministic seeds so the UI always renders the same output.
237
+
- Add as many pages as possible but keep the tests simple.
0 commit comments