coverage.py how to exclude contexts

I run different coverage tests each with a different context.
After combining them I would like to exclude some contexts from an html report.
From my small experience and understanding of the documentation, the context option is only used for filtering the run lines. I would like a way to use contexts to filter on the statement lines.

Options:
  --contexts=REGEX1,REGEX2,...
                        Only display data from lines covered in the given
                        contexts. Accepts Python regexes, which must be
                        quoted.

I am now looking at the Coverage.py API to find a solution
@nedbat

I don’t understand what this means. When lines are run, the context is noted. When reporting, you can choose which contexts to include to mark lines as having been run. What does it mean to filter the statement lines? A line in your Python file is either a statement or it isn’t. The context doesn’t change that.

Can you show a concrete example of the result you would like?

Many thanks for taking into consideration my question, for creating this great package and sorry for my bad explanations.

My real concern is how can I know what proportion of code that I really use is tested.

I thought I could do that by running coverage.py on my “used-in-production” code with a context “used-in-production”, then run coverage.py on my tests with a context “tested” and finally combine the results for use by coverage htmlcov
Reading at the documentation of coverage htmlcov, I misinterpreted the word “display” in

  --contexts=REGEX1,REGEX2,...
                        Only display data from lines covered in the given
                        contexts. Accepts Python regexes, which must be
                        quoted.

as “redefine statements lines as statement lines in the given context” in the htmlcov whereas form my later understanding it meant “redefine covered lines as covered lines in the given context”

I hoped I could get either of the following behaviour:

  1. Only lines from used-in-production_context are shown in the htmlcov
  2. Lines not in used-in-production_context are counted as excluded in the htmlcov

Here is an example for a file.py:

def used_in_production_but_untested():
    return

def used_in_production_and_tested():
    return

def unused_in_production_and_untested():
    return

def unused_in_production_and_tested():
    return

Desired behaviour 1, in htmlcov:

def used_in_production_but_untested():
     return  # missed

def used_in_production_and_tested():
     return  # covered

Desired behaviour 2, in htmlcov:

def used_in_production_but_untested():
    return  # missed

def used_in_production_and_tested():
    return  # covered

def unused_in_production_and_untested():  # excluded
    return  # excluded

def unused_in_production_and_tested():  # excluded
    return  # excluded

I hope this is clearer like this
Thank you

And in a more general way I was interested by the possibility of changing the “denominator” of the coverage results (being statements - excluded from my understanding) based on for instance covered lines with a given context

I’m trying to picture how this might work. How do you determine or indicate what code is used in production?

I run a python script with « production » configurations. While I run another phthon script for testing. My idea was to combien the 2 .coverage while keeping the origin of thoses coverage by using contexts. And then do something like « coverage htmlcov —include-only-context=production —cover-context=testing ».

This isn’t something that coverage.py can do. You might be able to dig into the data in the database, but I don’t have a way for you to tell coverage what the possible statements were (that’s your “used in production” code).

Ok, thank you, I will try to look at the database or find another solution