无法使用lcov生成覆盖率报告 [英] Cannot generate coverage report using lcov

查看:1390
本文介绍了无法使用lcov生成覆盖率报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用lcov为我的单元测试套件生成覆盖率报告,但我甚至无法捕获跟踪文件。错误消息表明无法找到源文件。代码由构建机器上的Jenkins作业编译,单元测试作为目标机器上的下游作业执行。源代码和gcno文件被转移到下游作业,然后执行对lcov的调用。以下是所有细节,可能需要一杯咖啡。



在构建机器上,make在
中执行

  / var / lib / jenkins / workspace / App-Coverage / BUILD / app / 

我想要覆盖的源代码位于

  / var / lib / jenkins中的子目录中/ workspace / App-Coverage / BUILD / app / packages / 

生成目标文件和gcno文件在相对于相应cpp文件的子目录o中。例如

  / var / lib / jenkins / workspace / App-Coverage / BUILD / app / packages / subdir / Myclass。 cpp 
/var/lib/jenkins/workspace/App-Coverage/BUILD/app/packages/subdir/o/Myclass.o
/ var / lib / jenkins / workspace / App-Coverage / BUILD / app / packages / subdir / o / Myclass.gcno

源文件和gcno文件被复制到单元测试机器保持相同的文件夹结构并最终以

  / var / lib / jenkins / workspace / App-Coverage-Unittest / BUILD / app / 

注意:由于这两个Jenkins作业不能具有相同的工作空间文件夹名称App-Coverage-Unittest而不是App-Coverage名称。

所以现在有例如

  /var/lib/jenkins/workspace/App-Coverage-Unittest/BUILD/app/packages/subdir/Myclass.cpp 
/ var / lib / jenkins / workspace / App- Coverage-Unittest / BUILD / app / packages / subdir / o / Myclass.o
/var/lib/jenkins/workspace/App-Coverage-Unittest/BUILD/app/packages/subdir/o/Myclass.gcno

单元测试在

  / opt / app / test / app 
<使gcda文件出现在与gcno文件相同的文件夹中,例如

  / var / lib / jenkins / workspace / App-Coverage-Unittest / BUILD / app / packages / subdir / o / Myclass.gcno 
/ var / lib / jenkins / workspace / App-Coverage-Unittest / BUILD / app / packages / subdir / o / Myclass。 gcda

现在我想使用lcov生成覆盖率报告,但我似乎无法理解正确设置路径。以下示例在Jenkins单元测试作业中从/ var / lib / jenkins / workspace / App-Coverage-Unittest /执行。



例如,我试过

  lcov -d BUILD / app / packages / -c --no-external -o app.info -b / var / lib / jenkins / workspace / App-Coverage-Unittest / BUILD / app / 

推理:-d BUILD / app /包/是我想要覆盖的,-b / var / lib / jenkins / workspace / App-Coverage-Unittest / BUILD / app /是我执行make的项目的根目录(但在构建机器上用不同的工作区名称...)。

我也试过了

$ $ $ $ $ $ c> lcov -d BUILD / app / packages / --capture --no-external --output-file app.info

推理:-d BUILD / app / packages /是我想覆盖的范围,请不要设置-b,因为每个gcno / gcda和相应源文件之间的相对路径与构建时相同机器,也许lcov可以算出来。



在两者中案例会出现错误,如无法打开源文件/var/lib/jenkins/workspace/App-Coverage/BUILD/app/packages/subdir/Myclass.cpp
注意:此路径中的工作区文件夹是建立机器,而不是单元测试机器。我认为这是-b选项打算解决的问题。显然这是非常可疑的并且是一个有价值的线索。
我也遇到类似无法打开源文件../../../packages/subdir/Myclass.h这样的错误,我想这与我如何包含头文件有关。



我试过在这里指定所有的路径。是否有可能使用lcov在单元测试工作的工作空间中生成覆盖率报告,就像我在这里尝试的那样?如果是,哪些是指定lcov -d和-b标志的正确路径?如果没有,我需要改变什么才能使它工作?

解决方案

幸运的是答案是肯定的,这是可能的。我得到了一个lcov开发人员的回复,向我提供了解决方案,谢谢彼得!

他指出,所有的源代码路径在编译过程中都是硬编码的.gcno文件。然而,尽管没有找到源文件(并产生警告),但仅仅基于.gcda和.gcno文件中的数据,lcov将生成代码覆盖率输出,即使无法找到源代码。但是,genhtml步骤将失败,因为它无法找到源代码来使用代码覆盖率数据进行注释。



解决方案是使用lcov的geninfo_adjust_src_path配置设置。通过使用此设置,lcov被指示在编写输出.info文件时将.gcno文件中的源代码路径更改为正确的源代码路径。所以在我的情况下:

  lcov -d BUILD / app / packages / --capture --no-external --output-文件app.info 
--rc geninfo_adjust_src_path =/ var / lib / jenkins / workspace / App-Coverage / BUILD /
=> / var / lib / jenkins / workspace / App-Coverage-Unittest / BUILD /

当调用lcov时,警告无法打开源文件仍然存在,但生成的.info文件将包含正确的路径,因此可以使用genhtml在测试机器上转换为HTML。


I'm trying to use lcov to generate coverage reports for my unit test suite, but I cannot even capture a tracefile. The error messages indicate that the source files cannot be found. The code is compiled by a Jenkins job on a build machine and the unit test are executed as a downstream job on a target machine. The source code and gcno files are transfered to the downstream job, which then executes the call to lcov. Here follows all the details, a cup of coffee might be needed.

On the build machine, make is executed in

/var/lib/jenkins/workspace/App-Coverage/BUILD/app/

The source code which I want coverage for is in subdirectories in

/var/lib/jenkins/workspace/App-Coverage/BUILD/app/packages/

The object files and gcno files are generated in an subdirectory o relative to the corresponding cpp file. So for example

/var/lib/jenkins/workspace/App-Coverage/BUILD/app/packages/subdir/Myclass.cpp
/var/lib/jenkins/workspace/App-Coverage/BUILD/app/packages/subdir/o/Myclass.o
/var/lib/jenkins/workspace/App-Coverage/BUILD/app/packages/subdir/o/Myclass.gcno

The source files and gcno files are copied to the unit test machine keeping the same folder structure and ends up in

/var/lib/jenkins/workspace/App-Coverage-Unittest/BUILD/app/

Note: There is a difference in the name of the workspace folder, "App-Coverage-Unittest" instead of "App-Coverage" since these two Jenkins jobs cannot have the same name.

So there is now for example

/var/lib/jenkins/workspace/App-Coverage-Unittest/BUILD/app/packages/subdir/Myclass.cpp
/var/lib/jenkins/workspace/App-Coverage-Unittest/BUILD/app/packages/subdir/o/Myclass.o
/var/lib/jenkins/workspace/App-Coverage-Unittest/BUILD/app/packages/subdir/o/Myclass.gcno

The unit tests are executed in

/opt/app/test/app

Using GCOV_PREFIX_STRIP and GCOV_PREFIX I make the gcda files appear in the same folders as the gcno files, so for example

/var/lib/jenkins/workspace/App-Coverage-Unittest/BUILD/app/packages/subdir/o/Myclass.gcno
/var/lib/jenkins/workspace/App-Coverage-Unittest/BUILD/app/packages/subdir/o/Myclass.gcda

Now I want to generate a coverage report using lcov, but I don't seem to understand how to set the paths correctly. The following examples where executed from /var/lib/jenkins/workspace/App-Coverage-Unittest/ by the Jenkins unittest job.

For example I tried

lcov -d BUILD/app/packages/ -c --no-external -o app.info -b /var/lib/jenkins/workspace/App-Coverage-Unittest/BUILD/app/

Reasoning: "-d BUILD/app/packages/" is what I want coverage for, "-b /var/lib/jenkins/workspace/App-Coverage-Unittest/BUILD/app/" is the root of my project in which I executed make (but on the build machine with a different workspace name...).

I also tried

lcov -d BUILD/app/packages/ --capture --no-external --output-file app.info

Reasoning : "-d BUILD/app/packages/" is what I want coverage for, don't set -b since relative path between each gcno/gcda and corresponding source file is the same as on the build machine, maybe lcov can figure it out.

In both cases get errors like "Cannot open source file /var/lib/jenkins/workspace/App-Coverage/BUILD/app/packages/subdir/Myclass.cpp" Note: The workspace folder in this path is that of the build machine, not the unittest machine. I thought that this is what the -b option is intended to solve. Clearly this is very suspicious and a valuable clue. I also get errors like "Cannot open source file ../../../packages/subdir/Myclass.h", which I guess has to do with how I include header files.

I have tried specifying all the paths here. Is it possible to generate the coverage report in the workspace of the unittest job using lcov, like I'm trying to do here? If yes, which are the correct paths to specify for lcov -d and -b flags? If not, what do I need to change to make it work?

解决方案

Fortunately the answer is yes, it is possible. I got a reply from an lcov dev providing me with the solution, thank you Peter!

He pointed out that all source code paths are hard-coded during the compile step into the .gcno files. However, despite not finding the source files (and producing the warnings) lcov will generate code coverage output even when the source code cannot be found, based solely on the data found in .gcda and .gcno files. However, the genhtml step will fail because it won't be able to find the source code to annotate with code coverage data.

The solution is to use lcov's "geninfo_adjust_src_path" configuration setting. By using this setting, lcov is instructed to change source code paths as found in the .gcno files into the correct source code paths while writing the output .info files. So in my case:

lcov -d BUILD/app/packages/ --capture --no-external --output-file app.info
     --rc geninfo_adjust_src_path="/var/lib/jenkins/workspace/App-Coverage/BUILD/ 
     => /var/lib/jenkins/workspace/App-Coverage-Unittest/BUILD/"

The warnings "Cannot open source file" will still be there when invoking lcov, but the resulting .info file will contain the correct paths and can therefore be converted to HTML on the test machine using genhtml.

这篇关于无法使用lcov生成覆盖率报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆