pytestplaywrightpytest-markers

Wrap pytest mark with defaults


I've got a test that sometimes fails because the playwright screenshot doesn't match. I added a flaky mark to the test so it runs a few times before failing, which works:

@pytest.mark.flaky(reruns=3, reruns_delay=1, only_rerun="Failed: --> Snapshots DO NOT match!")
def test_foo(page: Page, assert_snapshot):
    assert_snapshot(page.screenshot())

How can I create a custom mark that has these values as default? I've tried adding the following and it fails like the mark isn't there at all:

conftest.py:

def flaky_screenshot():
    return pytest.mark.flaky(
        reruns=3,
        reruns_delay=1,
        only_rerun="Failed: --> Snapshots DO NOT match!"
    )

pytest.ini:

[pytest]
markers =
    flaky_screenshot: mark test to rerun flaky screenshots

test.py:

@pytest.mark.flaky_screenshot()
def test_foo(page: Page, assert_snapshot):
    assert_snapshot(page.screenshot())

Solution

  • Not a custom mark, but I was able to get a decorator with Dunes's help in the comments.

    conftest.py:

    flaky_screenshot = pytest.mark.flaky(
      reruns=3,
      reruns_delay=1,
      only_rerun="Failed: --> Snapshots DO NOT match!"
    )
    

    And to use it:

    from conftest import flaky_screenshot
    
    @flaky_screenshot
    def test_foo(page: Page, assert_snapshot):
        assert_snapshot(page.screenshot())