Reputation: 23
I'm writing some Test in C++ and I'm using gcov (actually lcov but I think it's beside the point) to get informations about coverage.
Is there any way to disable the information record at run-time? E.G. :
bool myTest() {
ObjectToTest obj;
/* Enable gcov... */
obj.FunctionToTest();
/* ...Disable gcov */
if(obj.GetStatus() != WHATEVER)
return false;
else
return true;
}
In this case I would like gcov to display as "covered" just FunctionToTest but leave ObjectToTest constructor and GetStatus "uncovered".
Thanks in advance!
Upvotes: 2
Views: 1897
Reputation: 333
Feasible solution now (2023),
void some_function() {
__gcov_reset(); // this will reset all profile counters
do_something();
__gcov_flush(); // this will flush all counters (note: this flush is incremental and will not overwrite existing profile data)
}
// To prevent other parts of profile data from being flushed
void __attribute__((destructor)) clear_redundant_gcov() {
__gcov_reset();
}
Additional explanation: when you compile your source code with gcov, gcc will insert a lot of API calls (such as __gcov_inc_counter(xxx), just an example), and it is possible to invoke these gcov API calls within your source code.
Upvotes: 0
Reputation: 5688
Although I agree with what @VikasTawniya said, you can also mock the functions you don't like to track in your test code.
#ifdev NO_COV
#include mock.h // mock of obj.FunctionToTest(); does nothing
#else
#include real.h // real implementation of obj.FunctionToTest();
#endif
Now your coverage result is not spoiled with the call of obj.FunctionToTest()
Upvotes: 0
Reputation: 1451
No, in case of gcov we don't have any such option.
I have seen such options in some coverage tools like clover, which works by instrumenting source code directly though.
Beside a solution to your problem will be to write that part of code into a different source file and then call it inside your desired source file by including it.
I am suggesting this because when you generate coverage report later using LCOV or GCOVR they both provide the option to exclude specified files from coverage report by passing them to certain switches.
LCOV:
-r tracefile pattern
--remove tracefile pattern
Remove data from tracefile.
GCOVR:
-e EXCLUDE, --exclude=EXCLUDE
Exclude data files that match this regular expression
Upvotes: 3