vendredi 2 octobre 2020

Using fixtures to skip a test in pytest

So I have a huge object that holds information that is being initiated inside a fixture. I need to use this information to run my tests and here starts the tricky part. IF I do not have an attribute inside the object that is being used inside the test case I have to skip it.

The fixture with the generation of the object is being initiated once before test runs (in general). I need an easy-to-use decorator/fixture/whatever before the test that will check if the object has what it is needed inside the object.

Example:

@pytest.fixture(scope="package")
def info(request):
    print("Setting up...")
    obj = Creator()
    obj.setup()
    obj.prepare() if hasattr(obj, "prepare") else ""
    def teardown():
        obj.teardown() if hasattr(obj, "teardown") else ""
    request.addfinalizer(teardown)
    return obj.call()

...

@has_attr("some_attr")
def test_sometest(info):
    assert info.some_attr == 42

Aucun commentaire:

Enregistrer un commentaire