In pytest, how can I figure out if a test failed? (from “request”)

前端 未结 2 958
予麋鹿
予麋鹿 2021-02-13 09:48

I\'m using Selenium with PYTEST to test a site. I would like to take a screenshot of the page whenever a test fails (and only when it fails).

Is there a way that I can

2条回答
  •  孤街浪徒
    2021-02-13 10:29

    I had to do something similar on a per-module level. After examining the existing solutions I was a little surprised by their complexity. Here's an approach I came up with to address this issue:

    import pytest
    
    
    @pytest.fixture(scope="module", autouse=True)
    def failure_tracking_fixture(request):
        tests_failed_before_module = request.session.testsfailed
        yield
        tests_failed_during_module = request.session.testsfailed - tests_failed_before_module
    

    It can be tweaked to do what you want by making the fixture a function-level one.

    Hope this helps!

提交回复
热议问题