c++ - googletest:如何从内部方法中跳过测试
问题描述
有了GTEST_SKIP
,我们可以(有条件地)跳过一个测试。它的实现方式如下:
#define GTEST_SKIP_(message) \
return GTEST_MESSAGE_(message, ::testing::TestPartResult::kSkip)
如果我们在主测试体中,一切都很好。
如何从我的代码库深处跳过测试?这不起作用:
int skip_this(int x) {
if (x == 3) GTEST_SKIP();
return x;
}
TEST(X, Y) {
skip_this(2);
skip_this(3);
throw std::runtime_error("Did not skip");
}
错误:
/Users/markus/Projekte/Opossum/Git/src/test/operators/aggregate_test.cpp:183:15: error: cannot initialize return object of type 'int' with an rvalue of type 'void'
if (x == 3) GTEST_SKIP();
^~~~~~~~~~~~
换句话说:如何修改skip_this
以使测试通过或标记为“已跳过”?
解决方案
这不是很好,但适用于我的目的。这个想法是展开堆栈,搜索调用测试的 googletest 方法并将指令指针重置为该堆栈帧。
请注意,堆栈未正确销毁,这意味着例如资源可能会泄漏。
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#include <cxxabi.h>
#include <iostream>
#include <gtest/gtest.h>
void skip() {
unw_cursor_t cursor;
unw_context_t context;
unw_getcontext(&context);
unw_init_local(&cursor, &context);
while (unw_step(&cursor)) {
unw_word_t off;
char symbol[256] = {"<unknown>"};
char* name = symbol;
if (!unw_get_proc_name(&cursor, symbol, sizeof(symbol), &off)) {
int status;
if ((name = abi::__cxa_demangle(symbol, nullptr, nullptr, &status)) == nullptr) name = symbol;
}
if (std::string(name) == "testing::Test::Run()") {
::testing::internal::AssertHelper(::testing::TestPartResult::kSkip, __FILE__, __LINE__, "skipped") = ::testing::Message();
unw_resume(&cursor);
}
if (name != symbol) free(name);
}
throw std::runtime_error("Did not find test method on the stack, could not skip test");
}
示例测试(可以粘贴在上面的代码下方):
int foo(int x) {
if (x == 3) skip();
return 11;
}
TEST(SkipTest, ShouldNotBeSkipped) {
foo(2);
EXPECT_TRUE(false);
}
TEST(SkipTest, ShouldBeSkipped) {
foo(3);
EXPECT_TRUE(false);
}
TEST(SkipTest, TestExecutionContinues) {
EXPECT_FALSE(false);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
输出:
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from SkipTest
[ RUN ] SkipTest.ShouldNotBeSkipped
skip_test.cpp:44: Failure
Value of: false
Actual: false
Expected: true
[ FAILED ] SkipTest.ShouldNotBeSkipped (0 ms)
[ RUN ] SkipTest.ShouldBeSkipped
[ SKIPPED ] SkipTest.ShouldBeSkipped (0 ms)
[ RUN ] SkipTest.TestExecutionContinues
[ OK ] SkipTest.TestExecutionContinues (0 ms)
[----------] 3 tests from SkipTest (0 ms total)
[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (0 ms total)
[ PASSED ] 1 test.
[ SKIPPED ] 1 test, listed below:
[ SKIPPED ] SkipTest.ShouldBeSkipped
[ FAILED ] 1 test, listed below:
[ FAILED ] SkipTest.ShouldNotBeSkipped
1 FAILED TEST
推荐阅读
- list - Agda:std-lib:列表:除了最后一个元素之外的所有元素与 snoc
- scheduled-tasks - rundeck 开源有没有办法在失败的工作流步骤中触发作业?
- javascript - 如何让我的 inrole 命令缓存公会中的每个成员
- java - Git、Intellij 和 module-info.java 的问题
- django - 当“if”条件嵌套在 for 循环中时,django 模板中的空标记 {% empty %} 不起作用
- html - 如何在桌子上制作水平丝带
- python - CYK 算法的 Python 实现
- c# - 错误 CS0246 找不到类型或命名空间名称“CreateRandomAnswersForKey”(您是否缺少 using 指令或程序集引用?)?
- javascript - 如何在附加部分中的轮播中获取图像的 src
- ios - UICollectionViewCell 确实选择项目不起作用