question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Code executed by exec excluded from coverage

See original GitHub issue

Originally 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:closed
  • Created 8 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
brondsemcommented, May 14, 2021

Another note: I had code like exec(fp.read()) which wasn’t working. I had to change it to exec(compile(fp.read(), filename, 'exec')) for coverage to be recorded. I didn’t have to set __file__.

0reactions
rohitsharma1113commented, Oct 19, 2022

How can I include code executed with exec ? The links in this discussion are broken (bit bucket)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found