From 2919a331915bf1197e266a1467d67f9634148352 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 22 Nov 2023 22:51:20 +0100 Subject: [PATCH] and new hooks for result logging --- src/conftest.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/conftest.py b/src/conftest.py index 5c0bcee..7e714ee 100644 --- a/src/conftest.py +++ b/src/conftest.py @@ -5,18 +5,22 @@ # sys.path. It is thus good practise for projects to either put conftest.py under # a package scope or to never import anything from a conftest.py file. - +import os.path from pathlib import Path import pytest from dirmanager import DirManager from dotenv import dotenv_values +from playwright.sync_api import BrowserContext pytest_plugins = [ "setup.setup_authentik", ] +TIMEOUT = 5000 + + def pytest_addoption(parser): parser.addoption( "--env_file", @@ -62,3 +66,54 @@ def STATES(dirmanager) -> Path: @pytest.fixture(scope="session", autouse=True) def RESULTS(dirmanager) -> Path: return dirmanager.dirs["results"] + + +# log failed tests + + +@pytest.hookimpl(tryfirst=True, hookwrapper=True) +def pytest_runtest_makereport(item, call): + # execute all other hooks to obtain the report object + outcome = yield + rep = outcome.get_result() + + # we only look at actual failing test calls, not setup/teardown + if rep.when == "call" and rep.failed: + mode = "a" if os.path.exists("failures") else "w" + with open("failures", mode) as f: + f.write(rep.longreprtext + "\n") + + +# trace chrome on failed test + + +# inspired from https://docs.pytest.org/en/6.2.x/example/simple.html#request-example +""" +@pytest.hookimpl(tryfirst=True, hookwrapper=True) +def pytest_runtest_makereport(item, call): + outcome = yield + rep = outcome.get_result() + # set a report attribute for each phase of a call, which can + # be "setup", "call", "teardown" + setattr(item, "rep_" + rep.when, rep) + + +@pytest.fixture +def setup(request, context: BrowserContext): + # before test + context.tracing.start(screenshots=True, snapshots=True, sources=True) + context.set_default_timeout(TIMEOUT) + + # test execution + yield context + + # after test + if request.node.rep_setup.failed: + # test setup failed + pass + else: + if request.node.rep_call.failed: + # test execution failed + filename = "trace-" + request.node.originalname + ".zip" + context.tracing.stop(path=filename) +"""