Skip to content

[Question]: how to fail test on any unsuccessful status? #873

@nedbat

Description

@nedbat

Your question

I would like to be able to write tests that use goto and click, and have the test automatically fail if the navigation results in an unsuccessful status.

I tried this:

@pytest.fixture(autouse=True)
def fail_on_failure(page):
    def _finished(response):
        assert response.status == 200, f"{response.request.method} {response.url} returned {response.status}"
    page.on("response", _finished)

My test had a page.click that produced a 500 error. I see the assert exception in a traceback in my test results, but only because a subsequent assert failed.

I also tried:

@pytest.fixture(autouse=True)
def fail_on_failure(page):
    def _finished(response):
        if response.status == 500:
            pytest.fail(f"{response.request.method} {response.url} returned {response.status}")
    page.on("response", _finished)

This caused pytest to freeze after my test with a Fail. The failure seems to be due to the pytest.fail (I removed the subsequent assert and the test still failed), but pytest becomes stuck.

Is there a way to systemically fail tests if any request fails?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions