4 ms·
The frustrating thing about these testing discussions is that they so often boil down to extreme positions, such as: - only write integration tests - only wri
by Tainnor 3y ago
The frustrating thing about these testing discussions is that they so often boil down to extreme positions, such as:
- only write integration tests
- only write unit tests
- never use mocks
- mock all your dependencies
- always use fakes instead of mocks (or the other way around)
- always use factories instead of fixtures
- unit tests can only test a single class/method
- there can only be one assertion per test
and so on.
What gets me is how divorced from the pragmatics of our tests these discussions are. It's as if we don't apply the same nuance to our test code that we're comfortable applying to our main code base (namely, that different parts of our code may require different coding styles).
First and foremost I wish testing wasn't treated as such an afterthought both in education and in actual practice; many developers say "I don't check test cases in code review", but badly written tests are as much a maintenance burden as other bad code.
We should work towards an understanding of different techniques and styles in testing, just as we cultivate an understanding of architectural patterns. When having to write - or maybe refactor - tests, we should start with a set of leading questions: what is worth testing more, the logic of the component itself, or its interaction with other components? is the external component that I use regular and predictable enough that I can get away with a shallow fake or is it weird, stateful and complex, so that I should run the tests against a real instance (e.g. a test container)? how can I write the tests such that they're comprehensible and maintainable? how can I write them to be as fast as possible while still giving me the guarantees I need? if I have a set of tests that are particularly hard to work with, or are really bad at catching regressions, how can I fix that and make the tests better?
Yes, that may mean that more time has to be spent on tests. But the alternative is messy tests of questionable value which slow down progress and don't catch enough regressions.