【问题标题】:How to pass parameters to the gtest如何将参数传递给gtest
【发布时间】:2011-01-27 16:12:10
【问题描述】:

如何将参数传递给我的测试套件?

gtest --number-of-input=5

我有以下主要的 gtest 代码。并且--number-of-input=5 应该被传递给 InitGoogleTest()。

#include <iostream>
#include <gtest/gtest.h>

int main(int argc, char **argv) {
  std::cout << "Running main() from gtest_main.cc\n";
  ::testing::GTEST_FLAG(output) = "xml:hello.xml";
  testing::InitGoogleTest(&argc, argv);

  return RUN_ALL_TESTS();
}

我不知道如何将我的参数传递给测试套件/用例如下?

class TestTwo : public QuickTest {
 protected:
  virtual void SetUp() {
      QuickTest::SetUp();
      square = new Square(10);
      circle = new Circle(10);

  }

  virtual void TearDown() {
      delete square;
      delete circle;
      QuickTest::TearDown();
  }

  Square* square;
  Circle* circle;
};


// Now, let's write tests using the QueueTest fixture.

// Tests the default constructor.
TEST_F(TestOne, DefaultConstructor) {
  EXPECT_EQ(100.0, square->area());
}
TEST_F(TestOne, DefaultDestructor) {
  EXPECT_EQ(1,1);
}
TEST_F(TestOne, VHDL_EMIT_Passthrough) {
  EXPECT_EQ(1,1);
}
TEST_F(TestOne, VHDL_BUILD_Passthrough) {
  EXPECT_EQ(1,1);
}

添加

我修改了 main 方法以在 InitGoogleTest() 之后显示 argv[i]。

int main(int argc, char **argv) {
    std::cout << "Running main() from gtest_main.cc\n";
    ::testing::GTEST_FLAG(output) = "xml:hello.xml";
    testing::InitGoogleTest(&argc, argv);

    for (int i = 0; i < argc; i++) {
        cout << i << ":" << argv[i] << endl;
    }

这是给 gtest 的参数:./s --number-of-input=5 --gtest_filter=Test_Cases1*

这是结果:

Running main() from gtest_main.cc
0:./s
1:--number-of-input=5
Note: Google Test filter = Test_Cases1*
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (0 ms total)
[  PASSED  ] 0 tests.

gtest 过滤掉名称不为Test_Cases1 的测试,并显示除以gtest 开头的参数以外的正确参数。

参考 - How to run specific test cases in GoogleTest

【问题讨论】:

    标签: c++ unit-testing googletest


    【解决方案1】:

    Google Test 仅识别其自己的命令行选项。每次找到一个,它就会从argv 中删除它并相应地更新argc,因此在InitGoogleTest 返回后,argv 中剩余的任何内容都可供您自己处理。使用您最喜欢的命令行解析技术,将结果存储在某个全局变量中,并在测试期间引用它。

    如果命令行选项看起来像 Google 测试选项但实际上并非如此,那么程序将打印其帮助消息并退出而不运行任何测试。 Google 测试选项以 gtest_ 开头。

    【讨论】:

    • 那么你如何处理剩下的从 TEST() 内部访问它们?
    • @David,我认为我第一段的最后一句话完全回答了这个问题。
    猜你喜欢
    • 2018-04-12
    • 2020-10-09
    • 2012-12-21
    • 2012-01-07
    • 2014-05-03
    • 2015-06-26
    • 2012-01-18
    • 2017-07-14
    • 2018-03-08
    相关资源
    最近更新 更多