Skip to content

Instantly share code, notes, and snippets.

@mattbennett
Last active March 4, 2020 05:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mattbennett/0086536d500894448167 to your computer and use it in GitHub Desktop.
Save mattbennett/0086536d500894448167 to your computer and use it in GitHub Desktop.
coverage subprocess
[run]
branch = True
source = .
def foo():
return "foo"
def bar():
return "bar"
import coverage
coverage.process_startup()
from impl import foo, bar
def test_foo():
assert foo() == "foo"
def test_bar():
assert bar() == "bar"
import subprocess
import os
os.environ['COVERAGE_PROCESS_START'] = ".coveragerc"
def test_via_subproc():
proc = subprocess.Popen(["py.test", "subproc_test.py", "-v"])
proc.wait()
@mattbennett
Copy link
Author

$ PYTHONPATH=. coverage run -p --source . -m pytest -s -v test.py && coverage combine && coverage report
============================= test session starts ==============================
platform darwin -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2 -- /Users/mattbennett/.virtualenvs/tmp-ad23438d009e56b5/bin/python2.7
rootdir: /Users/mattbennett/.virtualenvs/tmp-ad23438d009e56b5/0086536d500894448167, inifile:
collected 1 items

test.py::test_via_subproc ============================= test session starts ==============================
platform darwin -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2 -- /Users/mattbennett/.virtualenvs/tmp-ad23438d009e56b5/bin/python2.7
rootdir: /Users/mattbennett/.virtualenvs/tmp-ad23438d009e56b5/0086536d500894448167, inifile:
collected 2 items

subproc_test.py::test_foo PASSED
subproc_test.py::test_bar PASSED

=========================== 2 passed in 0.01 seconds ===========================
PASSED

=========================== 1 passed in 0.24 seconds ===========================
Name            Stmts   Miss Branch BrMiss  Cover
-------------------------------------------------
impl                4      0      0      0   100%
sitecustomize       2      2      0      0     0%
subproc_test        5      0      0      0   100%
test                6      0      0      0   100%
-------------------------------------------------
TOTAL              17      2      0      0    88%
$ py.test -s -v test.py --cov --cov-report term
============================= test session starts ==============================
platform darwin -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2 -- /Users/mattbennett/.virtualenvs/tmp-f17938f4e8d61189/bin/python2.7
rootdir: /private/tmp/coverage_subprocess, inifile:
plugins: cov
collected 1 items

test.py::test_via_subproc ============================= test session starts ==============================
platform darwin -- Python 2.7.10 -- py-1.4.30 -- pytest-2.7.2 -- /Users/mattbennett/.virtualenvs/tmp-f17938f4e8d61189/bin/python2.7
rootdir: /private/tmp/coverage_subprocess, inifile:
plugins: cov
collected 2 items

subproc_test.py::test_foo PASSED
subproc_test.py::test_bar PASSED

=========================== 2 passed in 0.01 seconds ===========================
PASSED
--------------- coverage: platform darwin, python 2.7.10-final-0 ---------------
Name            Stmts   Miss Branch BrMiss  Cover
-------------------------------------------------
impl                4      0      0      0   100%
sitecustomize       4      4      0      0     0%
subproc_test        5      0      0      0   100%
test                4      0      0      0   100%
-------------------------------------------------
TOTAL              17      4      0      0    76%

=========================== 1 passed in 0.28 seconds ===========================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment