This lesson is part of the Network Testing with Pytest course.
In this lesson, you will learn:
- What is a yield fixture.
- Refactoring our previous VLAN test to use yield fixtures along with pull data via Scrapli + TextFSM.
You can find the scripts and code for this lesson within the Pytest repo under the directory: 003_fixtures/002_yield.
What is a Yield Fixture?
Up until this point, we have used the return
keyword within our fixtures to pass the data back to the requesting fixture or test, like so:
...
@pytest.fixture
def device_vlan():
return "101"
def test_vlan(expected_vlan, device_vlan):
assert expected_vlan == device_vlan
However, what if we needed to perform some actions (i.e cleanup/teardown actions) within our fixtures after the return statement? For example: removing temp directories, closing device sessions or even destroying a test topology once our tests have been run.