C++ 如何将参数传递给gtest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4818785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:38:24  来源:igfitidea点击:

How to pass parameters to the gtest

c++unit-testinggoogletest

提问by prosseek

How can I pass parameter to my test suites?

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

gtest --number-of-input=5

I have the following main gtest code. And --number-of-input=5should be passed to InitGoogleTest().

我有以下主要的 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();
}

I don't know how to pass my parameter to the test suites/cases as follows?

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

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);
}

Added

添加

I modified the main method to show the argv[i] after InitGoogleTest().

我修改了 main 方法以在InitGoogleTest().

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;
    }

This is the arguments given to the gtest: ./s --number-of-input=5 --gtest_filter=Test_Cases1*.

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

This is the results:

这是结果:

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 filters out the tests that does not have the name of Test_Cases1, and it also shows the correct arguments other than those start with gtest.

gtest 过滤掉没有名称的测试Test_Cases1,并且它还显示了除以 开头的参数之外的正确参数gtest

Reference - How to run specific test cases in GoogleTest

参考 -如何在 GoogleTest 中运行特定的测试用例

回答by Rob Kennedy

Google Test only recognizes its own command-line options. Each time it finds one, it removes it from argvand updates argcaccordingly, so after InitGoogleTestreturns, anything left over in argvis available for you to process yourself. Use your favorite command-line-parsing technique, store the results in some global variable, and refer to it during your tests.

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

If a command-line options looks likea Google Test option but really isn't, then the program will print its help message and exit without running any tests. Google Test options start with gtest_.

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