通过SWIG编译的C ++/Python代码测试覆盖范围 [英] Test coverage from SWIG-compiled C++/Python code
问题描述
我有一个经典的SWIG设置,其中从Python调用了一堆C ++函数.现在,在添加了一些测试(在Python中)之后,我想了解C ++源代码的测试范围.经典的Python方法是
I have a classical SWIG setup where a bunch of C++ function are called from Python. Now, after adding some tests (in Python), I'd like to get test coverage on the C++ sources. The classical Python method is
nosetests --with-coverage --cover-package=mypackage
,但这仅适用于本机Python模块.实际上,这将返回SWIG生成的mypackage.py
文件的覆盖率.
but that only works for native Python modules. In fact, this will return the coverage on the mypackage.py
file generated by SWIG.
由于我需要覆盖C ++文件(/SWIG生成的共享库),因此肯定需要添加--coverage
到编译和链接器标志.虽然不知道从那里去.
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. Not sure where to go from there though.
有任何提示吗?
推荐答案
遇到了类似的问题,但使用了C代码.它与SWIG部分无关,只需要使用coverage工具编译您的C ++代码并执行它即可.我为此使用了gcov
和gcovr
.
Faced with a similar issue, but with C code. It's not related to the SWIG part, just need to compile your C++ code with the coverage instrumentation and execute it. I used gcov
and gcovr
for that.
使用gcov
,要执行此操作,请使用以下标志编译代码(启用gcov检测)
With gcov
, to make this work, compile the code with the following flags (enables gcov instrumentation)
CFLAGS_VAL + = -O0-覆盖率
CFLAGS_VAL += -O0 --coverage
然后执行测试.在测试运行之后,应生成.gcno和.gcda文件.
Then, execute the test. Following the test run, .gcno and .gcda files should be generated.
要创建覆盖率报告,请从您的根文件夹运行
To create the coverage report, run from your root folder
gcovr -r. --filter =" --html --html-details -o coverage/coverage.html
gcovr -r . --filter="" --html --html-details -o coverage/coverage.html
GCOV文档,此处
同一人可以使用lcov创建,请按照 Wiki 页面
Same can create with lcov, follow an example in this wiki page
这篇关于通过SWIG编译的C ++/Python代码测试覆盖范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!