How strict is the rule not underlined in TEST () names?

The Google Test document says:

TEST() arguments go from general to concrete. The first argument is the name of the test case, and the second argument is the test name in the test case. Both names must be valid C ++ identifiers, and they must not contain an underscore ( _ ) .

This surprises me, because I usually call tests underscores (instead of CamelCase), for example:

 TEST(foo_test, should_fail_if_either_arg_negative) { 

Tests seem to be going well.

My question is: how strict is this non-underline rule? What can happen if I break it?

+5
source share
2 answers

From one Vlad Losev (see this discussion ), when he actually worked at Google on a test product:

This restriction has been introduced to provide us some flexibility in our tests. One fine day, we can decide to change the implementation and rely on this assumption. [Then] a user code that does not match him may end up broken. Therefore, please try to save test case names without underscores.

So, even if you take a standards-based approach where rules are indicated by must and shall symbols, while recommendations should be indicated, you should follow the recommendations above to be more confident that your code will not be broken in the future.

Later in this discussion, it was also clear that some arguments with underscores can lead to names beginning with an underscore or containing two or more consecutive underscores, both of which are technically β€œinvalid” in the user code (they are reserved for implementation).

+8
source

Google Test answers this question in the FAQ . Shortly speaking,

  • Test names beginning or ending with _ can easily generate invalid identifiers.
  • In most cases, test names that have _ in the middle are perfect. but

     TEST(Time, Flies_Like_An_Arrow) { ... } TEST(Time_Flies, Like_An_Arrow) { ... } 

    will generate the same names.

For simplicity, the rule is set more restrained than necessary, and it also gives Google Test some room if its implementation should change in the future.

So in conclusion:

If you break the rule, there can be no consequences immediately, but your test may (just might) break with the new compiler (or with the new version of the compiler you use) or with the new version of Google Test. Therefore, it is better to follow the rule.

+4
source

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


All Articles