conftest.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import shutil
  3. from pathlib import Path
  4. import pytest
  5. from ultralytics.utils import ROOT
  6. from ultralytics.utils.torch_utils import init_seeds
  7. TMP = (ROOT / '../tests/tmp').resolve() # temp directory for test files
  8. def pytest_addoption(parser):
  9. parser.addoption('--runslow', action='store_true', default=False, help='run slow tests')
  10. def pytest_configure(config):
  11. config.addinivalue_line('markers', 'slow: mark test as slow to run')
  12. def pytest_collection_modifyitems(config, items):
  13. if config.getoption('--runslow'):
  14. # --runslow given in cli: do not skip slow tests
  15. return
  16. skip_slow = pytest.mark.skip(reason='need --runslow option to run')
  17. for item in items:
  18. if 'slow' in item.keywords:
  19. item.add_marker(skip_slow)
  20. def pytest_sessionstart(session):
  21. """
  22. Called after the 'Session' object has been created and before performing test collection.
  23. """
  24. init_seeds()
  25. shutil.rmtree(TMP, ignore_errors=True) # delete any existing tests/tmp directory
  26. TMP.mkdir(parents=True, exist_ok=True) # create a new empty directory
  27. def pytest_terminal_summary(terminalreporter, exitstatus, config):
  28. # Remove files
  29. for file in ['bus.jpg', 'decelera_landscape_min.mov']:
  30. Path(file).unlink(missing_ok=True)
  31. # Remove directories
  32. for directory in ['.pytest_cache/', TMP]:
  33. shutil.rmtree(directory, ignore_errors=True)