and new hooks for result logging

This commit is contained in:
Daniel 2023-11-22 22:51:20 +01:00
parent 97ed87c79f
commit 2919a33191

View file

@ -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)
"""