Code executed by exec excluded from coverage
See original GitHub issueOriginally reported by mb2297 (Bitbucket: mb2297, GitHub: Unknown)
It might be unrealistic to expect this to work, but python files that are read and then executed with exec (or with execfile) aren’t counted towards the coverage report.
Example:
#!python
# impl.py
def foo():
print "foo"
def bar():
print "bar"
#!python
# impl2.py
print "the answer to life, the universe and everything is: {}".format(undeclared_var)
#!python
# test.py
from impl import foo
foo()
namespace = {
'undeclared_var': 42
}
with open('impl2.py') as f:
code = compile(f.read(), 'impl2.py', 'exec')
exec(code, globals(), namespace)
Results in the following on both OS X and Ubuntu:
$ coverage run --source . test.py
foo
the answer to life, the universe and everything is: 42
$ coverage report
Name Stmts Miss Cover
---------------------------
impl 4 1 75%
impl2 1 1 0%
test 6 0 100%
---------------------------
TOTAL 11 2 82%
The example is also at https://gist.github.com/mattbennett/f57a5286bd77ee52651e if you want to clone and verify.
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
jacoco.exec coverage is not removing ...
I have added configuration with excluded files in jacoco maven plugin but coverage under jacoco.exec is not reflecting to exclude files.
Read more >Exclusions from Jacoco Report
In this tutorial, we'll learn how to exclude certain classes and packages from JaCoCo test coverage reports.
Read more >Excluding code from coverage.py - Read the Docs
Excluded code is executed as usual, and its execution is recorded in the coverage data as usual. When producing reports though, coverage.py excludes...
Read more >Executive Officers and Partners
In order to be excluded, the individual must execute a document, in writing and under penalty of perjury, waiving his or her rights...
Read more >Customizing Code Coverage Analysis - Visual Studio
If an assembly or member matches a clause in the Exclude section, then it is excluded from code coverage. The Exclude section takes...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Another note: I had code like
exec(fp.read())
which wasn’t working. I had to change it toexec(compile(fp.read(), filename, 'exec'))
for coverage to be recorded. I didn’t have to set__file__
.How can I include code executed with exec ? The links in this discussion are broken (bit bucket)