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.

Members Only Content
Want to Learn Network Automation?
Join our membership to access:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Tech demos and sessions

Join Now ➜
Close You've successfully subscribed to Packet Coders.
Close Success! Your account is fully activated, you now have access to all content.
Close Welcome back! You've successfully signed in.
Close Nearly there! To activate your account, please click the link in the email we just sent you.