Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prefix for testing methods in Unit: "test" vs "should" [closed]

It is a common practice to prefix the tests method names in JUnit with "test". But in the last few years, some people changed this to the prefix "should".

If I want to test the customer creation in a database, I would normally name the method "testCustomerCreation". However, some people would name "shouldCreateCustomer".

This is a lot of personal taste when I am the only person in the project or when everyone else in the project agrees with me. But when/where this is not the case, some divergences or inconsistent mixes starts to shows up.

I readed somewhere an article of a guy that named his methods like "testShouldCreateCustomer", and for this reason he decided to drop the "test" prefix. But in fact he wasn't prefixing with "test", he was using "testShould" and changed to "should". Obviously, this did not convinced me.

I am personally strongly inclined to stick to "test" prefix because the methods names normally starts with verbs in the infinitive form ("get", "set", "add", "remove", "clear", "send", "receive", "open", "close", "read", "write", "create", "list", "pop", "print", etc, so is "test"). So, prefixing a method name with "should" makes it sound really very strange for me, looks wrong.

So, what is the real good reason to use "should" instead of "test"? What are the great advantages and disadvantages?

like image 955
Victor Stafusa Avatar asked Dec 20 '10 17:12

Victor Stafusa


1 Answers

The 'Should' convention is aligned with the behaviour driven development style of testing.

I personally really prefer to write tests in this style, as it encourages you to write tests that read as specifications, and are more aligned with the behaviour of the class or system that you are testing.

Where possible, I sometimes go one step further and give the test class even more context using it's name:

class ANewlyCreatedAccount {
  shouldHaveAZeroBalance() {}
  shouldCalculateItsOwnInterest() {}
}

By naming your classes and thinking about them in this specification style, this can give you a lot of guidance on which tests to write, and in which order you should write the tests and make them green.

Yes, 'should' vs 'test' is just a prefix and it's important to be consistent, but this question is also about the style and mindset of how you test your code and choose which tests to write. BDD has a ton of value, so I suggest reading further and give this a try.

like image 197
Benjamin Wootton Avatar answered Nov 25 '22 08:11

Benjamin Wootton