不清楚您是否要求一种适用于 googletest 的技术,
或捕捉,或两者之一,或两者兼而有之。此答案适用于 googletest。
跳过不需要的测试的习惯方法是使用命令行
为此目的提供的选项,--gtest_filter。
请参阅Documentation。
这是一个用于测试套件的示例,其中蜂鸣器可能或
可能未启用:
test_runner.cpp
#include "gtest/gtest.h"
TEST(t_with_beeper, foo) {
SUCCEED(); // <- Your test code here
}
TEST(t_without_beeper, foo) {
SUCCEED(); // <- Your test code here
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
运行:
./test_runner --gtest_filter=t_with_beeper*
输出:
Note: Google Test filter = t_with_beeper*
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from t_with_beeper
[ RUN ] t_with_beeper.foo
[ OK ] t_with_beeper.foo (0 ms)
[----------] 1 test from t_with_beeper (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 1 test.
运行:
./test_runner --gtest_filter=t_without_beeper*
输出:
Note: Google Test filter = t_without_beeper*
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from t_without_beeper
[ RUN ] t_without_beeper.foo
[ OK ] t_without_beeper.foo (0 ms)
[----------] 1 test from t_without_beeper (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[ PASSED ] 1 test.
报告没有逐项列出跳过的测试,但它相当明显
是否启用蜂鸣器测试,这应该足以
预先排除您担心避免的任何误解或疑虑。
要在test_runner 中启用或禁用蜂鸣器测试,您可以使用
比如:
using namespace std;
int main(int argc, char **argv)
{
vector<char const *> args(argv,argv + argc);
int nargs = argc + 1;
if (have_beeper()) {
args.push_back("--gtest_filter=t_with_beeper*");
} else {
args.push_back("--gtest_filter=t_without_beeper*");
}
::testing::InitGoogleTest(&nargs,const_cast<char **>(args.data()));
return RUN_ALL_TESTS();
}
其中have_beeper() 是一个布尔函数,用于查询是否存在
蜂鸣器。