gpt4 book ai didi

c++ - Gtest EXPECT_DEATH() 因 SIGABRT 而失败

转载 作者:行者123 更新时间:2023-12-04 19:25:45 26 4
gpt4 key购买 nike

我正在使用 EXPECT_DEATH() 测试断言它在 Ubuntu(gitlab ci 管道)上失败了 killed by signal 6 SIGABRT .但是,完全相同的代码在 Windows 上运行良好。
Afaik,这个宏是专门为处理带有错误代码的退出而设计的。

#include "gtest/gtest.h"

class MyDeathTest : public ::testing::Test {
protected:
...
};

TEST_F(MyDeathTest, TestNegative1) {
EXPECT_DEATH(assert(false), "");
}
我尝试使用 EXPECT_EXIT(assert(false), ::testing::KilledBySignal(SIGBART)), "");而是 ::testing::ExitedWithCode(6) ,以及 EXPECT_DEBUG_DEATH() , EXPECT_DEATH_IF_SUPPORTED()等没有运气。
另外,我尝试设置 GTEST_FLAG_SET(death_test_style, "threadsafe");但得到 error: 'GTEST_FLAG_SET' was not declared in this scope;关于修复的任何建议?

最佳答案

有几件事:

  • 您应该使用 SIGABRT ,而不是 SIGBART
  • 看起来您使用的 google 测试版本很旧并且没有 GTEST_FLAG_SET宏呢。您可以使用它作为替代方案:

  • (void)(::testing::GTEST_FLAG(death_test_style) = "threadsafe");
    工作示例: https://godbolt.org/z/15azKeMG4
    来自 googletest docs

    In googletest, death tests are run in a child process and the way theywork is delicate. To write death tests you really need to understandhow they work—see the details at Death Assertions in the AssertionsReference.

    In particular, death tests don’t like having multiple threads in theparent process. So the first thing you can try is to eliminatecreating threads outside of EXPECT_DEATH(). For example, you may wantto use mocks or fake objects instead of real ones in your tests.

    Sometimes this is impossible as some library you must use may becreating threads before main() is even reached. In this case, you cantry to minimize the chance of conflicts by either moving as manyactivities as possible inside EXPECT_DEATH() (in the extreme case, youwant to move everything inside), or leaving as few things as possiblein it. Also, you can try to set the death test style to "threadsafe",which is safer but slower, and see if it helps.


    所以我想说首先确保您使用的是最新版本,然后尝试使用 threadsafe .

    关于c++ - Gtest EXPECT_DEATH() 因 SIGABRT 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71251067/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com