Namespace for unit tests?

4,514 views
Skip to first unread message

Achuith Bhandarkar

unread,
Mar 5, 2012, 3:44:22 PM3/5/12
to Chromium-dev
Should unit tests be in an anonymous namespace, the namespace of the class being tested, or no namespace at all?

My understanding was that unit tests should not be in a namespace so name-collisions are detected at link time, since gtest requires unique names. 

John Abd-El-Malek

unread,
Mar 5, 2012, 3:52:52 PM3/5/12
to ach...@google.com, Chromium-dev
annonymous namespace doesn't work as a general rule, because then all the FRIEND_TEST macros and "friend" statements in implementation classes won't work.

On Mon, Mar 5, 2012 at 12:44 PM, Achuith Bhandarkar <ach...@chromium.org> wrote:
Should unit tests be in an anonymous namespace, the namespace of the class being tested, or no namespace at all?

My understanding was that unit tests should not be in a namespace so name-collisions are detected at link time, since gtest requires unique names. 

--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev

William Chan (陈智昌)

unread,
Mar 5, 2012, 4:03:43 PM3/5/12
to jabde...@google.com, ach...@google.com, Chromium-dev
I don't think this is worth debating :)

FWIW, I advocate for anonymous namespaces when FRIEND_TEST is not involved. It just feels more hygenic to me, and I suspect it'd help our link times (not sure about this though). If gtest is sane at all, then I suspect it'd fail fast if there are any conflicts, albeit at startup instead of at link time. Whatever.

oshima

unread,
Mar 5, 2012, 4:47:58 PM3/5/12
to will...@chromium.org, jabde...@google.com, ach...@google.com, Chromium-dev
gtest_filter assumes that test names are globally unique in the binary, and putting tests inside namespace breaks this rule.
Valgrind bots uses this to exclude failing test so it'd be nice if linker can catch such case early.

- oshima

Satoru Takabayashi

unread,
Mar 6, 2012, 2:02:03 PM3/6/12
to osh...@chromium.org, will...@chromium.org, jabde...@google.com, ach...@google.com, Chromium-dev
Just FYI.

TEST_Fs with the same test case name from different namespaces are banned and handled as a run time error:

http://code.google.com/p/googletest/wiki/FAQ?ts=1331059474&updated=FAQ#Is_it_OK_if_I_have_two_separate_TEST(Foo,_Bar)_test_methods_defi

gtest.cc:

      // The user defined two fixture classes with the same name in             
      // two namespaces - we'll tell him/her how to fix it.                     
      ADD_FAILURE()
          << "All tests in the same test case must use the same test fixture\n"
          << "class.  However, in test case "
          << this_test_info->test_case_name() << ",\n"
          << "you defined test " << first_test_name
          << " and test " << this_test_name << "\n"
          << "using two different test fixture classes.  This can happen if\n"
          << "the two classes are from different namespaces or translation\n"
          << "units and have the same name.  You should probably rename one\n"
          << "of the classes to put the tests into different test cases.";



TESTs with the same test case name from different namespaces are not banned, but these aren't very harmful (i.e. developers may be surprised if they run with --gtest_filter, but they can change names easily).

Satoru

Marc-Antoine Ruel

unread,
Mar 6, 2012, 8:10:26 PM3/6/12
to sat...@chromium.org, osh...@chromium.org, will...@chromium.org, jabde...@google.com, ach...@google.com, Chromium-dev
Le 6 mars 2012 14:02, Satoru Takabayashi <sat...@chromium.org> a écrit :
Just FYI.

TEST_Fs with the same test case name from different namespaces are banned and handled as a run time error:

http://code.google.com/p/googletest/wiki/FAQ?ts=1331059474&updated=FAQ#Is_it_OK_if_I_have_two_separate_TEST(Foo,_Bar)_test_methods_defi

gtest.cc:

      // The user defined two fixture classes with the same name in             
      // two namespaces - we'll tell him/her how to fix it.                     
      ADD_FAILURE()
          << "All tests in the same test case must use the same test fixture\n"
          << "class.  However, in test case "
          << this_test_info->test_case_name() << ",\n"
          << "you defined test " << first_test_name
          << " and test " << this_test_name << "\n"
          << "using two different test fixture classes.  This can happen if\n"
          << "the two classes are from different namespaces or translation\n"
          << "units and have the same name.  You should probably rename one\n"
          << "of the classes to put the tests into different test cases.";



TESTs with the same test case name from different namespaces are not banned, but these aren't very harmful (i.e. developers may be surprised if they run with --gtest_filter, but they can change names easily).

It'd be preferable to not allow that. sharding_supervisor already retry tests that failed and having duplicate names could be messy to handle. Right now the behavior is simply undefined.

M-A 
Reply all
Reply to author
Forward
0 new messages