mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Run unit tests in isolation w/ coverage support.
This commit is contained in:
parent
d0e1a1c6c3
commit
fcac261eef
5 changed files with 36 additions and 1 deletions
|
@ -9,6 +9,7 @@ branch = True
|
||||||
# results from multiple tests simultaneously, as well as supporting multiple
|
# results from multiple tests simultaneously, as well as supporting multiple
|
||||||
# test runs, such as from integration tests.
|
# test runs, such as from integration tests.
|
||||||
concurrency = multiprocessing
|
concurrency = multiprocessing
|
||||||
|
parallel = True
|
||||||
|
|
||||||
# When running tests through ansible-test, this option is overridden by
|
# When running tests through ansible-test, this option is overridden by
|
||||||
# the COVERAGE_FILE environment variable. This option is present for
|
# the COVERAGE_FILE environment variable. This option is present for
|
||||||
|
|
|
@ -144,7 +144,6 @@ def coverage_command(self_dir, version):
|
||||||
args = [
|
args = [
|
||||||
find_executable(executable),
|
find_executable(executable),
|
||||||
'run',
|
'run',
|
||||||
'--append',
|
|
||||||
'--rcfile',
|
'--rcfile',
|
||||||
os.path.join(self_dir, '.coveragerc'),
|
os.path.join(self_dir, '.coveragerc'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -624,6 +624,7 @@ def command_units(args):
|
||||||
|
|
||||||
cmd = [
|
cmd = [
|
||||||
'pytest',
|
'pytest',
|
||||||
|
'--boxed',
|
||||||
'-r', 'a',
|
'-r', 'a',
|
||||||
'--color',
|
'--color',
|
||||||
'yes' if args.color else 'no',
|
'yes' if args.color else 'no',
|
||||||
|
|
|
@ -6,6 +6,7 @@ passlib
|
||||||
pycrypto
|
pycrypto
|
||||||
pytest
|
pytest
|
||||||
pytest-mock
|
pytest-mock
|
||||||
|
pytest-xdist
|
||||||
python-memcached
|
python-memcached
|
||||||
pyyaml
|
pyyaml
|
||||||
redis
|
redis
|
||||||
|
|
33
test/units/conftest.py
Normal file
33
test/units/conftest.py
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
"""Monkey patch os._exit when running under coverage so we don't lose coverage data in forks, such as with `pytest --boxed`."""
|
||||||
|
import gc
|
||||||
|
import os
|
||||||
|
|
||||||
|
try:
|
||||||
|
import coverage
|
||||||
|
except ImportError:
|
||||||
|
coverage = None
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_configure():
|
||||||
|
if not coverage:
|
||||||
|
return
|
||||||
|
|
||||||
|
coverage_instances = []
|
||||||
|
|
||||||
|
for obj in gc.get_objects():
|
||||||
|
if isinstance(obj, coverage.Coverage):
|
||||||
|
coverage_instances.append(obj)
|
||||||
|
|
||||||
|
if not coverage_instances:
|
||||||
|
return
|
||||||
|
|
||||||
|
os_exit = os._exit
|
||||||
|
|
||||||
|
def coverage_exit(*args, **kwargs):
|
||||||
|
for instance in coverage_instances:
|
||||||
|
instance.stop()
|
||||||
|
instance.save()
|
||||||
|
|
||||||
|
os_exit(*args, **kwargs)
|
||||||
|
|
||||||
|
os._exit = coverage_exit
|
Loading…
Reference in a new issue