我正在使用lcov2.0检查我的测试分支覆盖率,遇到了这个问题。
// source code
void test_string_plus(const string& local ,const string& remote)
{
static string recv_msg;
static_cast<void>(recv_msg.assign("/" + (local < remote ? local + "_" + remote : remote + "_" + local)));
}
// test code
TEST(teststring, teststringplus)
{
string local = "test1";
string remote = "test12";
EXPECT_NO_THROW(test_string_plus(local, remote));
local = "test21";
remote = "test2";
EXPECT_NO_THROW(test_string_plus(local, remote));
}
字符串
它在lcov中报告如下
281 : 2 : void test_string_plus(const string& local ,const string& remote)
282 : : {
283 : 2 : static string recv_msg;
284 [ + + + - : 2 : static_cast<void>(recv_msg.assign("/" + (local < remote ? local + "_" + remote : remote + "_" + local)));
+ - + - +
- + - + +
+ + - - -
- ]
285 : 2 : }
型
gcov文件显示,这段代码包含了大量未覆盖的throw分支。
2: 284: static_cast<void>(recv_msg.assign("/" + (local < remote ? local + "_" + remote : remote + "_" + local)));
call 0 returned 2
branch 1 taken 1 (fallthrough)
branch 2 taken 1
call 3 returned 1
branch 4 taken 1 (fallthrough)
branch 5 taken 0 (throw)
call 6 returned 1
branch 7 taken 1 (fallthrough)
branch 8 taken 0 (throw)
call 9 returned 1
branch 10 taken 1 (fallthrough)
branch 11 taken 0 (throw)
call 12 returned 1
branch 13 taken 1 (fallthrough)
branch 14 taken 0 (throw)
call 15 returned 2
branch 16 taken 2 (fallthrough)
branch 17 taken 0 (throw)
call 18 returned 2
call 19 returned 2
call 20 returned 2
branch 21 taken 1 (fallthrough)
branch 22 taken 1
call 23 returned 1
branch 24 taken 1 (fallthrough)
branch 25 taken 1
call 26 returned 1
call 27 never executed
branch 28 never executed
branch 29 never executed
call 30 never executed
branch 31 never executed
branch 32 never executed
call 33 never executed
2: 285:}
型
我使用的工具版本和命令。
- g++/gcov(Ubuntu 11.4.0-1ubuntu1~22.04)11.4.0
- gtest 1.12.x
- lcov/genhtml:LCOV版本2.0-1
g++ -std=c++17 test.cpp -o test -lgtest -lgtest_main -pthread -fprofile-arcs -ftest-coverage -fprofile-update=atomic && \
./test && \
gcov -b -c -o . test.cpp && \
lcov --capture \
--rc branch_coverage=1 \
--directory . \
--filter branch \
--output-file coverage_all.info \
--ignore-errors mismatch && \
genhtml coverage_all.info \
--rc branch_coverage=1 \
--output-directory coverage_report && \
rm *.info
型
我已经使用--filter branch
忽略了一些无法访问的分支
我应该如何减少这里未覆盖的分支?或者我可以忽略它们吗?
https://legacy.cplusplus.com/reference/string/string/operator+/
我在std::string中检出了operator+()
的引用,似乎只有在尝试添加超过长度限制的字符串或者内存应用失败时才会抛出异常,但在实际项目中,构造这样的测试条件是非常不合理的。
我试着修改?:
为if else,那些由string引起的分支可以被过滤。
281 : 2 : void test_string_plus(const string& local ,const string& remote)
282 : : {
283 : 2 : static string recv_msg;
284 : 2 : string target("/");
285 [ + + ]: 2 : if(local<remote)
286 : : {
287 : 1 : target += local + "_" + remote;
288 : : }
289 : : else{
290 : 1 : target += remote + "_" + local;
291 : : }
292 : 2 : static_cast<void>(recv_msg.assign(target));
型
1条答案
按热度按时间d4so4syb1#
我试着修改
?:
为if else,那些由字符串引起的分支可以通过lcov--filter branch
选项过滤字符串