From 3a37fb70098e480c3825b1d69459afef448197fa Mon Sep 17 00:00:00 2001 From: Oleg Kulachenko Date: Mon, 19 Jun 2023 19:55:47 +0400 Subject: [PATCH] pytest_tests: Collect logs only for failed tests This commit modifies the log collection procedure to collect logs only for failed tests. This change will reduce the total testing time. Collecting logs for each test takes about 15 seconds, on hundreds of tests we were losing too much time. The all logs at the end of the test set, of course, are saved. Signed-off-by: Oleg Kulachenko --- pytest_tests/testsuites/conftest.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/pytest_tests/testsuites/conftest.py b/pytest_tests/testsuites/conftest.py index 77578993f..f6f6ab660 100644 --- a/pytest_tests/testsuites/conftest.py +++ b/pytest_tests/testsuites/conftest.py @@ -45,8 +45,24 @@ from helpers.wallet import WalletFactory +from typing import Dict +from pytest import StashKey, CollectReport + logger = logging.getLogger("NeoLogger") +phase_report_key = StashKey[Dict[str, CollectReport]]() + + +@pytest.hookimpl(tryfirst=True, hookwrapper=True) +def pytest_runtest_makereport(item, call): + # https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures + # execute all other hooks to obtain the report object + outcome = yield + rep = outcome.get_result() + + # store test results for each phase of a call, which can + # be "setup", "call", "teardown" + item.stash.setdefault(phase_report_key, {})[rep.when] = rep def pytest_collection_modifyitems(items): # Make network tests last based on @pytest.mark.node_mgmt @@ -177,7 +193,7 @@ def collect_full_tests_logs(temp_directory, hosting: Hosting): @pytest.fixture(scope="function", autouse=True) -@allure.title("Collect test logs") +@allure.title("Collect logs for failed tests") def collect_test_logs(request, temp_directory, hosting: Hosting): test_name = request.node.nodeid.translate(str.maketrans(":[]/", "____")) hash_suffix = hashlib.md5(test_name.encode()).hexdigest() @@ -186,9 +202,12 @@ def collect_test_logs(request, temp_directory, hosting: Hosting): with allure.step(f'Start collecting logs for {file_name}'): start_time = datetime.utcnow() yield - with allure.step(f'Stop collecting logs for {file_name}, logs path: {logs_dir} '): - end_time = datetime.utcnow() - store_logs(hosting, logs_dir, file_name, start_time, end_time) + # https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures + report = request.node.stash[phase_report_key] + if report["setup"].failed or ("call" in report and report["call"].failed): + with allure.step(f'Stop collecting logs for {file_name}, logs path: {logs_dir} '): + end_time = datetime.utcnow() + store_logs(hosting, logs_dir, file_name, start_time, end_time) @pytest.fixture(scope="session", autouse=True)