Hi Eliot,

The only way to detect failures is using the pytest_runtest_logreport hook,
where you will obtain the test report for each test during each phase
(setup/call/teardown). You will need to store that somewhere so you can
reuse it later, possibly in a global variable or in self if you implement a
plugin class and register it during pytest_configure.

Here’s the example using a global variable, and printing the failed nodes
at the end of the session:

failed_tests = []
def pytest_runtest_logreport(report):
    if report.outcome == "failed":
        failed_tests.append(report.nodeid)
def pytest_terminal_summary(terminalreporter):
    terminalreporter.section("my custom report")
    terminalreporter.line(f"detected {len(failed_tests)} failures")

You can read more about hooks here:
https://docs.pytest.org/en/stable/writing_plugins.html#writing-hook-functions

Hope this helps,
Bruno.

On Thu, Apr 15, 2021 at 7:17 PM Eliot, Christopher <
christopher.el...@nagrastar.com> wrote:

> Thank you again.  What I really want is a way to ask Pytest, from inside a
> fixture or other python class, “hey Pytest, have you encountered any
> assertion failures during this session?”
>
>
>
> Topher Eliot
>
>
>
> *From:* Ronny Pfannschmidt <opensou...@ronnypfannschmidt.de>
> *Sent:* Thursday, April 15, 2021 3:05 PM
> *To:* Eliot, Christopher <christopher.el...@nagrastar.com>;
> pytest-dev@python.org
> *Subject:* Re: [pytest-dev] How can I suppress cleanups when a test fails
>
>
>
> in case you use a orm/abstraction layer, sqlite in a tmpdir can be a
> wonderful companion
>
> in case you use another db, you can actually have failures do a db/data
> dump if you like,
>
> unfortunately i'm not aware of a tmp_path alike db setup/teardown tool
>
> --Ronny
>
> Am 15.04.21 um 23:00 schrieb Eliot, Christopher:
>
> Thank you.  This might be helpful, but I also have other resources that
> are not files that I would like to selectively clean up or not.  These
> resources are in a relational DB, not a file system.  And no, I can’t just
> wrap everything in a big transaction and then roll it back, unfortunately.
>
>
>
> Topher Eliot
>
>
>
> *From:* Ronny Pfannschmidt <opensou...@ronnypfannschmidt.de>
> <opensou...@ronnypfannschmidt.de>
>
> Hi Christopher,
>
> if you use the tmpdir/tmp_path fixtures pytest provides,
> just don't do additional cleanup, pytest keeps the last 3 basetemps around
> precisely for that use-case and drops older ones
>
> -- Ronny
>
> Am 15.04.21 um 21:56 schrieb Eliot, Christopher:
>
> My test suit generates some intermediate files and other resources which
> typically would be deleted upon termination of the test.  However, if there
> is a failure, I would like to leave them in place to help in diagnosing the
> failure.
>
>
>
> Is there a clean way to do this?
>
>
>
> I’m already using a fixture to do cleanup, so I’m prepared to use some
> aspect of the fixture if that’s appropriate.
>
>
>
> Thanks,
>
> Topher Eliot
>
> _______________________________________________
> pytest-dev mailing list
> pytest-dev@python.org
> https://mail.python.org/mailman/listinfo/pytest-dev
>
_______________________________________________
pytest-dev mailing list
pytest-dev@python.org
https://mail.python.org/mailman/listinfo/pytest-dev

Reply via email to