简体   繁体   English

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

[英]How to auto-register test cases in a test system?

Usually in test systems when we write a new testcase we need to register the test case somewhere so that it can be called.通常在测试系统中,当我们编写新的测试用例时,我们需要在某处注册测试用例,以便可以调用它。

For example in a test system : TESTCASE(a,b){...} can map to void testcase_a_b() {...} and the test system may call each of these void testcase_a_b() , void testcase_c_d() etc. from main and hence run all the test cases.例如在测试系统中: TESTCASE(a,b){...}可以映射到void testcase_a_b() {...}并且测试系统可以调用这些void testcase_a_b()void testcase_c_d()等中的每一个。从 main 开始运行所有测试用例。

What is the way to auto-register test cases in an executable?在可执行文件中自动注册测试用例的方法是什么? For example, in Google Test (just like several other test frameworks), if we call RUN_ALL_TESTS() it automatically executes all the declarations starting with TEST(a,b) etc. in the executable.例如,在 Google Test 中(就像其他几个测试框架一样),如果我们调用RUN_ALL_TESTS()它会自动执行可执行文件中以TEST(a,b)等开头的所有声明。

How does Google Test know about the existence of TEST(a,b) in the exe ? Google Test 如何知道 exe 中TEST(a,b)的存在? I am trying to understand(from a high level design perspective) what would be a simple way to implement a system like that one in C++.我试图理解(从高级设计的角度)用 C++ 实现这样的系统的简单方法是什么。 where a macro like TEST(a,b) automatically appends itself to the list of valid test cases, so that it can be run from main without worrying about registering it separately.其中像 TEST(a,b) 这样的宏会自动将自身附加到有效测试用例列表中,这样它就可以从 main 运行,而不必担心单独注册它。

Generally this is done by creating global objects, which call a registration method when they are constructed.通常这是通过创建全局对象来完成的,这些对象在构造时调用注册方法。 This goes against generally regarded "good practices" in C++ (see https://isocpp.org/wiki/faq/ctors#static-init-order ), so you should be quite versed in these issues before attempting such an implementation.这与 C++ 中普遍认为的“良好实践”背道而驰(请参阅https://isocpp.org/wiki/faq/ctors#static-init-order ),因此在尝试此类实现之前,您应该非常熟悉这些问题。

Regardless, this is the method googletest uses - the TEST preprocessor macro eventually boils down to this (gtest-internal.h):无论如何,这是 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()

So, when you use this macro, a global instance of a class which calls ::testing::internal::MakeAndRegisterTestInfo with parameters corresponding to the test case.所以,当你使用这个宏时,一个类的全局实例调用::testing::internal::MakeAndRegisterTestInfo并带有与测试用例对应的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM