✍️ Write tests. Not too many. Mostly integration.
This is an impromptu post, as I got one-upped by the lack of proper testing yesterday.
“If you don’t have tests, how do you know your code is doing the right thing and doing the thing right?”
You might have heard this familiar exchange:
A: That functionality you implemented, you have tested it?
B: No, that’s useless! It’s trivial!
A: …
B: …
A: … doesn’t work …
B: ????
What is trivial? Trivial for who? For how long? These are the questions we need to ask ourselves.
Why tests are important?
- Less debug time
- Code proven to meet requirements
- Safety Net
- Documentation! (I can't emphasize this enough!)
- Reduce chances of bugs in new / existing features
- Improve design
- Reduce fear (Afraid to touch code if we don’t know the ins and outs)
The Three Laws of TDD
Uncle Bob describes them as:
- You are not allowed to write any production code unless you have written a failing unit test.
- You are not allowed to write any more of a unit test than is sufficient to fail; and not compiling is failing.
- You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
Red Green Refactor - Building better software one test at a time.
P.S. Don't worry; I'm not going to judge you if you don't use it! 😏
^ It's very easy to talk about TDD and suggesting someone else to use it. However, in practice, it doesn't scale well (or rather, hasn't scaled well for me).
I've now shifted to writing integration tests which cover your most important flows and run in your CI pipeline.
Learnings
- It's very easy for documentation to go stale; it has a snowball effect. But tests remain up-to-date forever.
- I've written some of the best code in my life when the test coverage was close to 100%.
- The presence of tests can empower you to refactor anything, anytime!
- Good intentions don’t work, mechanisms do.
[Quiz] How many types of testing do you know about?
Functionality, Regression, Stress, Black/white box, Unit, Integration, Performance, Usability, Security, Alpha/beta, ...
[Meme] Leaving y'all with this:
https://twitter.com/kentcdodds/status/628658648001048577
Until We Meet Again...
🖖 swap