简体   繁体   English

如何测量 C++ 中执行部分的代码覆盖率

[英]How to measure code coverage of the part of execution in C++

I have two C++ binaries (A and B) with socket connections between two binaries.我有两个 C++ 二进制文件(A 和 B),两个二进制文件之间有套接字连接。
Binary A is listening for B. And, B connects to the A.二进制 A 正在监听 B。并且,B 连接到 A。

A accepts B's message and executes some functions depending on message contents. A 接受 B 的消息并根据消息内容执行一些功能。
We created a kind of unit-test to test various message types.我们创建了一种单元测试来测试各种消息类型。

From these tests, I would like to measure A's executed line from our test messages.从这些测试中,我想从我们的测试消息中测量 A 的执行行。
I searched for tools to measure code coverage, and I found lcov .我搜索了测量代码覆盖率的工具,发现了lcov

However, lcov measured total executed lines of code including initialization and other useless chunks.然而,lcov 测量了总执行的代码行数,包括初始化和其他无用的块。
I may create a new code to test functionality without socket connection.我可能会创建一个新代码来测试没有套接字连接的功能。
But the code structure is complex, so it takes very long time to implement tests.但是代码结构复杂,所以实现测试需要很长时间。

So, I would like to skip those lines in lcov to obtain purely executed lines caused by our test messages .所以,我想跳过lcov中的那些行,以获得由我们的测试消息引起的纯粹执行的行

If it is impossible and there is a better tool for this situation, please recommend suitable tools to measure this.如果不可能并且有更好的工具来解决这种情况,请推荐合适的工具来衡量。

Thanks.谢谢。

Call __gcov_reset before your testcases to discard the data collected from initialization.在测试用例之前调用__gcov_reset以丢弃从初始化收集的数据。

It is important to understand that "coverage" is a set of source code locations.重要的是要了解“覆盖”是一源代码位置。 You can do "arithmetic" on sets: union ("add"), complement, difference ("subtract").你可以在集合上做“算术”:联合(“加”)、补、差(“减”)。

The key is to get a tool that will (set)subtract test coverage data sets.关键是要获得一个可以(设置)减去测试覆盖率数据集的工具。

Then you collect test coverage for starting/stopping your program.然后你收集测试覆盖率来启动/停止你的程序。 This covers initialization and setup.这包括初始化和设置。 Call this set I.称这组为 I。

Now collect test coverage for your program executing the activities of interest, eg, the "communication" part.现在为您的程序执行感兴趣的活动收集测试覆盖率,例如“通信”部分。 Call this set X.将此集合称为 X。

What you want is the set computed by subtracting I from X: "XI" in set notation.您想要的是通过从 X 中减去 I 计算得出的集合:集合符号中的“XI”。

This means you need a tool that will let you collect these sets and do this computation.这意味着您需要一个工具来收集这些集合并进行计算。

I'm sure you can collect X and I with lcov.我相信你可以用 lcov 收集 X 和 I。 But I don't think lcov gives you a way to compute the set difference.但我不认为 lcov 给你一种计算集合差异的方法。 If it does, bingo, you're in business.如果是这样,宾果游戏,你在做生意。

If it doesn't, you might consider using my company's test coverage tools.如果没有,您可以考虑使用我公司的测试覆盖率工具。 They provide an explicit means to collect test coverage sets, and do arbitrary set computations, eg, set unions, set complements, set differencing, in the user interface.它们提供了一种明确的方法来收集测试覆盖集,并在用户界面中进行任意集计算,例如集并集、集补集、集差分。 You don't need to change your code to do this.您无需更改代码即可执行此操作。

See http://www.semdesigns.com/Products/TestCoverage/http://www.semdesigns.com/Products/TestCoverage/

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

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