简体   繁体   English

有没有办法在运行特定测试后停止 GoogleTest,无论它通过还是失败?

[英]Is there a way to stop GoogleTest after a specific test is run, whether it passes or fails?

I have a large number of unit tests run through GoogleTest.我有大量通过 GoogleTest 运行的单元测试。 Currently one of them (call it fooTest) is failing when the full test suite is run, but passing when run alone.当前,其中一个(称为 fooTest)在运行完整测试套件时失败,但在单独运行时通过。 So one (or more then one) of the tests that run before fooTest is doing something that causes it to fail, which of course is a big nono for testing and I obviously want to find the culprit.因此,在 fooTest 之前运行的一个(或多个)测试正在做一些导致它失败的事情,这当然是测试的一个大错误,我显然想找到罪魁祸首。

This is happening with default run conditions, so the test order is always the same.这是在默认运行条件下发生的,因此测试顺序始终相同。 The fooTest is about half way through this run order and there are enough tests that the run time of the second half of the tests is significant, especially if running things multiple times. fooTest 大约完成了这个运行顺序的一半,并且有足够多的测试使得测试的后半部分的运行时间很重要,尤其是在多次运行的情况下。

So I want to set googletest to always stop the test run after fooTest is run - whether it passes or fails.所以我想将 googletest 设置为在 fooTest 运行后始终停止测试运行 - 无论它通过还是失败。 I know I can do --gtest_break_on_failure, but if I do a test run that causes fooTest to pass I still want to stop right there and not go through everything else.我知道我可以做 --gtest_break_on_failure,但如果我进行的测试运行导致 fooTest 通过,我仍然想就此停止,而不是 go 通过其他所有内容。 I could run with debug and add a breakpoint after the test, but that also slows things down a little and feels less then ideal.我可以使用调试运行并在测试后添加一个断点,但这也会稍微减慢速度并且感觉不太理想。

It seems like this could be an simple setting, but I've not been able to find anything yet.看起来这可能是一个简单的设置,但我还没有找到任何东西。 I'm hoping there is either a parameter on run I can use, or a trick to make this happen.我希望有一个我可以使用的运行参数,或者有一个技巧来实现这一点。

You have several options:您有几种选择:

  1. Simply call exit function at the end of fooTest .只需在 fooTest 结束时调用exit fooTest

  2. Create a test fixture .创建一个测试夹具 In SetUp check for a flag that is always false, but it sets to true if fooTest is executed.SetUp中检查始终为 false 的标志,但如果执行fooTest则它设置为 true。 Something like this:是这样的:

bool skip_testing = false;

// Class for test fixture
class MyTestFixture : public ::testing::Test {
 protected:
  void SetUp() override {
    if (skip_testing) {
      GTEST_SKIP();
    }
  }
};

TEST_F(MyTestFixture, test1) {
  //
  EXPECT_EQ(1, 1);
}

TEST_F(MyTestFixture, footest) {
  EXPECT_EQ(1, 1);
  skip_testing = true;
}

TEST_F(MyTestFixture, test2) {
  //
  EXPECT_EQ(1, 1);
}

TEST_F(MyTestFixture, test3) {
  //
  EXPECT_EQ(1, 1);
}


See a working a working example here: https://godbolt.org/z/8dzKGE6Eh在此处查看工作示例: https://godbolt.org/z/8dzKGE6Eh

  1. Similar to Option 2, but use explicit success and failure in SetUp instead of GTEST_SKIP .类似于选项 2,但在SetUp中使用显式成功和失败而不是GTEST_SKIP However using GTEST_SKIP is preferred, cause your output will show that the tests were skipped.但是,首选使用GTEST_SKIP ,因为您的 output 将显示测试已被跳过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM