如何将参数传递给gtest [英] How to pass parameters to the gtest
问题描述
如何将参数传递给我的测试套件?
How can I pass parameter to my test suites?
gtest --number-of-input=5
我有以下主要的gtest代码。并且 - 输入数= 5
应传递给InitGoogleTest()。
I have the following main gtest code. And --number-of-input=5
should be passed to 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);
}
已添加
我修改了main方法来显示 InitGoogleTest()
后的argv [i]。
Added
I modified the main method to show the argv[i] after 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;
}
这是给予gtest的参数:。 / s --number-of-input = 5 --gtest_filter = Test_Cases1 *
。
This is the arguments given to the 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
开头的正确参数。
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
.
推荐答案
Google测试只识别自己的命令行选项。每次找到它,它会从 argv
中删除它,并相应地更新 argc
,因此在 InitGoogleTest
返回, argv
中剩余的任何内容都可供您自己处理。使用您最喜欢的命令行解析技术,将结果存储在一些全局变量中,并在测试期间参考它。
Google Test only recognizes its own command-line options. Each time it finds one, it removes it from argv
and updates argc
accordingly, so after InitGoogleTest
returns, anything left over in argv
is 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测试选项,但实际上不是,那么程序将打印其帮助消息,退出而不运行任何测试。 Google测试选项以 gtest _
开头。
If a command-line options looks like a 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_
.
这篇关于如何将参数传递给gtest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!