非测试代码中的gtest断言

时间:2015-01-16 22:35:01

标签: c++ unit-testing assert googletest

我正在开发一个C ++库,我正在使用gtest进行单元测试。我想将ASSERT_ *语句添加到库代码本身,而不仅仅是单元测试代码。如果代码在单元测试下运行,我希望这些ASSERTions导致单元测试失败,如果代码没有在unite测试下运行,我希望转为常规断言。

类似的东西:

if(gtest::is_running)
    ASSERT_TRUE(...);
else
    assert(...);

我怎样才能做到这一点?

5 个答案:

答案 0 :(得分:2)

即使这在技术上是可行的(我也不认为),我真的不相信让你的生产代码依赖于测试框架是一个好主意。

主要原因是健壮性,关注点分离和解耦:在生产代码中引入特定于测试的条件会使代码更难以理解,并且可能会降低测试套件的可信度(毕竟,您的测试获胜并且#39 ; t强调生产代码将经历的完全相同的路径。)

此外,有一天您可能想要在测试环境中更改某些内容(例如,单元测试框架的版本或单元测试框架本身),并且此依赖关系可能会强制您相应地修改生产代码,引入新bug的风险。

如果要验证的是,当客户端违反函数的前提条件时(例如,如果要测试前提条件是否被您的断言正确验证),您的断言实际触发,则this proposal可能与你有关,以及启发它的图书馆Bloomberg's BDE

如果这不是您项目的可行技术,也许您可​​以考虑采用基于依赖性倒置的策略。最简单的方法是:

  1. 使用抽象成员函数Verifier定义一个抽象类verify(),其中bool;
  2. 从中派生AssertingVerifier类(将在生产代码中使用)覆盖verify()并将其参数转发给assert()VerifierAssertVerifier都会存在于您的生产代码中;
  3. 在您的单元测试项目中,定义第二个派生类GracefulTestVerifier,它将覆盖verify()并将其参数转发给ASSERT_TRUE() - 或者执行您认为最合适的任何操作; < / LI>
  4. 找出将Verifier注入生产代码的最佳方法 - 存在多种可能性,但要知道哪一种最适合,需要详细了解您的设计。然后,您将在常规执行环境中注入AssertVerifier,在测试环境中注入GracefulTestVerifier
  5. 这样,执行可以从生产代码流向测试框架,而生产代码在物理上不依赖于测试框架本身。

答案 1 :(得分:1)

从另一个方向接近这个怎么样?而不是改变你的gtest行为,改变你的断言的行为。

例如,

Boost.Assert提供了一个BOOST_ASSERT宏,默认情况下,其行为与assert相同。但是,如果定义了BOOST_ENABLE_ASSERT_HANDLER,那么它会查找您必须提供的::boost::assertion_failed函数。您可以设计库代码,使用标准断言行为在测试套件之外构建,并使用::boost::assertion_failed在测试套件内调用gtest的FAIL()

如果你不想使用Boost,那么自己实现类似的东西是微不足道的。

这需要两次构建您的库(一次用于测试套件,一次用于常规用途),这可能不符合您的总体目标。

答案 2 :(得分:1)

您可以使用预处理程序指令。

使用gtest编译时,请告诉编译器定义类似“GTEST_ON”的内容,然后在代码中:

#ifdef GTEST_ON
    ASSERT_TRUE(...);
#else
    assert(...);
#endif

答案 3 :(得分:1)

以下是@Josh Kelley的建议后我最终要做的事情:

我已从assert切换到BOOST_ASSERT。 我没有包含boost/assert.hpp我添加了自己的包含Boost文件的assert.hpp文件,而是定义了BOOST_ENABLE_ASSERT_HANDLER和一个BOOST_ASSERT_HANDLER函数指针(与Boost完全相同的类型)断言处理程序)。

我还包括了我自己的Boost断言处理程序(::boost::assertion_failed),它将断言信息输出到std::cerr并调用BOOST_ASSERT_HANDLER指向的函数(如果存在)。如果没有,那只是assert(false) s。

在我的测试主程中,我将BOOST_ASSERT_HANDLER指向一个只调用EXPECT_FALSE(true)的函数。

就是这样。现在我可以在不在gtest下运行时使用普通断言,并且在gtest下运行时gtest-integrated断言。

答案 4 :(得分:0)

我基本上使用这些来源。他们非常独立。

要使其有效,您必须执行以下步骤:

  1. 使用从库源编译而不是与库文件链接的单元测试(这与cmake很容易)。
  2. 已将单元测试和基准测试提取到独立项目中,并为单元测试定义了UNIT_TESTS定义,但尚未用于基准测试。
  3. utility/assert.hpp标题包含之前,将gtest/gtest.hpp包含在单元测试和主项目的主标题中。
  4. 使用ASSERT_TRUE / ASSERT_EQ / etc代替assert
  5. 注意:如果是基准测试,则不应定义UNIT_TESTS定义,否则断言定义会降低执行速度。

    <强>效用/ assert.hpp

    <强> UPD1

    • 修复了断言表达式评估,仅对其进行一次评估

    &#39;

    #pragma once
    
    #include "debug.hpp"
    
    #ifdef UNIT_TESTS
    #include <gtest/gtest.h>
    #endif
    
    #include <cassert>
    
    #define ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(exp, precondition) \
        if (!(precondition)); else if(!!(exp)); else ::utility::debug_break()
    
    #ifdef GTEST_FAIL
    
    #ifdef _MSC_VER
        #if _MSC_VER < 1600 // < MSVC++ 10 (Visual Studio 2010)
            #error lambda is not supported
        #endif
    #else
        #if __cplusplus < 201103L
            #error lambda is not supported
        #endif
    #endif
    
    // TIPS:
    //  * all lambdas captured by reference because of the error in the MSVC 2015:
    //    `error C3493 : '...' cannot be implicitly captured because no default capture mode has been specified`
    //  * if debugger is attached but `::testing::GTEST_FLAG(break_on_failure)` has not been setted,
    //    then an assertion does a post break.
    
    // gtest asserts rebind with the `void` error workaround (C++11 and higher is required)
    #undef ASSERT_TRUE
    #define ASSERT_TRUE(condition) [&]() -> void { \
            const bool is_success = ::utility::is_true(condition); \
            const bool break_on_failure = ::testing::GTEST_FLAG(break_on_failure); \
            if (break_on_failure) { \
                GTEST_TEST_BOOLEAN_(is_success, #condition, false, true, GTEST_FATAL_FAILURE_); \
            } else { \
                GTEST_TEST_BOOLEAN_(is_success, #condition, false, true, GTEST_NONFATAL_FAILURE_); \
            } \
            ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(is_success, !break_on_failure); \
        }()
    #undef ASSERT_FALSE
    #define ASSERT_FALSE(condition) [&]() -> void { \
            const bool is_success = ::utility::is_false(condition); \
            const bool break_on_failure = ::testing::GTEST_FLAG(break_on_failure); \
            if (break_on_failure) { \
                GTEST_TEST_BOOLEAN_(is_success, #condition, true, false, GTEST_FATAL_FAILURE_); \
            } else { \
                GTEST_TEST_BOOLEAN_(is_success, #condition, true, false, GTEST_NONFATAL_FAILURE_); \
            } \
            ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(is_success, !break_on_failure); \
        }()
    
    #if !GTEST_DONT_DEFINE_ASSERT_EQ
    #undef ASSERT_EQ
    #define ASSERT_EQ(val1, val2) [&]() -> void { \
            const ::testing::AssertionResult exp_value = ::testing::internal::EqHelper<GTEST_IS_NULL_LITERAL_(val1)>::Compare(#val1, #val2, val1, val2); \
            const bool break_on_failure = ::testing::GTEST_FLAG(break_on_failure); \
            if (break_on_failure) { \
                GTEST_ASSERT_(exp_value, GTEST_FATAL_FAILURE_); \
            } else { \
                GTEST_ASSERT_(exp_value, GTEST_NONFATAL_FAILURE_); \
            } \
            ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(exp_value, !break_on_failure); \
        }()
    #endif
    
    #if !GTEST_DONT_DEFINE_ASSERT_NE
    #undef ASSERT_NE
    #define ASSERT_NE(val1, val2) [&]() -> void { \
            const ::testing::AssertionResult exp_value = ::testing::internal::CmpHelperNE(#val1, #val2, val1, val2); \
            const bool break_on_failure = ::testing::GTEST_FLAG(break_on_failure); \
            if (break_on_failure) { \
                GTEST_ASSERT_(exp_value, GTEST_FATAL_FAILURE_); \
            } else { \
                GTEST_ASSERT_(exp_value, GTEST_NONFATAL_FAILURE_); \
            } \
            ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(exp_value, !break_on_failure); \
        }()
    #endif
    
    #if !GTEST_DONT_DEFINE_ASSERT_LE
    #undef ASSERT_LE
    #define ASSERT_LE(val1, val2) [&]() -> void { \
            const ::testing::AssertionResult exp_value = ::testing::internal::CmpHelperLE(#val1, #val2, val1, val2); \
            const bool break_on_failure = ::testing::GTEST_FLAG(break_on_failure); \
            if (break_on_failure) { \
                GTEST_ASSERT_(exp_value, GTEST_FATAL_FAILURE_); \
            } else { \
                GTEST_ASSERT_(exp_value, GTEST_NONFATAL_FAILURE_); \
            } \
            ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(exp_value, !break_on_failure); \
        }()
    #endif
    
    #if !GTEST_DONT_DEFINE_ASSERT_LT
    #undef ASSERT_LT
    #define ASSERT_LT(val1, val2) [&]() -> void { \
            const ::testing::AssertionResult exp_value = ::testing::internal::CmpHelperLT(#val1, #val2, val1, val2); \
            const bool break_on_failure = ::testing::GTEST_FLAG(break_on_failure); \
            if (break_on_failure) { \
                GTEST_ASSERT_(exp_value, GTEST_FATAL_FAILURE_); \
            } else { \
                GTEST_ASSERT_(exp_value, GTEST_NONFATAL_FAILURE_); \
            } \
            ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(exp_value, !break_on_failure); \
        }()
    #endif
    
    #if !GTEST_DONT_DEFINE_ASSERT_GE
    #undef ASSERT_GE
    #define ASSERT_GE(val1, val2) [&]() -> void { \
            const ::testing::AssertionResult exp_value = ::testing::internal::CmpHelperGE(#val1, #val2, val1, val2); \
            const bool break_on_failure = ::testing::GTEST_FLAG(break_on_failure); \
            if (break_on_failure) { \
                GTEST_ASSERT_(exp_value, GTEST_FATAL_FAILURE_); \
            } else { \
                GTEST_ASSERT_(exp_value, GTEST_NONFATAL_FAILURE_); \
            } \
            ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(exp_value, !break_on_failure); \
        }()
    #endif
    
    #if !GTEST_DONT_DEFINE_ASSERT_GT
    #undef ASSERT_GT
    #define ASSERT_GT(val1, val2) [&]() -> void { \
            const ::testing::AssertionResult exp_value = ::testing::internal::CmpHelperGT(#val1, #val2, val1, val2); \
            const bool break_on_failure = ::testing::GTEST_FLAG(break_on_failure); \
            if (break_on_failure) { \
                GTEST_ASSERT_(exp_value, GTEST_FATAL_FAILURE_); \
            } else { \
                GTEST_ASSERT_(exp_value, GTEST_NONFATAL_FAILURE_); \
            } \
            ASSERT_FAIL_BREAK_ON_ATTACHED_DEBUGGER(exp_value, !break_on_failure); \
        }()
    #endif
    
    #define ASSERT(x) ASSERT_TRUE(x)
    
    #else
    
    #ifndef ASSERT_IMPL
    #define ASSERT_IMPL(exp) assert(exp)
    #endif
    
    #ifdef _DEBUG
    
    #define ASSERT_TRUE(exp) ASSERT_IMPL(exp)
    #define ASSERT_FALSE(exp) ASSERT_IMPL(!(exp))
    
    #define ASSERT_EQ(v1, v2) ASSERT_IMPL((v1) == (v2))
    #define ASSERT_NE(v1, v2) ASSERT_IMPL((v1) != (v2)))
    #define ASSERT_LE(v1, v2) ASSERT_IMPL((v1) <= (v2))
    #define ASSERT_LT(v1, v2) ASSERT_IMPL((v1) < (v2))
    #define ASSERT_GE(v1, v2) ASSERT_IMPL((v1) >= (v2))
    #define ASSERT_GT(v1, v2) ASSERT_IMPL((v1) > (v2))
    
    #define ASSERT(exp) ASSERT_IMPL(exp)
    
    #else
    
    #define ASSERT_TRUE(exp) (::utility::is_true(exp), (void)0)
    #define ASSERT_FALSE(exp) (::utility::is_false(exp), (void)0))
    
    #define ASSERT_EQ(v1, v2) (::utility::is_equal(v1, v2), (void)0)
    #define ASSERT_NE(v1, v2) (::utility::is_not_equal(v1, v2), (void)0)
    #define ASSERT_LE(v1, v2) (::utility::is_less_or_equal(v1, v2), (void)0)
    #define ASSERT_LT(v1, v2) (::utility::is_less(v1, v2), (void)0)
    #define ASSERT_GE(v1, v2) (::utility::is_greater_or_equal(v1, v2), (void)0)
    #define ASSERT_GT(v1, v2) (::utility::is_greater(v1, v2), (void)0)
    
    #define ASSERT(exp) ::utility::is_true(exp)
    
    #endif
    
    #endif
    
    namespace utility
    {
        // TIPS:
        // * to capture parameters by reference in macro definitions for single evaluation
        // * to suppress `unused variable` warnings like: `warning C4101: '...': unreferenced local variable`
        template<typename T>
        inline bool is_true(const T & v)
        {
            return !!v; // to avoid warnings of truncation to bool
        }
    
        template<typename T>
        inline bool is_false(const T & v)
        {
            return !v; // to avoid warnings of truncation to bool
        }
    
        template<typename T1, typename T2>
        inline bool is_equal(const T1 & v1, const T2 & v2)
        {
            return v1 == v2;
        }
    
        template<typename T1, typename T2>
        inline bool is_not_equal(const T1 & v1, const T2 & v2)
        {
            return v1 != v2;
        }
    
        template<typename T1, typename T2>
        inline bool is_less_or_equal(const T1 & v1, const T2 & v2)
        {
            return v1 <= v2;
        }
    
        template<typename T1, typename T2>
        inline bool is_less(const T1 & v1, const T2 & v2)
        {
            return v1 < v2;
        }
    
        template<typename T1, typename T2>
        inline bool is_greater_or_equal(const T1 & v1, const T2 & v2)
        {
            return v1 >= v2;
        }
    
        template<typename T1, typename T2>
        inline bool is_greater(const T1 & v1, const T2 & v2)
        {
            return v1 > v2;
        }
    }
    

    <强>效用/ debug.hpp

    #pragma once
    
    namespace utility
    {
        void debug_break(bool breakCondition = true);
        bool is_under_debugger();
    }
    

    <强>效用/ debug.cpp

    #include "debug.hpp"
    #include "platform.hpp"
    
    #if defined(UTILITY_PLATFORM_WINDOWS)
    #include <windows.h>
    #include <intrin.h>
    #elif defined(UTILITY_PLATFORM_POSIX)
    #include <sys/ptrace.h>
    #include <signal.h>
    static void signal_handler(int) { }
    #else
    #error is_under_debugger is not supported for this platform
    #endif
    
    
    namespace utility {
    
    void debug_break(bool breakCondition)
    {
        // avoid signal if not under debugger
        if (breakCondition && is_under_debugger()) {
    #if defined(UTILITY_COMPILER_CXX_MSC)
            __debugbreak(); // won't require debug symbols to show the call stack, when the DebugBreak() will require system debug symbols to show the call stack correctly
    #elif defined(UTILITY_PLATFORM_POSIX)
            signal(SIGTRAP, signal_handler);
    #else
    #error debug_break is not supported for this platform
    #endif
        }
    }
    
    bool is_under_debugger()
    {
    #if defined(UTILITY_PLATFORM_WINDOWS)
        return !!::IsDebuggerPresent();
    #elif defined(UTILITY_PLATFORM_POSIX)
        return ptrace(PTRACE_TRACEME, 0, NULL, 0) == -1;
    #endif
    }
    
    }
    

    <强>效用/ platform.hpp

    #pragma once
    
    // linux, also other platforms (Hurd etc) that use GLIBC, should these really have their own config headers though?
    #if defined(linux) || defined(__linux) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
    #  define UTILITY_PLATFORM_LINUX
    #  define UTILITY_PLATFORM_POSIX
    #  if defined(__mcbc__)
    #     define UTILITY_PLATFORM_MCBC
    #     define UTILITY_PLATFORM_SHORT_NAME "MCBC"
    #  elif defined( __astra_linux__ )
    #     define UTILITY_PLATFORM_ASTRA_LINUX
    #     define UTILITY_PLATFORM_SHORT_NAME "Astra Linux"
    #  else
    #     define UTILITY_PLATFORM_SHORT_NAME "Linux"
    #  endif
    #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) // BSD:
    #  define UTILITY_PLATFORM_BSD
    #  define UTILITY_PLATFORM_POSIX
    #  define UTILITY_PLATFORM_SHORT_NAME "BSD"
    #elif defined(sun) || defined(__sun) // solaris:
    #  define UTILITY_PLATFORM_SOLARIS
    #  define UTILITY_PLATFORM_POSIX
    #  define UTILITY_PLATFORM_SHORT_NAME "Solaris"
    #elif defined(__CYGWIN__) // cygwin is not win32:
    #  define UTILITY_PLATFORM_CYGWIN
    #  define UTILITY_PLATFORM_POSIX
    #  define UTILITY_PLATFORM_SHORT_NAME "Cygwin"
    #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32) // win32:
    #  define UTILITY_PLATFORM_WINDOWS
    #  define UTILITY_PLATFORM_SHORT_NAME "Windows"
    #  if defined(__MINGW32__)  //  Get the information about the MinGW runtime, i.e. __MINGW32_*VERSION.
    #     include <_mingw.h>
    #  endif
    #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) // MacOS
    #  define UTILITY_PLATFORM_APPLE
    #  define UTILITY_PLATFORM_POSIX
    #  define UTILITY_PLATFORM_SHORT_NAME "MacOS"
    #elif defined(__QNXNTO__)  // QNX:
    #  define UTILITY_PLATFORM_QNIX
    #  define UTILITY_PLATFORM_POSIX
    #  define UTILITY_PLATFORM_SHORT_NAME "QNX"
    #elif defined(unix) || defined(__unix) || defined(_XOPEN_SOURCE) || defined(_POSIX_SOURCE)
    #  define UTILITY_PLATFORM_UNIX
    #  define UTILITY_PLATFORM_POSIX
    #  define UTILITY_PLATFORM_SHORT_NAME "Unix"
    #else
    #   error Unknown platform
    #endif
    
    #if defined(__GNUC__)
    #   define UTILITY_COMPILER_CXX_GCC
    #   define UTILITY_COMPILER_CXX "gcc"
    #   define UTILITY_COMPILER_CXX_VERSION __GNUC__
    #   if __GNUC__ < 4
    #     error "Unsuported gcc version"
    #   endif
    #elif defined(_MSC_VER)
    #   define UTILITY_COMPILER_CXX_MSC
    #   define UTILITY_COMPILER_CXX "MS VisualC"
    #   define UTILITY_COMPILER_CXX_VERSION _MSC_VER
    #else
    #   error "Unknown compiler"
    #endif