0

I am using the Catch C++ test framework. I have a separate test-code file, where I have

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

#include "my_own_headers.h"

TEST_CASE( "test of thisnthat", "[thisnthat]" )
{
   SomeStuff a(42);
 ...

Is there some symbol that Catch defines at build time that I can use it to change my code definition?

An example: Say I have some struct that needs to having some special constructor but only for testing purposes, for some specific test case. I want to be able to use this constructor when I build my test code, but the regular build should not enable this function.

// someheader.h
struct SomeStuff
{
   int someValue;
 #if BUILD_IS_TEST_MODE
   SomeStuff( int a ), someValue(a) {}
 #endif
};

What symbol can I use ?

I had a look on https://github.com/philsquared/Catch/blob/master/docs/configuration.md but couldn't find anything relevant.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
kebs
  • 6,387
  • 4
  • 41
  • 70
  • 2
    I'm not aware of any special symbol dedicated for this purpose, however using `TWOBLUECUBES_CATCH_HPP_INCLUDED` header guard protecting "catch.hpp" should work fine. – user7860670 Jul 16 '17 at 17:30
  • 1
    If you want to keep your regular code "clean", you could also simply extend `SomeStuff` to a custom type in your test code. This may not be doable in every situations, but it works well and separate test and regular code properly (in my opinion). – Holt Jul 16 '17 at 17:48
  • Thanks, @Holt, but I'm not sure to understand: how can I manage 2 declarations of the same type in 2 different files ? unclear to me. – kebs Jul 16 '17 at 18:49
  • @kebs You don't declare the same type, you declare a new one that you will be using for testing, e.g. `TestSomeStuff` (see [here](https://godbolt.org/g/BQVh2k) for instance). This won't work everywhere, but it's a start, and it's often sufficient. – Holt Jul 16 '17 at 19:15
  • Oh, inheritance, I see. Yeah, thats a solution, I'd just prefer avoid creating new types, if possible. But thanks for suggesting. – kebs Jul 16 '17 at 19:41

0 Answers0