Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyTest : Allow Failure Rate

Tags:

python

pytest

I am currently working on a project where we are running a large suite of parameterized tests (>1M). The tests are randomly generated use-cases and in this large test space, it is expected that in each run certain edge cases will fail, ~1-2%. Is there a implementation for Pytest where you can pass a failure-rate argument, or handle this behavior?

like image 704
Shiny Brar Avatar asked Dec 09 '17 08:12

Shiny Brar


People also ask

How do you fail a test in pytest?

You can use the xfail marker to indicate that you expect a test to fail: @pytest. mark. xfail def test_function(): ...

Does pytest have a cache?

The pytest-cache plugin adds a config. cache object during the configure-initialization of pytest. This allows other plugins, including conftest.py files, to safely and flexibly store and retrieve values across test runs because the config object is available in many places.

What is pytest mark flaky?

Broadly speaking, a flaky test indicates that the test relies on some system state that is not being appropriately controlled - the test environment is not sufficiently isolated. Higher level tests are more likely to be flaky as they rely on more state.

What is Conftest py?

conftest.py is where you setup test configurations and store the testcases that are used by test functions. The configurations and the testcases are called fixture in pytest.


1 Answers

I guess what you want is modify exit status of pytest command, there is a nonpublic hook, named pytest_sessionfinish, could do this.

consider you have following tests:

def test_spam():
    assert 0

def test_ham():
    pass

def test_eggs():
    pass

and a hook in conftest.py:

import pytest, _pytest

ACCEPTABLE_FAILURE_RATE = 50

@pytest.hookimpl()
def pytest_sessionfinish(session, exitstatus):
    if exitstatus != _pytest.main.EXIT_TESTSFAILED:
        return
    failure_rate = (100.0 * session.testsfailed) / session.testscollected
    if failure_rate <= ACCEPTABLE_FAILURE_RATE:
        session.exitstatus = 0

then invoke pytest:

$ pytest --tb=no -q tests.py
F..                                                                                        [100%]
1 failed, 2 passed in 0.06 seconds

here the failure rate is 1 / 3 == 33.3%, below 50%:

$ echo $?
0

you could see the exit status of pytest is 0.

like image 136
georgexsh Avatar answered Sep 23 '22 13:09

georgexsh