One Secret To Writing Understandable Unit Tests

Here’s a quick secret to writing understandable unit tests.

 

Name your object under test, the lower case letter o.

 

As in object.

 

As in

Foo o = new Foo();

 

The value of this approach is that in any test file, it’s very easy to determine what a particular test is trying to accomplish.

“What test class am I in?  The file name is Foo_Test.  In this case, o must be Foo”

 

It also reinforces the idea of one test class per application class.

 

Either way, when you come back to look at your code, you’ll be surprised at how easy it is to figure out what your intent was.

2 Responses to “One Secret To Writing Understandable Unit Tests”

  1. Caglar Abbasoglu Says:

    Why not naming it objectUnderTest (or object_under_test)? It would be even easier for the ones that are not particularly familiar with your convention 🙂

  2. malcolm.b.anderson Says:

    I guess you could do that, but when I’m coding I don’t want to take the time to type objectUnderTest dot something.

    I just want to hit the o key and the period and go.

    What convention do you use to name your object under test?

Leave a Reply