Fixtures (fixtures) : fixtures (fixture function) Fixtures (fixture function) : fixtures (fixture function) = fixtures (fixture function)
First, in PyTest, if multiple fixture functions are passed into a test function, pyTest will execute in as linear a sequence as possible. If a problem with the previously executed fixture function raises an exception, PyTest will stop executing the fixture of that test function and mark the test function as faulty.
However, when a test is flagged as having an error, it doesn’t mean that the result of the test function has failed. It simply means that the fixture on which the test function relies is not working properly.
That brings me to another point: Fixtures, while flexible, need not be abused.
In practice, reduce unnecessary dependencies as much as possible. That way, the test function won’t break itself due to other unrelated problems.
With code examples, learn more:
import pytest
@pytest.fixture
def order():
return []
@pytest.fixture
def append_first(order):
order.append(1)
@pytest.fixture
def append_second(order, append_first):
order.extend([2])
@pytest.fixture(autouse=True)
def append_third(order, append_second):
order += [3]
def test_order(order):
assert order == [1, 2, 3]
Copy the code
First, declare that this code works and that the test function test_Order passes normally.
Suppose, however, that an error is always reported at order.append(1). At this point, we can’t actually determine if order.extend([2]) or order +=[3] are also problematic.
After append_first throws an exception, PyTest does not continue to run any fixture functions, even the test function test_Order itself.