BellaCode

by Geoff Cox

Software Craftsman. Guitarist. Folk Hero.

Name your tests with the When/Then pattern

The When/Then naming pattern captures the condition and expected result of the test.

Pattern

WhenActionConditionThenExpectedResult

Examples

Unit testing a Name property:

public void WhenNameSetThenValueSet() {}

public void WhenNameSetToDifferentValueThenPropertyChangedRaised() {}

public void WhenNameSetToSameValueThenPropertyChangedNotRaised() {}

public void WhenNameSetToNullThenExceptionThrown() {}

public void WhenNameSetToEmptyThenValueSet() {}

Unit testing a bank account transfer method:

public void WhenTransferCalledWithAmountGreaterThanMaximumThenExceptionThrown() {}

public void WhenTranserCalledAndTransferCancelledThenAccountBalancesNotChanged() {}

Tips

Benefits