Skip to content

Commit

Permalink
pytest_tests: Collect logs only for failed tests
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
vvarg229 committed Jun 19, 2023
1 parent 16a9567 commit 3a37fb7
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions pytest_tests/testsuites/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 3a37fb7

Please sign in to comment.