The line 65 and 66 says:
The test case name and the test name should both be valid C++ identifiers.
And you should not use underscore (_) in the names.
Is it a strict rule that I must follow?
Hi Joey and Vlad,
Thank you very much for your reply.
Hi Vlad,
Do you mean that we shuold not use underscore in "test case name" but
we can use it in "test name"?
For example:
1. TEST(FooTest , FooMethod_InvalidArgument_WorksAsExpected) {...}
2. TEST(Foo_Test, FooMethod_InvalidArgument_WorksAsExpected) {...}
The item 1 is fully supported by GoogleTest,
However, we should not name test case like item 2.
Is that correct?
On the other hand, does it rule also apply to test fixture?
For example:
3. TEST_F(CFooTest , FooMethod_InvalidArgument_WorksAsExpected) {...}
4. TEST_F(C_Foo_Test, FooMethod_InvalidArgument_WorksAsExpected) {...}
The item 3 is fully supported by GoogleTest,
However, we should not name test case like item 4.
Is that correct?
Underscore (_) is special, as C++ reserves the following to be used by
the compiler and the standard library:
1. any identifier that starts with an _ followed by an upper-case letter, and
2. any identifier that containers two consecutive underscores (i.e.
__) *anywhere* in its name.
User code is prohibited from using such identifiers.
Now let's look at what this means for TEST and TEST_F.
Currently TEST(TestCaseName, TestName) generates a class named
TestCaseName_TestName_Test. What happens if TestCaseName or TestName
contains _?
1. If TestCaseName starts with an _ followed by an upper-case letter
(say, _Foo), we end up with _Foo_TestName_Test, which is reserved and
thus invalid.
2. If TestCaseName ends with an _ (say, Foo_), we get
Foo__TestName_Test, which is invalid.
3. If TestName starts with an _ (say, _Bar), we get
TestCaseName__TestName_Test, which is invalid.
4. If TestName ends with an _ (say, Bar_), we get
TestCaseName_TestName__Test, which is invalid.
So clearly TestCaseName and TestName cannot start or end with _
(Actually, TestCaseName can start with _ -- as long as the _ isn't
followed by an upper-case letter. But that's getting complicated. So
for simplicity we just say that it cannot start with _.).
It may seem fine for TestCaseName and TestName to contain _ in the
middle. However, consider this:
TEST(Time, Flies_Like_An_Arrow) { ... }
TEST(Time_Flies, Like_An_Arrow) { ... }
Now, the two TESTs will both generate the same class
(Time_Files_Like_An_Arrow_Test). That's not good.
So for simplicity, we just ask the users to avoid _ in TestCaseName
and TestName. The rule is more constraining than necessary, but it's
simple and easy to remember. It also gives Google Test some wiggle
room in case its implementation needs to change in the future.
If you violate the rule, there may not be immediately consequences,
but your test may (just may) break with a new compiler (or a new
version of the compiler you are using) or with a new version of Google
Test. Therefore it's best to follow the rule.
--
Zhanyong
--
Zhanyong
Hi Zhanyong, sorry for reviving an old thread but I need you to clarify something. You wrote that undescores are fine in test names but not in test case names.
But then you said (and the current gtest documentation states so) that both test names and test case names shouldn't contain underscores. So which one is it then?
--
---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Oh, wow, I'm sorry, it was Vlad Losev that wrote that, not you. He wrote in this thread that underscores are fine in test names but not in test case names. So I understand that what he wrote is not correct?
Second question, if I'm already using underscores in test case names in my project and they work fine do I really have to change all those names and remove the underscores? What's the risk of something breaking (with future google test releases, I assume)?
W dniu czwartek, 14 października 2010 05:18:50 UTC+2 użytkownik Tom napisał:Dear GoogleTest Group Member,
I have a question regarding test case naming in GoogleTest,
Recently, I start to read the sample of GoogleTest, and I found a interested thing in the following source:
http://code.google.com/p/googletest/source/browse/trunk/samples/sample1_unittest.cc
The line 65 and 66 says:
The test case name and the test name should both be valid C++ identifiers.
And you should not use underscore (_) in the names.
Is it a strict rule that I must follow?
I known underscore seems a key word in GoogleTest and it also used in disabled case via the key word DISABLED_
but I have had many test cases that contains underscore in function name and works well,
I am confusing with that, could you please give me some suggestion?
Thank you very much.
Best regards.
Tom Liu
--
---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Well, actually this just presents the syntax but still I don't think it should use underscores if they're discouraged from actual names.TEST_F(test_case_name, test_name) { ... test body ... }
Thank you, Zhanyong. Btw I was just reading docs/Primer.md and noticed that in their example code they're using underscores as well:Well, actually this just presents the syntax but still I don't think it should use underscores if they're discouraged from actual names.TEST_F(test_case_name, test_name) { ... test body ... }
W dniu czwartek, 14 października 2010 05:18:50 UTC+2 użytkownik Tom napisał:Dear GoogleTest Group Member,
I have a question regarding test case naming in GoogleTest,
Recently, I start to read the sample of GoogleTest, and I found a interested thing in the following source:
http://code.google.com/p/googletest/source/browse/trunk/samples/sample1_unittest.cc
The line 65 and 66 says:
The test case name and the test name should both be valid C++ identifiers.
And you should not use underscore (_) in the names.
Is it a strict rule that I must follow?
I known underscore seems a key word in GoogleTest and it also used in disabled case via the key word DISABLED_
but I have had many test cases that contains underscore in function name and works well,
I am confusing with that, could you please give me some suggestion?
Thank you very much.
Best regards.
Tom Liu
--
---
You received this message because you are subscribed to the Google Groups "Google C++ Testing Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googletestframe...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
TEST(X_Y, Z ){}
TEST(X, Y_Z ){} // <-- error: 'X_Y_Z_Test' : 'class' type redefinition
TEST( my_fancy_class_test, bla ){}