From 0ea14d23a8ed50dab3756281f9ce34cb4f637ffd Mon Sep 17 00:00:00 2001 From: Roman Donchenko Date: Thu, 5 Jan 2023 13:26:01 +0300 Subject: [PATCH] Python tests: skip the session hooks if --collect-only is used (#5550) Turns out that #5456 had a nasty side effect. Session hooks are called when pytest is run with `--collect-only` (even though no tests are actually run in this case), and Visual Studio Code periodically runs `pytest --collect-only` in order to learn what tests exist in the project. As a result, it keeps restarting the services and restoring the database in the background. Work around this by skipping all logic in the hooks if `--collect-only` is in the options. --- tests/python/shared/fixtures/init.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/python/shared/fixtures/init.py b/tests/python/shared/fixtures/init.py index d0a68b94..77c7e702 100644 --- a/tests/python/shared/fixtures/init.py +++ b/tests/python/shared/fixtures/init.py @@ -259,12 +259,21 @@ def start_services(rebuild=False): docker_cp(CVAT_DB_DIR / "data.json", f"{PREFIX}_cvat_server_1:/tmp/data.json") -def pytest_sessionstart(session): +def pytest_sessionstart(session: pytest.Session) -> None: stop = session.config.getoption("--stop-services") start = session.config.getoption("--start-services") rebuild = session.config.getoption("--rebuild") cleanup = session.config.getoption("--cleanup") dumpdb = session.config.getoption("--dumpdb") + + if session.config.getoption("--collect-only"): + if any((stop, start, rebuild, cleanup, dumpdb)): + raise Exception( + """--collect-only is not compatible with any of the other options: + --stop-services --start-services --rebuild --cleanup --dumpdb""" + ) + return # don't need to start the services to collect tests + platform = session.config.getoption("--platform") if platform == "kube" and any((stop, start, rebuild, cleanup, dumpdb)): @@ -338,7 +347,10 @@ def pytest_sessionstart(session): ) -def pytest_sessionfinish(session, exitstatus): +def pytest_sessionfinish(session: pytest.Session, exitstatus: int) -> None: + if session.config.getoption("--collect-only"): + return + platform = session.config.getoption("--platform") if platform == "local":