Open thaiminhpv opened 1 month ago
Coverage.py doesn't directly provide the information you want, but it can help. Use the dynamic contexts feature to record lines separately for each test. Then you'll have to map the line numbers back to functions. The contexts_by_lineno should be helpful. Let us know what you come up with.
I am wondering if this is even possible or not by using
coveragepy
(orpytest-cov
).I want to get a mapping dictionary
dict[str, set[str]]
from a function to corresponding test functions from the execution trace by runningpytest
on the source code.The name of function and the name of test might be in
pytest
nodeid
format (e.g.module_abc.xyz::ClassA::test_method
)Example of what I expect
What I have tried
I tried to look at how
coverage.py
usesys.settrace
to record the trace, but it is quite complex. I see the documentation also say that the trace is dump to.coverage
sqlite database. But I do a simple test I find nothing useful in the dumped db.I also tried to implement a pytest plugin using
sys.settrace
, but thesetup
andteardown
part have different fixture scope and quite complex, because I want to includesetup
andteardown
functions also. I think the problem is also solved if we can get thesetup
andteardown
function given thetest case
.But as I currently understand (PLEASE CORRECT ME IF I AM WRONG) it seems like
coverage.py
only collect information is about "line coverage" (or arcs for "branch coverage"), and the information about each execution session got discarded, so the dumped database is not helpful.Am I correct? How can I get a mapping from
pytest nodeid
tofunction nodeid
, based on thecoverage.py
dumped trace output?