• Google Test Primer(入门)(六) 结束部分


    Invoking the Tests


    调用
    测试


    TEST()
    and TEST_F() implicitly register their tests with Google Test. So, unlike with many other C++ testing frameworks, you don't have to re-list all your defined tests in order to run them.

    TEST()TEST_F()Google Test中暗中注册他们的测试。因此,与其他C++测试框架不同的是你不必为运行他们再次列出所有自定义的测试


    After defining your tests, you can run them with
    RUN_ALL_TESTS() , which returns 0 if all the tests are successful, or 1 otherwise. Note that RUN_ALL_TESTS() runs all tests in your link unit -- they can be from different test cases, or even different source files.

    自定义测试后,用RUN_ALL_TESTS()运行测试,如果全部测试成功,RUN_ALL_TEST()返回0,否则失败返回1。注意RUN_ALL_TESTS()运行所有在链接单元中的测试——这些测试自不同的测试案例,甚至不同的源文件。


    When invoked, the
    RUN_ALL_TESTS() macro:

    当被调用时,RUN_ALL_TESTS()宏完成下列事情:

    1. Saves the state of all Google Test flags. 保存所有的Google Test标志状态
    2. Creates a test fixture object for the first test.  为第一次测试创建test fixture对象
    3. Initializes it via SetUp(). 通过SetUp()初始化test fixture对象
    4. Runs the test on the fixture object.fixture对象上运行测试
    5. Cleans up the fixture via TearDown(). 通过TearDown()清除fixture
    6. Deletes the fixture. 删除fixture
    7. Restores the state of all Google Test flags.恢复Google Test标志
    8. Repeats the above steps for the next test, until all tests have run. 重复上面的步骤直到所有的测试都运行完毕。

    In addition, if the text fixture's constructor generates a fatal failure in step 2, there is no point for step 3 - 5 and they are thus skipped. Similarly, if step 3 generates a fatal failure, step 4 will be skipped.

    此外,如果在步骤2test fixture的构造函数产生了一个致命错误,步骤3-5将没有进入点,因此将会被跳过,类似的,如果步骤3产生致命错误,步骤4也将被跳过。


    Important
    : You must not ignore the return value of RUN_ALL_TESTS(), or gcc will give you a compiler error. The rationale for this design is that the automated testing service determines whether a test has passed based on its exit code, not on its stdout/stderr output; thus your main() function must return the value of RUN_ALL_TESTS().


    重要:必须不能忽略
    RUN_ALL_TESTS()的返回值,否则gcc编译器将给出一个编译错误。如此设计的理由是自动测试服务决定是否传递一个基于退出码,而不是基于stdout/stderr输出的测试,因此main()函数必须返回RUN_ALL_TESTS()值。


    Also, you should call
    RUN_ALL_TESTS() only once. Calling it more than once conflicts with some advanced Google Test features (e.g. thread-safe death tests) and thus is not supported.


    同时,应该仅一次调用
    RUN_ALL_TESTS(),多次调用将对一些高级的Google Test特征产生冲突(例如线程安全的death测试),从而使得这些特征不被支持。


    Availability
    : Linux, Windows, Mac.


    Writing the main() Function


    编写
    main()函数


    You can start from this boilerplate:

    从下面的样本文件开始:

    #include "this/package/foo.h"

    #include <gtest/gtest.h>

     

    namespace {

     

    // The fixture for testing class Foo.

    class FooTest : public testing::Test {

     protected:

      // You can remove any or all of the following functions if its body

      // is empty.

     

      FooTest() {

        // You can do set-up work for each test here.

      }

     

      virtual ~FooTest() {

        // You can do clean-up work that doesn't throw exceptions here.

      }

     

      // If the constructor and destructor are not enough for setting up

      // and cleaning up each test, you can define the following methods:

     

      virtual void SetUp() {

        // Code here will be called immediately after the constructor (right

        // before each test).

      }

     

      virtual void TearDown() {

        // Code here will be called immediately after each test (right

        // before the destructor).

      }

     

      // Objects declared here can be used by all tests in the test case for Foo.

    };

     

    // Tests that the Foo::Bar() method does Abc.

    TEST_F(FooTest, MethodBarDoesAbc) {

      const string input_filepath = "this/package/testdata/myinputfile.dat";

      const string output_filepath = "this/package/testdata/myoutputfile.dat";

      Foo f;

      EXPECT_EQ(0, f.Bar(input_filepath, output_filepath));

    }

     

    // Tests that Foo does Xyz.

    TEST_F(FooTest, DoesXyz) {

      // Exercises the Xyz feature of Foo.

    }

     

    }  // namespace

     

    int main(int argc, char **argv) {

      testing::InitGoogleTest(&argc, argv);

      return RUN_ALL_TESTS();

    }


    The
    testing::InitGoogleTest() function parses the command line for Google Test flags, and removes all recognized flags. This allows the user to control a test program's behavior via various flags, which we'll cover in GTestAdvanced. You must call this function before calling RUN_ALL_TESTS(), or the flags won't be properly initialized.

    testing::InitGoogleTest()函数为Google Test标志分析命令行,移除所有认证的标志。允许用户通过不同的标志控制测试程序,这并不覆盖GTestAdvanced。需要在RUN_ALL_TESTS()之前调用该函数,否则标注不能够被合适的初始化。


    On Windows,
    InitGoogleTest() also works with wide strings, so it can be used in programs compiled in UNICODE mode as well.

    Windows中,InitGoogleTest()也支持宽字节字符串,因此也可以通过UNICODE模式编译。


    But maybe you think that writing all those main() functions is too much work? We agree with you completely and that's why Google Test provides a basic implementation of main(). If it fits your needs, then just link your test with gtest_main library and you are good to go.

    但是,你可能会考虑如果编写所有这些main函数为太多的工作,我们赞同你的观点,那就是Google Test为什么提供了main的一个基础实现。如果符合你的要求,仅需要连接gtest_main库到你的测试程序中,就可以准确的工作了


    Where to Go from Here


    进阶阅读

    Congratulations! You've learned the Google Test basics. You can start writing and running Google Test tests, read some GoogleTestSamples, or continue with GoogleTestAdvancedGuide, which describes many more useful Google Test features.


    恭喜你!你已经学习了
    Google Test的基本概念。可以开始编写与运行Google Test测试了,阅读一些GoogleTestSamples或继续GoogleTestAdvancedGuide,其中将描述更多有用的Google Test特性。


    Known Limitations


    知识局限

    Google Test is designed to be thread-safe. However, we haven't had time to implement the synchronization primitives on various platforms yet. Therefore, it is currently unsafe to use Google Test assertions from two threads concurrently. In most tests this is not an issue as usually the assertions are done in the main thread. If you want to help, you can volunteer to implement the necessary synchronization primitives in gtest-port.h.


    Google Test
    被设计为线程安全的。但是,我们没有足够的时间实现不同平台同步原语(synchronization primitives)。因此,当前在两个线程中同时使用Google Test断言并不安全。由于断言通常是在主线程中完成的,在大部分测试中这不是问题。如果你愿意帮忙,可以在gtest-port.h中实现必要的synchronization primitives
  • 相关阅读:
    java连接Mysql数据库
    js数组的操作
    Eclipse安装flash builder4.6插件
    MyEclipse 7.5,MyEclipse 8.0到10不好安装FLEX插件了
    关于MyEclipse10的破解激活
    用PHP做Linux/Unix下守护进程
    Debugging Tip: “Disallowed Key Character” Error In CodeIgniter
    股指期货模拟系统
    几个基本的设计原则
    基于mirror driver的windows屏幕录像
  • 原文地址:https://www.cnblogs.com/ubunoon/p/GoogleTestPrimerTest.html
Copyright © 2020-2023  润新知