Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33944

How can I run google death tests in debug only?

We have a series of death tests that check that specific debug asserts fire. For example, we construct something like this:

LockManager::LockManager(size_t numManagedLocks) :
    _numManagedLocks(numManagedLocks)
{
    assert(_numManagedLocks <= MAX_MANAGABLE_LOCKS &&
        "Attempting to manage more than the max possible locks.");

And we have a test for its failure:

EXPECT_DEATH(LockManager sutLockManager(constants::MAX_NUMBER_LOCKS + 1), 
    "Attempting to manage more than the max possible locks.");

Since assert is only compiled in debug, these tests will fail when the component is built in release. Is the best way to avoid this to wrap EXPECT_DEATH tests in a DEBUG detection macro:

#ifndef NDEBUG
     // DEATH TESTS
#endif

Or is there an approach that is better and specific to Google Test?

Upvotes: 1

Views: 1565

Answers (3)

Olppah
Olppah

Reputation: 914

I think that GTest have a solution to this now: EXPECT_DEBUG_DEATH

Upvotes: 1

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33944

We generated a working MACRO to use in place of full death tests or just the ASSERT_DEATH that appears in amongst other tests:

#if defined _DEBUG

    #define DEBUG_ONLY_TEST_F(test_fixture, test_name) \
        TEST_F(test_fixture, test_name)
    #define DEBUG_ONLY_ASSERT_DEATH(statement, regex) \
        ASSERT_DEATH(statement, regex)

#else

    #define DEBUG_ONLY_TEST_F(test_fixture, test_name) \
        TEST_F(test_fixture, DISABLED_ ## test_name)
    #define DEBUG_ONLY_ASSERT_DEATH(statement, regex) \
        std::cout << "WARNING: " << #statement << " test in " << __FUNCTION__ << " disabled becuase it uses assert and fails in release.\n";

#endif

Of course we will need to override any other test types we use (eg. TEST_P or EXPECT_DEATH), but this shouldn't be a big problem.

Upvotes: 0

Spock77
Spock77

Reputation: 3325

Since assert() macro uses preprocessor logic, the solution should be also on this level - via conditional compilation. You can use GoogleTest specific DISABLED_ syntax (See Temporarily Disabling Tests) and write something like

#ifdef _DEBUG
#define DEBUG_TEST_ 
#else
#define DEBUG_TEST_ DISABLED_
#endif 

Your original suggestion also looks good, however I would better write direct condition:

#ifdef _DEBUG 
 ...

Upvotes: 1

Related Questions