Test-Driven Development Patterns
Test n.
How do you test your software? Write an automated test.
Isolated Test
How should the running if tests affect each other? Not at all.
Test List
What should you test? Before you begin, write a list of all the tests you know you will have to write.
Test First
When should you write your tests? Before you write the code that is to be tested.
Assert First
When should you write the asserts? Try writing them first.
Test Data
What data do you use for test-first tests? Use data that makes the tests easy to read and follow.
Evident Data
How do you represent the intent of the data? Include expected and actual results in the test itself, and try to make their relationship apparent.
Red Bar Patterns
One Step Test
Which test should you pick next from the list? Pick a test that will teach you some thing and that you are confident you can implement.
Starter Test
Which test should you start with? Start by testing a variant of an operation that doesn't do anything.
Explanation Test
How do you spread the use of automated testing? Ask for and give explanations in terms of tests.
Learning Test
When do you write tests for externally produced software? Before the first time you are going to use a new facility in the package.
Another Test
How do you keep a technical discussion from straying off topic? When a tangential idea arises, add a test to the list and go back to the topic.
Regression Test
What's the first thing you do when a defect is reported? Write the smallest possible test that fails, and that once it runs, the defect will be repaired.
Break
What do you do when you feel tired or stuck? Take a break.
Do Over
What do you do when you are feeling lost? Throw away the code and start over.
Cheap Desk, Nice Chair
What physical setup should you use for test-driven development? Get a really nice chair, skimping on the rest of the furniture if necessary.
Testing Patterns
Child Test
How do you get a test case running that turns out to be too big? Write a smaller test case that represents the broken part of the bigger test case. Get the smaller test case running. Reintroduce the larger test case.
Mock Object
How do you test an object that relies on an expensive or complicated resource? Create a fake version of the resource that answers constants.
Self Shunt
How do you test that one object communicates correctly with another? Have the object under test communicate with the test case instead of with the object it expects.
Log String
How do you test that the sequence in which messages are called is correct? Keep a log in a string, and append to the string when a message is called.
Crash Test Dummy
How do you test error code that is unlikely to be invoked? Invoke it anyway with a special object that throws an exception instead of doing real work.
Broken Test
How do you leave a programming session when you're programming alone? Leave the last test broken.
Clean Check-in
How do you leave a programming session when you're programming in a team? Leave all the tests running.
Green Bar Patterns
Fake It ('Til You Make It)
What is your first implementation once you have a broken test? Return a constant. Once you have the test running, gradually transform the constant into an expression using variables.
Triangulate
How do you most conservatively drive abstraction with tests? Only abstract when you have two or more examples.
Obvious Implementation
How do you implement simple operations? Just implement them.
One to Many
How do you implement an operation that works with collections of objects? Implement it without the collections first, then make it work with collections.
xUnit Patterns
Assertion
How do you check that tests worked correctly? Write Boolean expressions that automate your judgment about whether the code worked.
Fixture
How do you create common objects needed by several tests? Convert the local variables in the tests into instance variables. Override setup() and initialize those variables.
External Fixture
How do you release external resources in the fixture? Override teardown() and release the resources.
Test Method
How do you represent a single test case? As a method (whose name begins with “test” by convention.)
Exception Test
How do you test for expected exceptions? Catch expected exceptions and ignore them, failing only if the exception isn’t thrown.
All Tests
How do you run all tests together? Make a suite of all the suites, one for each package and one aggregating the package tests for the whole application.
Design Patterns
Command
Value Object
Null Object
Template Method
Pluggable Object
Pluggable Selector
Factory Method
Imposter
Composite
Collecting Parameter
Singleton
How do you provide global variables in languages without global variables? Don’t. Your programs will thank you for taking the time to think about design instead.
Refactoring
Reconcile Differences
How do you unify two similar looking pieces of code? Gradually bring them closer. Unify them only when they are absolutely identical.
Isolate Change
How do you change one part of a multi-part method or object? First, isolate the part that has to change.
Migrate Data
How do you move from one representation? Temporarily duplicate the data.
Extract Method
How do you make a long, complicated method easier to read? Turn a small part of it into a separate method and call the new method.
Inline Method
How do you simplify control flows that have gotten to twisted or scattered? Replace a method invocation with the method itself.
Extract Interface
How do you introduce a second implementation of operations in Java? Create an interface containing the shared operations.
Move Method
How do you move a method to where it belongs? Add it to the class where it belongs. Then invoke it.
Method Object
How do you represent a complicated method that requires several parameters and local variables? Make an object out of the method.
Add Parameter
How do you add a parameter to a method? If the method is in an interface, add the parameter to the interface first. Use the compiler errors to tell you what other code you need to change.
Method Parameter to Constructor Parameter
How do you move a parameter from a method or methods to the constructor? Step by step.