Skip to content

Commit 499edbd

Browse files
committed
Add overarching Best Practices section, update other sections
1 parent 881efee commit 499edbd

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

recipes/automated-testing.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,48 @@
11
# Automated Testing
22

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+
346
## Types of Automated Tests
447

548
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.
3174
- Test function or class method with multiple input-output permutations
3275

3376
#### 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
3578

3679
#### Best practices
3780
- Unit tests should execute fast (<50ms)
@@ -72,13 +115,17 @@ time decoupling logic testing and endpoint testing.
72115

73116
#### When **not** to use
74117
- 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.
75119

76120
#### Best practices
77121
- Test basic API functionality and keep the tests simple.
78122
- If the tested endpoint makes database changes, verify that the changes were
79123
actually made.
124+
- Assert that output data is correct.
80125

81126
#### 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.
82129

83130
### API Tests
84131

@@ -127,6 +174,8 @@ level (integration or unit tests).
127174
#### Best practices
128175
- Performance is key in these tests. We want to run tests as often as possible
129176
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.
130179

131180
#### Antipatterns
132181

@@ -140,15 +189,29 @@ They are typically used to stress test the infrastructure and measure the throug
140189
of the application. They can expose bottlenecks and identify endpoints that need
141190
optimization.
142191

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+
143197
#### When to use
144198
- To stress test infrastructure.
145199
- To measure how increased traffic affects load speeds and overall app performance.
146200

147201
#### When **not** to use
202+
- To test if the application works according to specs.
203+
- To test a specific user scenario.
148204

149205
#### 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.
150210

151211
#### 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)
152215

153216

154217
### Visual Tests
@@ -161,9 +224,16 @@ easily indicate a change in the app. The downside is that they're not very preci
161224
and the engineer needs to spend some time to determine the cause of the error.
162225

163226
#### 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.
164230

165231
#### When **not** to use
232+
- To test a specific feature or business logic.
233+
- To test a specific user scenario.
166234

167235
#### 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.
168238

169239
#### Antipatterns

0 commit comments

Comments
 (0)