conftest.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import contextlib
  2. import gc
  3. import os
  4. import pytest
  5. import random
  6. from openpilot.common.prefix import OpenpilotPrefix
  7. from openpilot.system.manager import manager
  8. from openpilot.system.hardware import TICI, HARDWARE
  9. # TODO: pytest-cpp doesn't support FAIL, and we need to create test translations in sessionstart
  10. # pending https://github.com/pytest-dev/pytest-cpp/pull/147
  11. collect_ignore = [
  12. "selfdrive/ui/tests/test_translations",
  13. "selfdrive/test/process_replay/test_processes.py",
  14. "selfdrive/test/process_replay/test_regen.py",
  15. "selfdrive/test/test_time_to_onroad.py",
  16. ]
  17. collect_ignore_glob = [
  18. "selfdrive/debug/*.py",
  19. "selfdrive/modeld/*.py",
  20. ]
  21. def pytest_sessionstart(session):
  22. # TODO: fix tests and enable test order randomization
  23. if session.config.pluginmanager.hasplugin('randomly'):
  24. session.config.option.randomly_reorganize = False
  25. @pytest.hookimpl(hookwrapper=True, trylast=True)
  26. def pytest_runtest_call(item):
  27. # ensure we run as a hook after capturemanager's
  28. if item.get_closest_marker("nocapture") is not None:
  29. capmanager = item.config.pluginmanager.getplugin("capturemanager")
  30. with capmanager.global_and_fixture_disabled():
  31. yield
  32. else:
  33. yield
  34. @contextlib.contextmanager
  35. def clean_env():
  36. starting_env = dict(os.environ)
  37. yield
  38. os.environ.clear()
  39. os.environ.update(starting_env)
  40. @pytest.fixture(scope="function", autouse=True)
  41. def openpilot_function_fixture(request):
  42. random.seed(0)
  43. with clean_env():
  44. # setup a clean environment for each test
  45. with OpenpilotPrefix(shared_download_cache=request.node.get_closest_marker("shared_download_cache") is not None) as prefix:
  46. prefix = os.environ["OPENPILOT_PREFIX"]
  47. yield
  48. # ensure the test doesn't change the prefix
  49. assert "OPENPILOT_PREFIX" in os.environ and prefix == os.environ["OPENPILOT_PREFIX"]
  50. # cleanup any started processes
  51. manager.manager_cleanup()
  52. # some processes disable gc for performance, re-enable here
  53. if not gc.isenabled():
  54. gc.enable()
  55. gc.collect()
  56. # If you use setUpClass, the environment variables won't be cleared properly,
  57. # so we need to hook both the function and class pytest fixtures
  58. @pytest.fixture(scope="class", autouse=True)
  59. def openpilot_class_fixture():
  60. with clean_env():
  61. yield
  62. @pytest.fixture(scope="function")
  63. def tici_setup_fixture(openpilot_function_fixture):
  64. """Ensure a consistent state for tests on-device. Needs the openpilot function fixture to run first."""
  65. HARDWARE.initialize_hardware()
  66. HARDWARE.set_power_save(False)
  67. os.system("pkill -9 -f athena")
  68. @pytest.hookimpl(tryfirst=True)
  69. def pytest_collection_modifyitems(config, items):
  70. skipper = pytest.mark.skip(reason="Skipping tici test on PC")
  71. for item in items:
  72. if "tici" in item.keywords:
  73. if not TICI:
  74. item.add_marker(skipper)
  75. else:
  76. item.fixturenames.append('tici_setup_fixture')
  77. if "xdist_group_class_property" in item.keywords:
  78. class_property_name = item.get_closest_marker('xdist_group_class_property').args[0]
  79. class_property_value = getattr(item.cls, class_property_name)
  80. item.add_marker(pytest.mark.xdist_group(class_property_value))
  81. @pytest.hookimpl(trylast=True)
  82. def pytest_configure(config):
  83. config_line = "xdist_group_class_property: group tests by a property of the class that contains them"
  84. config.addinivalue_line("markers", config_line)
  85. config_line = "nocapture: don't capture test output"
  86. config.addinivalue_line("markers", config_line)
  87. config_line = "shared_download_cache: share download cache between tests"
  88. config.addinivalue_line("markers", config_line)