Skip to content

Instantly share code, notes, and snippets.

@stas00
Last active December 30, 2020 01:43
Show Gist options
  • Save stas00/5d58c606dbdcb82e019d6b0674f8b42a to your computer and use it in GitHub Desktop.
Save stas00/5d58c606dbdcb82e019d6b0674f8b42a to your computer and use it in GitHub Desktop.
pytest skip marker for when a test must not be run under pytest-xdist -n setting since it does something that requires say all gpus untouched
# this goes into transformers/testing_utils.py
_pytest_num_workers = 1
def set_pytest_num_workers(n):
"""
This is helper method that sets how many pytest workers are used (if under pytest-xdist's -n option)
"""
_pytest_num_workers = n
def require_no_pytest_distributed(test_case):
"""
Decorator marking a test that requires that the test suite isn't running under pytest-xdist with more than one worker (i.e. no `pytest -n 2` or higher).
"""
if _pytest_num_workers > 1:
return unittest.skip("test must not be run under pytest-xdist where n > 1")(test_case)
else:
return test_case
# this goes into tests/conftest.py
def pytest_configure(config):
# some tests need to know whether they are being run alone or in parallel with pytest-xdist multiple workers
from transformers.testing_utils import set_pytest_num_workers
pytest_num_workers = config.getoption("-n")
print(f"GOT {pytest_num_workers} procs")
if pytest_num_workers is not None:
set_pytest_num_workers(pytest_num_workers)
# this goes into the test after importing it
@require_no_pytest_distributed
def test_something(): ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment