will still be generated within individual test "working/" directories and
appear in test weblogs. In general, this option is only recommended when
manually/frequently running unit tests, to keep your local repo clean.
parser.addoption("--collect-tests", action="store_true", default=False,
help="Collect tests and export test node ids to a plain text file `collected_tests.txt`")
parser.addoption("--nologfile", action="store_true", default=False,
parser.addoption("--pyclean", action="store_true", default=False,
help="Clean up .pyc to reproduce certain warnings only issued when the bytecode is compiled.")
parser.addoption("--remove-workdir", action="store_true", default=False,
help="Remove individual working directories from regression tests.")
def pytest_sessionstart(session):
"""Prepare pytest session."""
# redirect casalog for the master process, if requested
if session.config.getoption('--nologfile') and not hasattr(session.config, 'workerinput'):
# clean up .pyc to reproduce certain warnings only when the bytecode is compiled, e.g,
# invalid escape sequence warnings
if session.config.getoption('--pyclean'):
for p in pathlib.Path('.').rglob('*.py[co]'):
for p in pathlib.Path('.').rglob('__pycache__'):
def pytest_configure(config):
# pytest.config (global) is deprecated from pytest ver>5.0.
# we save a copy of its content under `pytest.pytestconfig` for an easy access from helper classes.
pytest.pytestconfig = config
def pytest_collection_finish(session):
"""Exit after collection if only collect test."""
if session.config.getoption('--collect-tests'):
with open('collected_tests.txt', 'w') as f:
for item in session.items:
node_id = '{}::{}'.format(item.fspath, item.name)
pytest.exit('Tests collection completed.')
@pytest.fixture(scope="session", autouse=True)
def redirect_casalog_for_workers(request):
"""Remove casalog for each worker, if requested."""
if request.config.getoption("--nologfile") and hasattr(request.config, 'workerinput'):
"""Redirect casalog to /dev/null before executeting tests.
We clean up the default logfile potentially created from the CASA session initialization,
and then redirect logging to `/dev/null`.