Open jpo38 opened 3 years ago
I realize I have the test program ( main
) be part of excluded_modules
list.
Looks like coverage is reported correctly if the test program is not in excluded_modules
(that's due to templates being compiled on the fly). But then, test program itself is part of the report, which is not expected, I want the report for my library code only.
Is there a way to have this work as expected? (have only the shared library code in the report and have it be marked as fully covered).
The abstract template code in the shared library is not executed; it's only the instantiations that are, so what you have observed is the expected behaviour. One instantiation takes one path, the other is excluded from coverage so the fact that it takes the second path is not observed.
One approach to getting coverage of an instantiation, when the defining library has none, would be to have an essentially trivial shim library, from which coverage is gathered, between the unit tests and the defining one, which just does the necessary instantiation, like
class TemplateShims
{
public:
static void MyClass_templatefunc_uint8_t(uint8_t n)
{
MyClass::templatefunc<uint8_t>(n);
}
}
and call that from the test, like
#include "coverage.h"
int main( int argc, char* argv[] )
{
MyClass::testfunc();
TemplateShims::MyClass_templatefunc_uint8_t( 7 );
return 0;
}
OK, thank you for the explanation,
Jean
I'm observing something similar to this. The calling code for the template is showing up in the included modules, but for some reason, it still marks it as not covered, despite me knowing it was called.
To Reproduce Create a shared library:
coverage.h:
coverage.cpp:
Then, your main test program:
I then compile in Debug (using Visual Studio 2019), run OpenCppCoverage 0.9.9 with no particular arguments (but
export_type
orsources/exclude_sources
).Output it:
Expected behavior
MyClass::templatefunc
should be fully covered (if
statement covered as called bytestfunc()
called bymain
,else
statement covered as called directly bymain
). However,std::cout << "KO" << std::endl;
is marked as uncovered (while program output showsOK
andKO
, so it's definitely covered).Desktop (please complete the following information):