如何将参数传递给gtest

发布于 2024-10-15 01:05:12 字数 2193 浏览 2 评论 0原文

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

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 开头的参数之外的正确参数。

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

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

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

This is the arguments given to the 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.

Reference - How to run specific test cases in GoogleTest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2024-10-22 01:05:12

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

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

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.

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_.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文