简体   繁体   English

SWIG编译的C ++ / Python代码的测试覆盖率

[英]Test coverage from SWIG-compiled C++/Python code

I have a classical SWIG setup where a bunch of C++ function are called from Python. 我有一个经典的SWIG设置,其中从Python调用一堆C ++函数。 Now, after adding some tests (in Python), I'd like to get test coverage on the C++ sources. 现在,在添加一些测试(在Python中)之后,我想在C ++源代码上获得测试覆盖率。 The classical Python method is 经典的Python方法是

nosetests --with-coverage --cover-package=mypackage

but that only works for native Python modules. 但这只适用于本机Python模块。 In fact, this will return the coverage on the mypackage.py file generated by SWIG. 实际上,这将返回SWIG生成的mypackage.py文件的覆盖范围。

Since I need coverage on the C++ files (/the shared library generated by SWIG), adding --coverage to compile and linker flags is certainly called for. 由于我需要覆盖C ++文件(/ SWIG生成的共享库),因此当然需要添加--coverage来编译和链接器标志。 Not sure where to go from there though. 不知道从那里去哪里。

Any hints? 任何提示?

Faced with a similar issue, but with C code. 面对类似的问题,但用C代码。 It's not related to the SWIG part, just need to compile your C++ code with the coverage instrumentation and execute it. 它与SWIG部分无关,只需要使用coverage工具编译C ++代码并执行它。 I used gcov and gcovr for that. 我使用gcovgcovr

With gcov , to make this work, compile the code with the following flags (enables gcov instrumentation) 使用gcov来完成这项工作,使用以下标志编译代码(启用gcov检测)

CFLAGS_VAL += -O0 --coverage CFLAGS_VAL + = - O0 - 覆盖

Then, execute the test. 然后,执行测试。 Following the test run, .gcno and .gcda files should be generated. 测试运行后,应生成.gcno和.gcda文件。

To create the coverage report, run from your root folder 要创建覆盖率报告,请从根文件夹运行

gcovr -r . gcovr -r。 --filter="" --html --html-details -o coverage/coverage.html --filter =“” - html --html-details -o coverage / coverage.html

GCOV docs, here GCOV docs, 这里

Same can create with lcov, follow an example in this wiki page 可以使用lcov创建相同的内容,请按照此Wiki页面中的示例进行操作

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

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