Quality Testing

Method Names

Creating a good name can be a difficult task at first, but it can pay dividends in the long run. The larger a codebase gets, the more complex it gets, or the more members of a team that are working on it – the more important having clear, concise method names become.

Methods should be named by the intention it serves. They should serve a single purpose and do a single thing. Methods should always start with a verb because it should be doing something. Methods should not accept a single boolean to determine what it does. That would be better served as 2 (or even 3!) methods. Having a boolean inside of a method name is like saying doSomeTask(false). What? That isn’t clear. doSomeTask and doSomeOtherTask makes more sense.

Of course, good practices for naming methods are also applicable when talking about the names of test methods as well. Tests should be named according to what the user is trying to accomplish. When you see a failed test title, you should have a good idea what the test was trying to accomplish. A test method with a small scope and a clear, distinct name allow anyone to quickly and easily identify what it is doing.

thisShouldDoThat is usually a good approach to the name. If you’re having trouble deciding on how to name your test, think to yourself

Is this test trying to verify too much, and should it be broken up?

Is this a full test, or should this be part of another test?

A good indicator of a bad test name is if it includes any of the following words:

    check
    verify
    test
    assert
    correct
    right
    good
    okay

“Check”, “verify”, “test”, “expect”, and “assert” are bad words for a test because it is a test. If you’re not checking/verifying/testing/expecting/asserting on something, then it wouldn't be a test. It’s redundant and unnecessary.

“Correct”, “right”, or “good” are bad words because somebody unfamiliar with a feature looking to a failure doesn’t know what they mean. You should be more specific as to what criteria makes it correct/right/good.

If a test is named eventStatusIsCorrect, how does someone who hasn’t worked on the feature know what correct is? eventStatusIsNotNull is an improvement. But again, we can go further. What happens that makes the event status not null? eventStatusIsNotNull_WhenStatusButtonIsClicked is a good test name. It says what is expected and what action takes place to make that expectation.

This is a carefully crafted list of bad test names, and generally bad practices that you can look at as reference of things not to do.

Clean Code also does a great dive into this topic as well.