writing better tests - applying clean-code tdd at 99designs
DESCRIPTION
My talk for 99designs Tech Talk fridays on pragmatic approaches to improving testsTRANSCRIPT
WRITING BETTER TESTSAPPLYING CLEAN-CODE LEARNINGS
@lox / 99designs
WHY DO WE WRITE TESTS?
• To find bugs?
• To support future refactoring and change without
regression?
• To document the system for future generations?
• Nope. Although these are awesome side-effects.
WHY DO WE WRITE TESTS?
• We write tests as part of the process of designing
cohesive units of code.
• Another way, tests should prove a designed
behaviour, as you are designing it.
WHAT MAKES A GOOD TEST?
• Orthogonality: Test one thing
• If you can’t test one class, test a few classes that
collaborate closely
• If you can’t do that, test a cohesive system as a
whole
• Mock out dependencies. If there are lots, consider
the design.
WHAT MAKES A GOOD TEST?
• Readability!
• Short, readable tests
• Refactor wiring into test-specific DSL’s
• Favour code over comments
WHAT MAKES A GOOD TEST?
• Carefully chosen names
• Test failures should clearly show what failed
• testClientThrowsExceptionOnFailedResponse 👍
• testClientException 👎
• Test methods should capture subject (Client),
scenario (Failed Response) and result (Throws
Exception).
GUIDELINES
• MUST have a single concept per test
• MUST be short and easy to read
• MUST test behaviour, not configuration or internals
• SHOULD follow a (build .. operate .. check) structure
• SHOULD have one assert per test
• Consider this a testing framework for your tests
EXAMPLE TIME!
QUESTIONS
• What does this test actually test?
• How does it fare on our guidelines?
• What could be improved about the test?