autospec
A unittest.mock feature that builds a mock with exactly the same methods and signatures as a real class.
In more detail
Without it a mock accepts any call, including ones the real object would reject. With it, a wrong argument list fails the test. It cannot tell you what the real object returns, so an invented return value can still lie.
Example
gateway = create_autospec(Gateway, instance=True)Related terms
- unittest.mock — The mocking library in Python's standard library, which builds mock objects and records how they were called.
- function signature — The name, parameters and their order that define how a function must be called.
- over-mocking — Replacing so many collaborators with mocks that tests only check the script the test writer invented, not real behaviour.
Taught in
- Fakes, Stubs, Mocks And Spies — Automated Testing, TDD & Quality Engineering
- Why Over-Mocking Breeds False Positives — Automated Testing, TDD & Quality Engineering