Today I want to share with you 3 of my favourite commands when using Pytest. But first of all...

Whats Pytest?

For those of you who are new to Pytest (checkout new instructor-led course here):

Pytest is a flexible, mature and feature-rich framework for testing Python code.

Pytest has been the defacto testing tool for Python devs for a number of years now. But for us in the networking world, it makes writing tests for network validation a breeze.

Why? Here is a quick example:

You build yourself a test function. Pass in some data, and then perform your test evaluation using the assert keyword (example below).

def test_vlan():
    device_vlan = "100"
    expected_vlan = "101"
    assert device_vlan == expected_vlan

Also as it's Python-based it plugs in easily with the other network automation tools like Batfish, Suzieq, Nornir and Scrapli.

Sounds good! So, what are the commands?

Traceback Output

First of all, there is traceback output. You see, when you are dealing with a lot of tests, and in some cases a lot of failures it can be good to reduce the amount of traceback output you see using --tb=<option>.

There are various options available to use but the 2 that I find most useful are line and short. Below shows an example of --tb=short:

$ pytest --tb=short .
collected 2 items                                                                                                                           F                                                                                                                           [ 50%] .                                                                                                                            [100%]

===================================================================== FAILURES =====================================================================
____________________________________________________________________ test_vlan _____________________________________________________________________ in test_vlan
    assert device_vlan == expected_vlan
E   AssertionError: assert '100' == '101'
E     - 101
E     + 100

Setup Plan

By far my number 1 command in Pytest, --setup-plan. This shows you the setups and teardowns of your fixtures, in terms of the order that they are performed. It's worth noting when running this option no tests will be performed, just the intended plan for your fixtures will be shown.

$ pytest -v --tb=short  003_fixtures/001_intro --setup-plan
collected 2 items                                                                                                                  
        SETUP    F expected_vlan
        SETUP    F device_vlan
        003_fixtures/001_intro/ (fixtures used: device_vlan, expected_vlan)
        TEARDOWN F device_vlan
        TEARDOWN F expected_vlan

Show Locals

Finally, we have --showlocals, which shows the values of the locally assigned variables from within a traceback. Super useful when troubleshooting your tests.

$ pytest . --showlocals

    def test_vlan():
        device_vlan = "100"
        expected_vlan = "101"
>       assert device_vlan == expected_vlan
E       AssertionError: assert '100' == '101'
E         - 101
E         + 100
device_vlan = '100'    <--- local vars
expected_vlan = '101'  <--- local vars AssertionError

Looking to learn more on how you can use Pytest for network validation. If so, check out our course below:

Network Testing with Pytest
Learn how to structure and build tests to validate your network using Pytest. Along with how to integrate Pytest with Scrapli, Nornir, NAPALM, Batfish, and Suzieq.
Want to Learn Network Automation?
Join our membership and get access to:
  • 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 ➜