Ferenc Deak
Ferenc Deak

Reputation: 35448

Google test macro

I am curious if there is a macro defined for the google test framework which allows me to test if the code is compiled as part of a test, or not. I am interested to achieve the following behaviour:

int someFunction()
{
    // do some stuff and some checks
    if(i_am_not_happy_with_results_above)
    {
    #if CODE_COMPILED_AS_PART_OF_TEST
        FAIL()
    #else
        exit();
    #endif
    }
    return 42;
}

Upvotes: 3

Views: 4340

Answers (3)

Schtolc
Schtolc

Reputation: 1146

As mentioned above, there is no such thing in Google Test, but you can achieve this by defining preprocessor token, just add -DCODE_COMPILED_AS_PART_OF_TEST to your build command to build in test mode. For example,

g++ -DCODE_COMPILED_AS_PART_OF_TEST source.cpp

Upvotes: 5

BЈовић
BЈовић

Reputation: 64283

No, there is no such thing, because that is not what unit tests are for. They tests run-time functionality. If you include or remove some code, ideally unit tests should fail 1.

1 Doesn't always happen, depending on the code.

Upvotes: -1

Paul Evans
Paul Evans

Reputation: 27577

You could use the std::system function from stdlib.h to run your make for the code-build and then test the returned value from make to see if it was successful.

Upvotes: 0

Related Questions