Unit testing OO code
medium 50 minA unit test runs one class alone, with stand-ins for the things it talks to, and checks what it promises.
Watch lesson4:09
Where it shows up
The same pattern appears across everyday software.
unittest.mock
Part of Python's standard library. `create_autospec` gives a double the same methods and signatures as the real class, so wrong calls fail.
Mockito
The common mock library of Java. `when` sets answers, `verify` checks calls after the act, so it can serve as stub and as spy.
JUnit 5
The user guide shows test interfaces with default methods, which is how one contract suite runs against several implementations.
pytest fixtures
A test names what it needs as parameters and pytest passes it in. Dependency injection for tests, also with several parameters for one test.
gMock
`MOCK_METHOD` makes a mock class in C++. Expectations are written before the act.
Jest
Mock functions that record calls, and timer mocks that move time without waiting.
java.time.Clock
The documentation says that using a clock object in place of a static method can simplify testing, and offers fixed and offset clocks.
Stripe test mode
Documents card numbers that simulate success, refusal and other cases, so an adapter can be tested without real money.
Testcontainers
Starts a throwaway database or broker in a container for the time of a test, for contract and integration tests.
Hypothesis
Property based testing for Python: you describe the range of inputs and a statement, it searches for a counterexample.
PIT
Mutation testing for Java: it changes the code and reports which changes no test noticed.
Software Engineering at Google
The chapter on test doubles prefers real implementations, then fakes, and warns of overuse of stubs and interaction tests.