Look for the right behavior.

Test for required behavior, not the specific implementation.

When creating tests, make sure you test for the system’s expected behavior and not merely validate a specific implementation. There are many different ways one can get to the correct behavior. Implementation details may change over time, invaliding tests in the future.

For example, sort functions require the comparator to be negative, zero, or positive. Developers frequently implement the function to return -1, 0, and +1. If you set your tests to look for -1, 0, +1, you are testing the implementation, not the required behavior. In this case, the required behavior may be that an array of data objects gets sorted in a certain way.

A later implementation may have the comparator return the difference between two attributes of an object. Although this is valid and would result in the correct behavior, the tests would fail because they are expecting -1, 0, and +1. A better test would have some unsorted sample data, run the sort, and compare the result against the expected sort order.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: