0
votes

So, I have a project tested using pytest and using the pytest-cov module to report coverage.

Some of the code involves bootstrapping and configuration and the canonical way of running this is starting via a shell script. The current unit tests use the subprocess module to test running this shell script on mocked data. I would like the code report against the coverage and I am trying specifically to avoid

1) Heavily modifying the wrapper to support the test scenario. Also, this runs the risk of doing 2).

2) Running the boostrap code outside the wrapper (e.g. by forking the process and running the code directly), since I want these tests to be as realistic as possible.

Is there any (canoical, Pythonic) way of propagating the coverage collection to all subproceses, even when launched using subprocess.Popen? I can easily solve the problem using a hack, so this is not something I am looking for.

1
There's a chapter in the docs devoted to this: coverage.readthedocs.io/en/coverage-5.1/subprocess.html - Ned Batchelder
The sitecustomize method was New to me and could work. I’ll try it out. Thanks! - Krumelur
Feel free to summarize the above link as an answer, and I'll accept it. Otherwise I put my solution that I adapted from that link as the answer - Krumelur
Actually, I realize this is built into pytest-cov but is broken for some reason. Probably something in my Docker setup - Krumelur

1 Answers

0
votes

This actually works out of the box. The reason for me thinking this did not work was that the path mappings w.r.t. the Docker volumes were incorrect, as the modules loaded by the subprocess were bind mounted in the container. Coverage is only reported when the paths match up exactly.