# Python coverage tool: Is there a mode to get only file to file coverage info?

**URL:** <https://discuss.python.org/t/python-coverage-tool-is-there-a-mode-to-get-only-file-to-file-coverage-info/25459>\
**Category:** Python Help\
**Created:** [April 4, 2023, 10:41pm UTC](https://discuss.python.org/t/python-coverage-tool-is-there-a-mode-to-get-only-file-to-file-coverage-info/25459 "2023-04-04T22:41:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![happyhebaby](https://avatars.discourse-cdn.com/v4/letter/h/779978/32.png) [@happyhebaby](https://discuss.python.org/u/happyhebaby)\
**Post date:** [April 4, 2023, 10:41pm UTC](https://discuss.python.org/t/python-coverage-tool-is-there-a-mode-to-get-only-file-to-file-coverage-info/25459/1 "2023-04-04T22:41:12Z")

</div>

@nedbat Just wonder if there is a mode to get only file to file coverage info instead of line level, so that the tool would run in a more performing way? In our scenario we only need file to file call relationship in dynamic analysis.

Thanks a lot for your help!

---

<div class="post-metadata">

**Author:** ![nedbat](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nedbat/32/8744_2.png) [@nedbat](https://discuss.python.org/u/nedbat)\
**Post date:** [April 4, 2023, 10:58pm UTC](https://discuss.python.org/t/python-coverage-tool-is-there-a-mode-to-get-only-file-to-file-coverage-info/25459/2 "2023-04-04T22:58:43Z")

</div>

Coverage.py doesn’t have a way to do that. How much overhead is coverage adding to your test suite? Make sure you have the C tracer, not the Python tracer. Use `coverage debug sys` and look for the “CTracer” line.

---

<div class="post-metadata">

**Author:** ![happyhebaby](https://avatars.discourse-cdn.com/v4/letter/h/779978/32.png) [@happyhebaby](https://discuss.python.org/u/happyhebaby)\
**Post date:** [April 4, 2023, 11:51pm UTC](https://discuss.python.org/t/python-coverage-tool-is-there-a-mode-to-get-only-file-to-file-coverage-info/25459/3 "2023-04-04T23:51:46Z")

</div>

Thanks for your prompt reply. I tried to use the tracer by:

1. Add the following to the config file:

```auto
[run]
plugins = CTracer

```

1. Run: `coverage run --rcfile=<localpath>/.coveragerc -m pytest`  
It emits error: `ModuleNotFoundError: No module named 'CTracer'`

What am I missing? thanks.

---

<div class="post-metadata">

**Author:** ![happyhebaby](https://avatars.discourse-cdn.com/v4/letter/h/779978/32.png) [@happyhebaby](https://discuss.python.org/u/happyhebaby)\
**Post date:** [April 5, 2023, 12:47am UTC](https://discuss.python.org/t/python-coverage-tool-is-there-a-mode-to-get-only-file-to-file-coverage-info/25459/4 "2023-04-05T00:47:29Z")

</div>

@nedbat If I may ask another question in the thread.  
If I run the coverage with `-m pytest' trying to run a group of pytest cases, the report result from `coverage result` will be the aggregated result for all these test cases. Is there a way I can separate the result by each file (group result by filename), we need to obtain coverage info in file level, so right now I have to run it by each file, this brought performance overhead that in worst case reach 60% of the original test run time.

---

<div class="post-metadata">

**Author:** ![nedbat](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nedbat/32/8744_2.png) [@nedbat](https://discuss.python.org/u/nedbat)\
**Post date:** [April 5, 2023, 1:21am UTC](https://discuss.python.org/t/python-coverage-tool-is-there-a-mode-to-get-only-file-to-file-coverage-info/25459/5 "2023-04-05T01:21:34Z")

</div>

Sorry, I’m not sure what you are seeing. Coverage.py never reports for each test case. `coverage report` (or `coverage html`) produce reports that list your files. You can run it once, and it will give you one report with a coverage percentage for each file. You are doing something very different, but I’m not sure what.

---

<div class="post-metadata">

**Author:** ![happyhebaby](https://avatars.discourse-cdn.com/v4/letter/h/779978/32.png) [@happyhebaby](https://discuss.python.org/u/happyhebaby)\
**Post date:** [April 5, 2023, 1:50am UTC](https://discuss.python.org/t/python-coverage-tool-is-there-a-mode-to-get-only-file-to-file-coverage-info/25459/6 "2023-04-05T01:50:08Z")

</div>

Let me clarify my scenario further: If I have test\_1.py test\_2.py under a folder, if I run coverage against pytest on that folder, it will run pytest on both files, and report on all files that get called by the two test, what I need is a report generate coverage on a per test base, something like:

```auto
For test_1.py:
file1.py 6 1 83% 
file3.py 2 1 10%
For test_2.py:
file2.py 1 1 15% 
file4.py 5 1 2%

```

Right now the tool generate something like the following, it doesn’t give each entry belongs to which test case:

```auto
file1.py 6 1 83% 
file3.py 2 1 10%
file2.py 1 1 15% 
file4.py 5 1 2%

```

---

<div class="post-metadata">

**Author:** ![PythonCHB](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pythonchb/32/2551_2.png) [@PythonCHB](https://discuss.python.org/u/PythonCHB)\
**Post date:** [April 5, 2023, 4:14am UTC](https://discuss.python.org/t/python-coverage-tool-is-there-a-mode-to-get-only-file-to-file-coverage-info/25459/7 "2023-04-05T04:14:17Z")

</div>

you can run pytest on a single test file – so you can get coverage for a single file:

`pytest --cov=my_package test_1.py`

or

`coverage run -m pytest test_1.py`

if you run it on another file, it will overwrite the first one, so you need to save it somehow first.

---

<div class="post-metadata">

**Author:** ![happyhebaby](https://avatars.discourse-cdn.com/v4/letter/h/779978/32.png) [@happyhebaby](https://discuss.python.org/u/happyhebaby)\
**Post date:** [April 6, 2023, 6:04am UTC](https://discuss.python.org/t/python-coverage-tool-is-there-a-mode-to-get-only-file-to-file-coverage-info/25459/8 "2023-04-06T06:04:54Z")

</div>

@PythonCHB Thanks for your reply.  
I understood that and already implemented a solution via that, but it’s performance heavy to need to run the tool on each file, especially for large repository where it has hundreds of test files. if I could run only one `pytest` cmd to kick off a run on all test and the result should be grouped by each file, then that’s the most desirable.

---

<div class="post-metadata">

**Author:** ![nedbat](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nedbat/32/8744_2.png) [@nedbat](https://discuss.python.org/u/nedbat)\
**Post date:** [April 8, 2023, 5:02pm UTC](https://discuss.python.org/t/python-coverage-tool-is-there-a-mode-to-get-only-file-to-file-coverage-info/25459/9 "2023-04-08T17:02:00Z")

</div>

Coverage.py includes a feature called dynamic contexts that can be used to record which tests ran which lines: [Measurement contexts — Coverage.py 7.2.3 documentation](https://coverage.readthedocs.io/en/7.2.3/contexts.html#dynamic-contexts) That can produce an HTML report which annotates each source line with the list of tests that ran the line.

That doesn’t give you a percentage for each test, but the data is in the .coverage data file, and so could possibly be used to get the reports you want with a single test suite run.

I’m curious though how you will use those reports? How many different reports will that produce?
