Add pytest report section listing e2e screenshots

I would like it to be obvious to contributors who run the tests locally
that there are screenshots of the processes under test that they can
examine. I don't think it's obvious that there could be useful files
sitting round in a temp directory.

This commit adds the screenshot file paths to a user property on failed
tests then adds a custom report section that pulls that lists those
properties. That way when there is errors users will get the paths to
the images printed out alongside the report of failed tests.

I find it difficult to navigate the internals of pytest. I tried various
ways of printing information and getting that information to methods
that could do the printing but couldn't get anything to work. I ended up
entirely copying this SO post which worked really well for attaching
information to test results in a place that is accessable to the
reporting hook: https://stackoverflow.com/a/64822668

It's added to the end of the existing terminal report hook, because
while it seems you can have two of those hooks, things can get pretty
confusing with interleaved reports and not all of them running every
time.

    --------------------- End2end screenshots available in: /tmp/pytest-of-user/pytest-56/pytest-screenshots ---------------------
    2024-08-17T14_49_35.896940-tests_end2end_features_test_utilcmds_bdd.py__test_cmdrepeatlast_with_modeswitching_command_deleting.png
    2024-08-17T14_49_37.391229-tests_end2end_features_test_completion_bdd.py__test_deleting_an_open_tab_via_the_completion.png
    =================================================== short test summary info ====================================================
    FAILED tests/end2end/features/test_utilcmds_bdd.py::test_cmdrepeatlast_with_modeswitching_command_deleting - AssertionError: assert 'http://local...ata/hello.txt' == 'http://local...ata/sello.txt'
    FAILED tests/end2end/features/test_completion_bdd.py::test_deleting_an_open_tab_via_the_completion - AssertionError: assert 'http://local...ata/hello.txt' == 'http://local...ata/sello.txt'
    ====================================================== 2 failed in 5.18s =======================================================

From adding debug messages I can see:

    RUNNER_TEMP=/home/runner/work/_temp
    /tmp/pytest-of-runner/pytest-0/pytest-screenshots

Means that I don't think GHA will be able to collect the temp files
because they are not being written to the temp dir being mounted in from
outside. I think that's the case anyway. Might have to pass
--basetemp=$RUNNER_TEMP to pytest or set TEMP or something. TODO
This commit is contained in:
toofar 2024-08-17 14:51:19 +12:00
parent 2fcd6eafc4
commit 0c3807b04a
2 changed files with 24 additions and 6 deletions

View File

@ -372,7 +372,8 @@ def pytest_runtest_makereport(item, call):
@pytest.hookimpl(hookwrapper=True)
def pytest_terminal_summary(terminalreporter):
"""Group benchmark results on CI."""
"""Add custom pytest summary sections."""
# Group benchmark results on CI.
if testutils.ON_CI:
terminalreporter.write_line(
testutils.gha_group_begin('Benchmark results'))
@ -380,3 +381,23 @@ def pytest_terminal_summary(terminalreporter):
terminalreporter.write_line(testutils.gha_group_end())
else:
yield
# List any screenshots of failed end2end tests that were generated during
# the run. Screenshots are captured from QuteProc.after_test()
properties = lambda report: dict(report.user_properties)
reports = [
report
for report in terminalreporter.getreports("")
if "screenshot" in properties(report)
]
screenshots = [
pathlib.Path(properties(report)["screenshot"])
for report in reports
]
if screenshots:
terminalreporter.ensure_newline()
screenshot_dir = screenshots[0].parent
terminalreporter.section(f"End2end screenshots available in: {screenshot_dir}", sep="-", blue=True, bold=True)
for screenshot in screenshots:
terminalreporter.line(screenshot.parts[-1])

View File

@ -932,7 +932,7 @@ def screenshot_dir(request, tmp_path_factory):
@pytest.fixture
def take_x11_screenshot(request, screenshot_dir, xvfb):
def take_x11_screenshot(request, screenshot_dir, record_property, xvfb):
"""Take a screenshot of the current pytest-xvfb display.
Screenshots are saved to the location of the `screenshot_dir` fixture.
@ -952,13 +952,10 @@ def take_x11_screenshot(request, screenshot_dir, xvfb):
for char in bad_chars:
fname = fname.replace(char, "_")
# TODO:
# 1. Log a "screenshot saved to ..." message so that people know where
# to go look for them when running locally? Using pytest-print? Or
# add an FYI to the report summary?
fpath = screenshot_dir / fname
img.save(fpath)
record_property("screenshot", str(fpath))
return doit