如何在测试系统中自动注册测试用例?

时间:2016-01-13 19:20:33

标签: c++ function unit-testing googletest

通常在测试系统中,当我们编写一个新的测试用例时,我们需要在某个地方注册测试用例,以便可以调用它。

例如在测试系统中: TESTCASE(a,b){...}可以映射到void testcase_a_b() {...},测试系统可以从main调用这些void testcase_a_b()void testcase_c_d()等中的每一个,从而运行所有测试用例。

在可执行文件中自动注册测试用例的方法是什么?例如,在Google Test中(就像其他几个测试框架一样),如果我们调用RUN_ALL_TESTS(),它会自动执行可执行文件中以TEST(a,b)等开头的所有声明。

Google Test如何知道exe中TEST(a,b)的存在?我试图理解(从高级设计角度来看)在C ++中实现类似系统的简单方法。像TEST(a,b)这样的宏会自动将其自身附加到有效测试用例列表中,这样它就可以从main运行而不必担心单独注册它。

1 个答案:

答案 0 :(得分:2)

通常,这是通过创建全局对象来完成的,这些对象在构造时调用注册方法。这违背了普遍认为的良好做法"在C ++中(参见https://isocpp.org/wiki/faq/ctors#static-init-order),所以在尝试这样的实现之前,你应该对这些问题非常熟悉。

无论如何,这是googletest使用的方法 - TEST预处理器宏最终归结为此(gtest-internal.h):

// Helper macro for defining tests.
#define GTEST_TEST_(test_case_name, test_name, parent_class, parent_id)\
class GTEST_TEST_CLASS_NAME_(test_case_name, test_name) : public parent_class {\
 public:\
  GTEST_TEST_CLASS_NAME_(test_case_name, test_name)() {}\
 private:\
  virtual void TestBody();\
  static ::testing::TestInfo* const test_info_ GTEST_ATTRIBUTE_UNUSED_;\
  GTEST_DISALLOW_COPY_AND_ASSIGN_(\
      GTEST_TEST_CLASS_NAME_(test_case_name, test_name));\
};\
\
::testing::TestInfo* const GTEST_TEST_CLASS_NAME_(test_case_name, test_name)\
  ::test_info_ =\
    ::testing::internal::MakeAndRegisterTestInfo(\
        #test_case_name, #test_name, NULL, NULL, \
        (parent_id), \
        parent_class::SetUpTestCase, \
        parent_class::TearDownTestCase, \
        new ::testing::internal::TestFactoryImpl<\
            GTEST_TEST_CLASS_NAME_(test_case_name, test_name)>);\
void GTEST_TEST_CLASS_NAME_(test_case_name, test_name)::TestBody()

因此,当您使用此宏时,类的全局实例使用与测试用例对应的参数调用::testing::internal::MakeAndRegisterTestInfo