1

I have a python program that imports other files which potentially import other files, as is normal with python development

The problem is, when I measure coverage with coverage.py, some files which are imported but not used, get coverage "hits" on the def and import statements.

My question is: is there are way to avoid those hits? For my particular application these hits are considered noise.

6
  • Why are you importing files that aren't used? Commented Feb 13, 2018 at 22:00
  • @jwodder this is a very big system and which I have no control of. my assignment is is to write a application which show coverage of the system in a smart way Commented Feb 13, 2018 at 22:05
  • Then perhaps you should leave coverage.py's results alone so that the coverage hits from the unused imports come to the attention of someone who can fix the imports. That's what the coverage report is for, right? Commented Feb 13, 2018 at 22:09
  • @jwodder its not that simple. my assignment is to provide the best tools that I can for other people to use at their own schedule. Commented Feb 13, 2018 at 22:21
  • Coverage.py has no idea why lines were run, only that they ran. I recommend that you not worry about the lines that are run by importing files that aren't used. They add to the total, but the difference between 75% and 77% is meaningless anyway, except that 77 is more than 75. Why is it so important to ignore lines run by files that are imported but not used? Commented Feb 14, 2018 at 22:45

3 Answers 3

2

You can use the exclude_lines configuration, as per documentation https://coverage.readthedocs.io/en/coverage-4.2/excluding.html#advanced-exclusion.

For example (quoting from the link pasted above):

[report] exclude_lines = pragma: no cover def __repr__ if self.debug: if settings.DEBUG raise AssertionError raise NotImplementedError if 0: if __name__ == .__main__.:

Sign up to request clarification or add additional context in comments.

Comments

1

Coverage.py has controls for what files to include, and which to omit: http://coverage.readthedocs.io/en/coverage-4.5.1/source.html You can tailor which files it will measure.

2 Comments

yes I know that. but that does not solves my problem as it removes files based on names. I want to remove individual hits based on the hit nature.
I don't know what "hit nature" means.
1

Since coverage.py does not provide this feature, my solution was to write a small ast based function that calculate ghost hit points and remove them from the the coverage.py results

Edit (after 5 years), here is the relevant code:

import ast
def calc_ghost_hits(text):
    def calc_args(node):        
        args=node.args
        if args:
            args=args.args
        if args:
            for arg in args:
                yield arg.lineno    
    def calc_decorators(node):
        for x in calc_args(node): yield x
        for decorator in node.decorator_list:
            yield decorator.lineno
        returns=node.returns
        if returns:
            yield returns.lineno
        
    def calc_it(branch,level):
        for x in ast.iter_child_nodes(branch):
            lineno=x.lineno
            if lineno is not None:
                yield lineno           
            t=type(x).__name__
            if t=='ClassDef' and level==0:
                for x in calc_it(x,level+1):
                    yield x
            if t=='FunctionDef' and level<=1:
                for x in calc_decorators(x):
                    yield x
    return list(set(calc_it(ast.parse(text),0)))

4 Comments

Awesome. @yigal can you share it?
Hi @yigal , I'm looking for similar solution , can you share your function
@yigal - Would you be able to share the code for your solution. Thanks.
edit: I just added the requested code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.