2023-10-05 02:51:44 +08:00
|
|
|
import os
|
2023-09-06 09:52:40 +08:00
|
|
|
import pytest
|
2023-11-09 05:43:32 +08:00
|
|
|
import random
|
2023-09-06 09:52:40 +08:00
|
|
|
|
|
|
|
from openpilot.common.prefix import OpenpilotPrefix
|
2023-11-08 09:35:44 +08:00
|
|
|
from openpilot.system.hardware import TICI
|
2023-09-06 09:52:40 +08:00
|
|
|
|
|
|
|
|
2023-12-10 06:28:32 +08:00
|
|
|
def pytest_sessionstart(session):
|
|
|
|
# TODO: fix tests and enable test order randomization
|
|
|
|
if session.config.pluginmanager.hasplugin('randomly'):
|
|
|
|
session.config.option.randomly_reorganize = False
|
|
|
|
|
|
|
|
|
2023-09-06 09:52:40 +08:00
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
2023-10-05 02:51:44 +08:00
|
|
|
def openpilot_function_fixture():
|
|
|
|
starting_env = dict(os.environ)
|
|
|
|
|
2023-11-09 05:43:32 +08:00
|
|
|
random.seed(0)
|
|
|
|
|
2023-09-06 09:52:40 +08:00
|
|
|
# setup a clean environment for each test
|
|
|
|
with OpenpilotPrefix():
|
2023-10-24 08:41:19 +08:00
|
|
|
prefix = os.environ["OPENPILOT_PREFIX"]
|
|
|
|
|
2023-09-06 09:52:40 +08:00
|
|
|
yield
|
2023-10-05 02:51:44 +08:00
|
|
|
|
2023-10-24 08:41:19 +08:00
|
|
|
# ensure the test doesn't change the prefix
|
|
|
|
assert "OPENPILOT_PREFIX" in os.environ and prefix == os.environ["OPENPILOT_PREFIX"]
|
|
|
|
|
2023-10-05 02:51:44 +08:00
|
|
|
os.environ.clear()
|
|
|
|
os.environ.update(starting_env)
|
|
|
|
|
|
|
|
|
|
|
|
# If you use setUpClass, the environment variables won't be cleared properly,
|
|
|
|
# so we need to hook both the function and class pytest fixtures
|
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
|
|
|
def openpilot_class_fixture():
|
|
|
|
starting_env = dict(os.environ)
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
os.environ.clear()
|
|
|
|
os.environ.update(starting_env)
|
2023-11-08 09:35:44 +08:00
|
|
|
|
|
|
|
|
2023-11-15 09:33:47 +08:00
|
|
|
@pytest.hookimpl(tryfirst=True)
|
2023-11-08 09:35:44 +08:00
|
|
|
def pytest_collection_modifyitems(config, items):
|
|
|
|
skipper = pytest.mark.skip(reason="Skipping tici test on PC")
|
|
|
|
for item in items:
|
|
|
|
if not TICI and "tici" in item.keywords:
|
2023-11-15 09:33:47 +08:00
|
|
|
item.add_marker(skipper)
|
|
|
|
|
|
|
|
if "xdist_group_class_property" in item.keywords:
|
2023-12-07 08:00:59 +08:00
|
|
|
class_property_name = item.get_closest_marker('xdist_group_class_property').args[0]
|
|
|
|
class_property_value = getattr(item.cls, class_property_name)
|
|
|
|
item.add_marker(pytest.mark.xdist_group(class_property_value))
|
2023-11-15 09:33:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.hookimpl(trylast=True)
|
|
|
|
def pytest_configure(config):
|
2023-12-10 05:24:18 +08:00
|
|
|
config_line = (
|
|
|
|
"xdist_group_class_property: group tests by a property of the class that contains them"
|
|
|
|
)
|
|
|
|
config.addinivalue_line("markers", config_line)
|