Preface |
|
xiv | |
Acknowledgments |
|
xv | |
About This Book |
|
xvi | |
About The Author |
|
xix | |
About The Cover Illustration |
|
xx | |
Part 1 The Bigger Picture |
|
1 | (64) |
|
1 The goal of unit testing |
|
|
3 | (17) |
|
1.1 The current state of unit testing |
|
|
4 | (1) |
|
1.2 The goal of unit testing |
|
|
5 | (3) |
|
What makes a good or bad test? |
|
|
7 | (1) |
|
1.3 Using coverage metrics to measure test suite quality |
|
|
8 | (7) |
|
Understanding the code coverage metric |
|
|
9 | (1) |
|
Understanding the branch coverage metric |
|
|
10 | (2) |
|
Problems with coverage metrics |
|
|
12 | (3) |
|
Aiming at a particular coverage number |
|
|
15 | (1) |
|
1.4 What makes a successful test suite? |
|
|
15 | (2) |
|
It's integrated into the development cycle |
|
|
16 | (1) |
|
It targets only the most important parts of your code base |
|
|
16 | (1) |
|
It provides maximum value with minimum maintenance costs |
|
|
17 | (1) |
|
1.5 What you will learn in this book |
|
|
17 | (3) |
|
|
20 | (21) |
|
2.1 The definition of "unit test" |
|
|
21 | (9) |
|
The isolation issue: The London take |
|
|
21 | (6) |
|
The isolation issue: The classical take |
|
|
27 | (3) |
|
2.2 The classical and London schools of unit testing |
|
|
30 | (4) |
|
How the classical and London schools handle dependencies |
|
|
30 | (4) |
|
2.3 Contrasting the classical and London schools of unit testing |
|
|
34 | (3) |
|
Unit testing one class at a time |
|
|
34 | (1) |
|
Unit testing a large graph of interconnected classes |
|
|
35 | (1) |
|
Revealing the precise bug location |
|
|
36 | (1) |
|
Other differences between the classical and London schools |
|
|
36 | (1) |
|
2.4 Integration tests in the two schools |
|
|
37 | (4) |
|
End-to-end tests are a subset of integration tests |
|
|
38 | (3) |
|
3 The anatomy of a unit test |
|
|
41 | (24) |
|
3.1 How to structure a unit test |
|
|
42 | (7) |
|
|
42 | (1) |
|
Avoid multiple arrange, act, and assert sections |
|
|
43 | (1) |
|
Avoid if statements in tests |
|
|
44 | (1) |
|
How large should each section be? |
|
|
45 | (2) |
|
How many assertions should the assert section hold? |
|
|
47 | (1) |
|
What about the teardown phase? |
|
|
47 | (1) |
|
Differentiating the system under test |
|
|
47 | (1) |
|
Dropping the arrange, act, and assert comments from tests |
|
|
48 | (1) |
|
3.2 Exploring the xUnit testing framework |
|
|
49 | (1) |
|
3.3 Reusing test fixtures between tests |
|
|
50 | (4) |
|
High coupling between tests is an anti-pattern |
|
|
52 | (1) |
|
The use of constructors in tests diminishes test readability |
|
|
52 | (1) |
|
A better way to reuse test fixtures |
|
|
52 | (2) |
|
|
54 | (4) |
|
Unit test naming guidelines |
|
|
56 | (1) |
|
Example: Renaming a test toward the guidelines |
|
|
56 | (2) |
|
3.5 Refactoring to parameterized tests |
|
|
58 | (4) |
|
Generating data for parameterized tests |
|
|
60 | (2) |
|
3.6 Using an assertion library to further improve test readability |
|
|
62 | (3) |
Part 2 Making Your Tests Work For You |
|
65 | (118) |
|
4 The four pillars of a good unit test |
|
|
67 | (25) |
|
4.1 Diving into the four pillars of a good unit test |
|
|
68 | (8) |
|
The first pillar: Protection against regressions |
|
|
68 | (1) |
|
The second pillar: Resistance to refactoring |
|
|
69 | (2) |
|
What causes false positives? |
|
|
71 | (3) |
|
Aim at the end result instead of implementation details |
|
|
74 | (2) |
|
4.2 The intrinsic connection between the first two attributes |
|
|
76 | (3) |
|
|
76 | (2) |
|
The importance of false positives and false negatives: The dynamics |
|
|
78 | (1) |
|
4.3 The third and fourth pillars: Fast feedback and maintainability |
|
|
79 | (1) |
|
4.4 In search of an ideal test |
|
|
80 | (7) |
|
Is it possible to create an ideal test? |
|
|
81 | (1) |
|
Extreme case #1: End-to-end tests |
|
|
81 | (1) |
|
Extreme case #2: Trivial tests |
|
|
82 | (1) |
|
Extreme case #3: Brittle tests |
|
|
83 | (1) |
|
In search of an ideal test: The results |
|
|
84 | (3) |
|
4.5 Exploring well-known test automation concepts |
|
|
87 | (5) |
|
Breaking down the Test Pyramid |
|
|
87 | (2) |
|
Choosing between black-box and white-box testing |
|
|
89 | (3) |
|
5 Mocks and test fragility |
|
|
92 | (27) |
|
5.1 Differentiating mocks from stubs |
|
|
93 | (6) |
|
The types of test doubles |
|
|
93 | (1) |
|
Mock (the tool) vs. mock (the test double) |
|
|
94 | (2) |
|
Don't assert interactions with stubs |
|
|
96 | (1) |
|
Using mocks and stubs together |
|
|
97 | (1) |
|
How mocks and stubs relate to commands and queries |
|
|
97 | (2) |
|
5.2 Observable behavior vs. implementation details |
|
|
99 | (7) |
|
Observable behavior is not the same as a public API |
|
|
99 | (1) |
|
Leaking implementation details: An example with an operation |
|
|
100 | (3) |
|
Well-designed API and encapsulation |
|
|
103 | (1) |
|
Leaking implementation details: An example with state |
|
|
104 | (2) |
|
5.3 The relationship between mocks and test fragility |
|
|
106 | (8) |
|
Defining hexagonal architecture |
|
|
106 | (4) |
|
Intra-system vs. intersystem communications |
|
|
110 | (1) |
|
Intra-system vs. inter-system communications: An example |
|
|
111 | (3) |
|
5.4 The classical vs. London schools of unit testing, revisited |
|
|
114 | (5) |
|
Not all out-of-process dependencies should be mocked out |
|
|
115 | (1) |
|
Using mocks to verify behavior |
|
|
116 | (3) |
|
|
119 | (32) |
|
6.1 The three styles of unit testing |
|
|
120 | (3) |
|
Defining the output-based style |
|
|
120 | (1) |
|
Defining the state-based style |
|
|
121 | (1) |
|
Defining the communication-based style |
|
|
122 | (1) |
|
6.2 Comparing the three styles of unit testing |
|
|
123 | (5) |
|
Comparing the styles using the metrics of protection against regressions and feedback speed |
|
|
124 | (1) |
|
Comparing the styles using the metric of resistance to refactoring |
|
|
124 | (1) |
|
Comparing the styles using the metric of maintainability |
|
|
125 | (2) |
|
Comparing the styles: The results |
|
|
127 | (1) |
|
6.3 Understanding functional architecture |
|
|
128 | (7) |
|
What is functional programming? |
|
|
128 | (4) |
|
What is functional architecture? |
|
|
132 | (1) |
|
Comparing functional and hexagonal architectures |
|
|
133 | (2) |
|
6.4 Transitioning to functional architecture and output-based testing |
|
|
135 | (11) |
|
Introducing an audit system |
|
|
135 | (2) |
|
Using mocks to decouple tests from the filesystem |
|
|
137 | (3) |
|
Refactoring toward functional architecture |
|
|
140 | (6) |
|
Looking forward to further developments |
|
|
146 | (1) |
|
6.5 Understanding the drawbacks of functional architecture |
|
|
146 | (5) |
|
Applicability of functional architecture |
|
|
147 | (1) |
|
|
148 | (1) |
|
Increase in the code base size |
|
|
149 | (2) |
|
7 Refactoring toward valuable unit tests |
|
|
151 | (32) |
|
7.1 Identifying the code to refactor |
|
|
152 | (6) |
|
|
152 | (3) |
|
Using the Humble Object pattern to split overcomplicated code |
|
|
155 | (3) |
|
7.2 Refactoring toward valuable unit tests |
|
|
158 | (9) |
|
Introducing a customer management system |
|
|
158 | (2) |
|
Take 1: Making implicit dependencies explicit |
|
|
160 | (1) |
|
Take 2: Introducing an application services layer |
|
|
160 | (3) |
|
Take 3: Removing complexity from the application service |
|
|
163 | (1) |
|
Take 4: Introducing a new Company class |
|
|
164 | (3) |
|
7.3 Analysis of optimal unit test coverage |
|
|
167 | (2) |
|
Testing the domain layer and utility code |
|
|
167 | (1) |
|
Testing the code from the other three quadrants |
|
|
168 | (1) |
|
Should you test preconditions? |
|
|
169 | (1) |
|
7.4 Handling conditional logic in controllers |
|
|
169 | (9) |
|
Using the CanExecute/Execute pattern |
|
|
172 | (3) |
|
Using domain events to track changes in the domain model |
|
|
175 | (3) |
|
|
178 | (5) |
Part 3 Integration Testing |
|
183 | (74) |
|
8 Why integration testing? |
|
|
185 | (31) |
|
8.1 What is an integration test? |
|
|
186 | (4) |
|
The role of integration tests |
|
|
186 | (1) |
|
The Test Pyramid revisited |
|
|
187 | (1) |
|
Integration testing vs. failing fast |
|
|
188 | (2) |
|
8.2 Which out-of-process dependencies to test directly |
|
|
190 | (3) |
|
The two types of out-of-process dependencies |
|
|
190 | (1) |
|
Working with both managed and unmanaged dependencies |
|
|
191 | (1) |
|
What if you can't use a real database in integration tests? |
|
|
192 | (1) |
|
8.3 Integration testing: An example |
|
|
193 | (4) |
|
|
194 | (1) |
|
Categorizing the database and the message bus |
|
|
195 | (1) |
|
What about end-to-end testing? |
|
|
195 | (1) |
|
Integration testing: The first try |
|
|
196 | (1) |
|
8.4 Using interfaces to abstract dependencies |
|
|
197 | (3) |
|
Interfaces and loose coupling |
|
|
198 | (1) |
|
Why use interfaces for out-of-process dependencies? |
|
|
199 | (1) |
|
Using interfaces for in-process dependencies |
|
|
199 | (1) |
|
8.5 Integration testing best practices |
|
|
200 | (5) |
|
Making domain model boundaries explicit |
|
|
200 | (1) |
|
Reducing the number of layers |
|
|
200 | (2) |
|
Eliminating circular dependencies |
|
|
202 | (2) |
|
Using multiple act sections in a test |
|
|
204 | (1) |
|
8.6 How to test logging functionality |
|
|
205 | (8) |
|
|
205 | (2) |
|
How should you test logging? |
|
|
207 | (5) |
|
How much logging is enough? |
|
|
212 | (1) |
|
How do you pass around logger instances? |
|
|
212 | (1) |
|
|
213 | (3) |
|
|
216 | (13) |
|
9.1 Maximizing mocks' value |
|
|
217 | (8) |
|
Verifying interactions at the system edges |
|
|
219 | (3) |
|
Replacing mocks with spies |
|
|
222 | (2) |
|
What about IDomainLogger? |
|
|
224 | (1) |
|
9.2 Mocking best practices |
|
|
225 | (4) |
|
Mocks are for integration tests only |
|
|
225 | (1) |
|
Not just one mock per test |
|
|
225 | (1) |
|
Verifying the number of calls |
|
|
226 | (1) |
|
Only mock types that you own |
|
|
227 | (2) |
|
|
229 | (28) |
|
10.1 Prerequisites for testing the database |
|
|
230 | (4) |
|
Keeping the database in the source control system |
|
|
230 | (1) |
|
Reference data is part of the database schema |
|
|
231 | (1) |
|
Separate instance for every developer |
|
|
232 | (1) |
|
State-based vs. migration-based database delivery |
|
|
232 | (2) |
|
10.2 Database transaction management |
|
|
234 | (9) |
|
Managing database transactions in production code |
|
|
235 | (7) |
|
Managing database transactions in integration tests |
|
|
242 | (1) |
|
10.3 Test data life cycle |
|
|
243 | (3) |
|
Parallel vs. sequential test execution |
|
|
243 | (1) |
|
Clearing data between test runs |
|
|
244 | (2) |
|
Avoid in-memory databases |
|
|
246 | (1) |
|
10.4 Reusing code in test sections |
|
|
246 | (6) |
|
Reusing code in arrange sections |
|
|
246 | (3) |
|
Reusing code in act sections |
|
|
249 | (1) |
|
Reusing code in assert sections |
|
|
250 | (1) |
|
Does the test create too many database transactions? |
|
|
251 | (1) |
|
10.5 Common database testing questions |
|
|
252 | (2) |
|
|
252 | (1) |
|
Should you test repositories? |
|
|
253 | (1) |
|
|
254 | (3) |
Part 3 Unit Testing Anti-Patterns |
|
257 | (18) |
|
11 Unit testing anti-patterns |
|
|
259 | (16) |
|
11.1 Unit testing private methods |
|
|
260 | (3) |
|
Private methods and test fragility |
|
|
260 | (1) |
|
Private methods and insufficient coverage |
|
|
260 | (1) |
|
When testing private methods is acceptable |
|
|
261 | (2) |
|
11.2 Exposing private state |
|
|
263 | (1) |
|
11.3 Leaking domain knowledge to tests |
|
|
264 | (2) |
|
|
266 | (2) |
|
11.5 Mocking concrete classes |
|
|
268 | (3) |
|
|
271 | (2) |
|
Time as an ambient context |
|
|
271 | (1) |
|
Time as an explicit dependency |
|
|
272 | (1) |
|
|
273 | (2) |
Index |
|
275 | |