且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

boost.test主场迎战的CppUnit

更新时间:2023-12-01 08:29:28

请你帮个忙,并直接进入谷歌测试,这使得CppUnit的和的boost :: unit_test 看起来笨重,重复性的。

Do yourself a favor and go straight to Google Test, which makes CppUnit and boost::unit_test look clunky and repetitive.

举例来说,假设你有一个简单的夹具:

For example, say you have a simple fixture:

class MyFixture : public ::testing::Test
{
  protected:
  int foo;

  virtual void SetUp() { foo = 0; }
};

要测试添加到您的夹具,写出来!

To add a test to your fixture, write it!

TEST_F(MyFixture, FooStartsAtZero) {
  EXPECT_EQ(0, foo);
}

这就是你需要的。注意,缺少明确的测试套件声明或重复所有的测试的名字一个单独的议程。

That's all you need. Notice the lack of explicit testsuite declarations or a separate agenda that repeats all your tests' names.

编译它作为

$ g++ -o utest utest.cpp -lgtest -lgtest_main

和运行测试,以获得

Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MyFixture
[ RUN      ] MyFixture.FooStartsAtZero
[       OK ] MyFixture.FooStartsAtZero (0 ms)
[----------] 1 test from MyFixture (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.

(运行它自己看到测试通过漂亮的绿色文本!)

(Run it yourself to see the nice green text for passing tests!)

这仅仅是个开始。看看在谷歌测试底漆和的高级指南来看看还有什么是可能的。

This is only the beginning. Take a look at the Google Test primer and the advanced guide to see what else is possible.