0

I'm using Catch Lib for unit testing

Previously, I tested a headers individually, and had no issue with TEST_CASEs but after final including all of these into the project I faced with a lot of errors like this:

c:\dev\workspace\algolist\src\algolist.hpp(143): error C2374: '`anonymous-namespace'::autoRegistrar143': redefinition; multiple initialization
c:\dev\workspace\algolist\src\mfactory.hpp(143): note: see declaration of '`anonymous-namespace'::autoRegistrar143'
c:\dev\workspace\algolist\src\algolist.hpp(143): error C2084: function 'void ____C_A_T_C_H____T_E_S_T____143(void)' already has a body
c:\dev\workspace\algolist\src\mfactory.hpp(143): note: see previous definition of '____C_A_T_C_H____T_E_S_T____143'

can someone explain me what does this mean ?

amigo421
  • 2,429
  • 4
  • 26
  • 55
  • don't know the framework but seems like the macros are probably creating function and namespace names just based on code line without filename, and since both files have something in line 143 there is a name collision now – Kai Iskratsch Jan 20 '16 at 14:58

1 Answers1

1

It looks like your're putting TEST_CASE's in the HPP files and then including them both from the same CPP file. You have several options:

  • move the test cases to CPP files instead. This is the preferred solution.
  • move one of the test cases up or down a line so that the auto-generated name is different and so doesn't clash. This is a short-term back.
  • if you really want to share what these tests do by including them, then leave the body of the tests in a header file but remove the TEST_CASE macros and instead invoke them from a test case in a CPP file.
JBRWilkinson
  • 4,821
  • 1
  • 24
  • 36
  • yes, I have a few hpp files as a independent libs , built into one binary, in addition to the code itself, each hpp contains TEST_CASEs, this is a header only library and main.cpp is a CATCH_CONFIG_MAIN for only. – amigo421 Jan 22 '16 at 13:20
  • Usually, tests go in CPP files. There's no problem with a header-only library, but do you want clients of the library to #include your tests too? – JBRWilkinson Jan 23 '16 at 16:03
  • It is defined by #define , but yes, you are right, already moved to separated files. Are you experienced in catch? I'm looking for cmake scripts to extract catch tests from project into ctest automatically – amigo421 Jan 23 '16 at 16:08