C++ 如何测量 CppUnit 测试覆盖率(在 win32 和 Unix 上)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:21:35  来源:igfitidea点击:

How can I measure CppUnit test coverage (on win32 and Unix)?

c++unit-testingcross-platformcode-coveragecppunit

提问by Thomi

I have a very large code base that contains extensive unit tests (using CppUnit). I need to work out what percentage of the code is exercised by these tests, and (ideally) generate some sort of report that tells me on a per-library or per-file basis, how much of the code was exercised.

我有一个非常大的代码库,其中包含大量的单元测试(使用 CppUnit)。我需要计算出这些测试执行了多少代码的百分比,并且(理想情况下)生成某种报告,以每个库或每个文件为基础告诉我执行了多少代码。

Here's the kicker: this has to run completely unnatended (eventually inside a continuous integration build), and has to be cross platform (well, WIN32 and *nix at least).

关键是:这必须完全无人值守地运行(最终在持续集成构建中),并且必须是跨平台的(至少是 WIN32 和 *nix)。

Can anyone suggest a tool, or set of tools that can help me do this? I can't change away from CppUnit (nor would I want to - it kicks ass), but otherwise I'm eager to hear any recommendations you might have.

任何人都可以建议可以帮助我做到这一点的工具或工具集吗?我不能改变 CppUnit(我也不想 - 它踢屁股),但除此之外我很想听到你可能有的任何建议。

Cheers,

干杯,

采纳答案by Justin Standard

Which tool should I use?

我应该使用哪种工具?

This article describesanother developers frustrations searching for C++ code coverage tools. The author's final solution was Bullseye Coverage.

文章描述了另一开发商挫折搜索为C ++代码覆盖工具。作者最终的解决方案是Bullseye Coverage

Bullseye Coveragefeatures:

Bullseye 覆盖功能:

As for hooking into your continuous integration, it depends on which CI solution you use, but you can likely hook the instrumentation / coverage measurement steps into the make file you use for automated testing.

至于挂钩到您的持续集成,这取决于您使用的 CI 解决方案,但您可能可以将检测/覆盖率测量步骤挂钩到用于自动化测试的 make 文件中。



Testing Linux vs Windows?

测试 Linux 与 Windows?

So long as all your tests run correctly in both environments, you should be fine measuring coverage on one or the other. (Though Bullseye appears to support both platforms). But why aren't you doing continuous integration builds in both environments?? If you deliver to clients in both environments then you needto be testing in both.

只要您的所有测试在两种环境中都能正确运行,您就应该可以很好地测量其中一种环境的覆盖率。(尽管 Bullseye 似乎支持两个平台)。但是为什么不在这两种环境中进行持续集成构建?如果您在两种环境中都向客户交付,那么您需要在两种环境中进行测试。

For that reason, it sounds like you might need to have two continuous build servers set up, one for a linux build and one for a windows build. Perhaps this can be easily accomplished with some virtualization software like vmwareor virtualbox. You may not need to run code coverage metrics on both OSs, but you should definitely be running your unit tests on both.

出于这个原因,听起来您可能需要设置两个连续构建服务器,一个用于 linux 构建,另一个用于 Windows 构建。也许这可以通过一些虚拟化软件(如vmwarevirtualbox )轻松实现。您可能不需要在两个操作系统上运行代码覆盖率指标,但您绝对应该在两个操作系统上运行单元测试。

回答by Imbue

If you can use GNU GCCas your complier, then the gcovtool works well. It's very easy to fully automate the whole process.

如果您可以使用GNU GCC作为您的编译器,那么gcov工具运行良好。使整个过程完全自动化非常容易。

回答by Jeremy Mayhew

If you are using the GCC toolchain, gcov is going to get you source, functional, and branch coverage statistics. gcov works fine for MinGW and Cygwin. This will allow you to get coverage statistics as well as emitting instrumented source code that allows you to visualize unexecuted code.

如果您使用 GCC 工具链,gcov 将为您提供源代码、功能和分支覆盖率统计信息。gcov 适用于 MinGW 和 Cygwin。这将允许您获取覆盖率统计信息以及发出检测源代码,使您可以可视化未执行的代码。

However, if you really want to hit it out of the park with pretty reports, using gcov in conjunction with lcovis the way to go. lcov will give you bar reports scoped to files and directories, functional coverage statistics, and color coded source file browsing to show coverage (green means executed, red means not...).

但是,如果您真的想通过漂亮的报告将其击出公园,那么将 gcov 与lcov结合使用是不错的选择。lcov 将为您提供范围为文件和目录的条形报告、功能覆盖率统计数据和颜色编码的源文件浏览以显示覆盖率(绿色表示已执行,红色表示未...)。

lcov is easy on Linux, but may require some perl hacking on Cygwin. I personally have had some problems executing the scripts (lcov is implemented in perl) on Windows. I've gotten a hacked up version to work, but be forewarned.

lcov 在 Linux 上很容易,但在 Cygwin 上可能需要一些 perl hacking。我个人在 Windows 上执行脚本时遇到了一些问题(lcov 是在 perl 中实现的)。我有一个破解版可以工作,但要预先警告。

Another approach is doing the gcov emit on windows, and doing the lcov post processing on Linux, where it will surely work out of the box.

另一种方法是在 Windows 上进行 gcov 发射,并在 Linux 上进行 lcov 后处理,它肯定可以开箱即用。

回答by Ira Baxter

Check out our SD C++ Test Coveragetool. It can be obtained for GCC, and for MSVC6.

查看我们的SD C++ 测试覆盖率工具。它可以为 GCC 和 MSVC6 获得。

It has low overhead probe data collection, a nice display of coverage data overlayed on your code, and complete report generation with rollups on coverage across the method/class/file/directory levels.

它具有低开销的探测数据收集、覆盖在您的代码上的覆盖数据的良好显示,以及完整的报告生成以及跨方法/类/文件/目录级别的覆盖范围汇总。

EDIT: Aug 2015: Now supports GCC5 and various MS dialects through Visual Studio 2015. To use these tools under Linux, you need Wine, but there the tools provide Linux-native sh scripting and a Linux/Java based UI, so the tool feels like a native Linux tool there.

编辑:2015 年 8 月:现在通过 Visual Studio 2015 支持 GCC5 和各种 MS 方言。要在 Linux 下使用这些工具,您需要 Wine,但这些工具提供 Linux 原生 sh 脚本和基于 Linux/Java 的 UI,因此该工具感觉就像那里的原生 Linux 工具。

回答by Thomi

I guess I should have specified the compiler - we're using gcc for Linux, and MSVC 6 (yeah I know, it's old, but it works (mostly) for us) for WIn32.

我想我应该指定编译器 - 我们在 Linux 上使用 gcc,而 MSVC 6(是的,我知道,它很旧,但它(大部分)对我们有用)用于 WIn32。

For that reasons, gcov won't work for our Win32 builds, and Bullseye won't work for our Linux builds.

出于这个原因,gcov 不适用于我们的 Win32 版本,而 Bullseye 不适用于我们的 Linux 版本。

Then again maybe I only need coverage in one OS...

再说一次,也许我只需要在一个操作系统中进行覆盖...