writing better tests - applying clean-code tdd at 99designs

9
WRITING BETTER TESTS APPLYING CLEAN - CODE LEARNINGS @lox / 99designs

Upload: lachlandonald

Post on 03-Jul-2015

320 views

Category:

Engineering


0 download

DESCRIPTION

My talk for 99designs Tech Talk fridays on pragmatic approaches to improving tests

TRANSCRIPT

Page 1: Writing Better Tests - Applying Clean-Code TDD at 99designs

WRITING BETTER TESTSAPPLYING CLEAN-CODE LEARNINGS

@lox / 99designs

Page 2: Writing Better Tests - Applying Clean-Code TDD at 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.

Page 3: Writing Better Tests - Applying Clean-Code TDD at 99designs

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.

Page 4: Writing Better Tests - Applying Clean-Code TDD at 99designs

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.

Page 5: Writing Better Tests - Applying Clean-Code TDD at 99designs

WHAT MAKES A GOOD TEST?

• Readability!

• Short, readable tests

• Refactor wiring into test-specific DSL’s

• Favour code over comments

Page 6: Writing Better Tests - Applying Clean-Code TDD at 99designs

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).

Page 7: Writing Better Tests - Applying Clean-Code TDD at 99designs

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

Page 8: Writing Better Tests - Applying Clean-Code TDD at 99designs

EXAMPLE TIME!

Page 9: Writing Better Tests - Applying Clean-Code TDD at 99designs

QUESTIONS

• What does this test actually test?

• How does it fare on our guidelines?

• What could be improved about the test?