The biggest barrier to automated testing isn't learning how to write tests


The biggest barrier to automated testing isn't learning how to write tests.

The biggest barrier is understanding how to write testable code.

#tdd
A few guidelines I've found useful for unit testing:

1. Separate concerns.
2. Write pure functions.
3. Handle I/O and logic separately.
4. Keep functions small and single purpose.
5. Prefer plain functions over classes (in languages that allow it like JS).
Item #5 is most controversial. The idea is: Classes can lead to methods that rely upon instance variables instead of explicitly accepting the necessary data via arguments. The latter is easier to unit test and understand because the func is pure. It relies upon no outside state.

View original on X