Prefix for testing methods in Unit: "test" vs "should"

A common practice is to prefix test method names in JUnit with "test". But in the past few years, some people have changed this to the "must" prefix.

If I want to test client creation in a database, I would usually call the testCustomerCreation method. However, some people will call "shouldCreateCustomer".

It is a lot of personal taste when I am the only person in the project or when all other project participants agree with me. But when / where it is not, some discrepancies or inconsistent mixes appear.

I read an article somewhere in a guy who called his methods, such as "testShouldCreateCustomer", and for this reason decided to abandon the prefix "test". But actually it is not a prefix of "test", it used "testShould" and changed to "should". Obviously, this did not convince me.

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

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

+8
java junit naming-conventions junit4
source share
5 answers

The 'Should' convention is aligned with a behavior-support test.

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

If possible, I sometimes take another step and give the test class even more context using its name:

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

By naming your classes and thinking of 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 tests and make them green.

Yes, "should" vs "test" is only a prefix, and it is important to be consistent, but this question also concerns the style and thinking, how you test your code and choose which tests to write. BDD has a ton of value, so I suggest reading on and trying.

+18
source share

I would say that the prefix โ€œtestโ€ is simply a delay from the days of preliminary annotation when it was necessary. I would suggest that you simply use meaningful names for your test cases (and this may mean with or without "test").

I prefer to name the testing method so that it is clear what is being tested. i.e.

 checkNullParameter() runSimpleQuery() runQueryWithBadParam() 

All test cases are still in the test directory, and all actual tests are annotated, so the prefix "test" is redundant.

+5
source share

Consistency is more important than the correct name problem. If you have any questions about the project, the technical member responsible for the project should clearly state the coding rules so that such problems do not kill valuable project time.

+3
source share

In the original JUnit, test methods should have started test . Many frameworks for other languages โ€‹โ€‹have copied this convention. Despite the fact that this is not so in JUnit, and although other structures may be different, I think that most programmers are still familiar with the methods named, for example. testX as unit tests, so I think itโ€™s advisable to stick to the test convention for this reason.

+3
source share

I prefer the suffix test . Perhaps you may have a method with a should prefix in your project, for example. shouldBuy , and then your test will be called testShouldBuy , because shouldShouldBuy will look very strange.

I also use the MoreUnit Eclipse plugin, which will automatically create a test method with the test prefix when I Ctrl+U or go to the test method when I Ctrl+J (Although you can configure which prefix it uses.) If you do not agree with your name, automatic tools like MoreUnit will not be able to help you with testing.

+2
source share

Source: https://habr.com/ru/post/650315/


All Articles