Skip to content

Misleading subprocess coverage with relative path --cov #282

@ndevenish

Description

@ndevenish

If --cov is passed relative, not absolute, and a subprocess is run in a different directory, then the subprocess coverage will be silently ignored. Example:

test_subprocess_coverage.py

import os
import subprocess
import sys


def do_something():
    print("A test in {}".format(os.getcwd()))


def test_subprocess_coverage(tmp_path):
    apy = os.path.abspath(__file__)
    print("Calling {}".format(apy))
    # Call this same script externally from the temporary path
    subprocess.call([sys.executable, apy, "--do"], cwd=str(tmp_path))


if __name__ == "__main__":
    if "--do" in sys.argv:
        do_something()

Output:

$ pytest test_subprocess_coverage.py --cov=.
test_subprocess_coverage.py      12      3    75%

$ pytest test_subprocess_coverage.py --cov=$(pwd)
test_subprocess_coverage.py      12      0   100%

I think I understand the reasons why, that the path is passed through in the environment relative so the child coverage has no idea what is being checked - but the fact that it's silently failing has bitten us several times. (instantiating child jobs as in-process function calls is something we are slowly working towards, but is a long ways off).

I'd imagine that just making the environment variable absolute (guessing here) would solve this for my case, but imagine might cause issues with cluster-distributed testing where the code might have been copied somewhere different? In which case maybe there is a similar bug when cluster-distributed and passing in an absolute path....

Edit: A slightly similar issue seems to have been dealt with for the .coveragerc file in #94/#95

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions