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
test_001_vlan.py F [ 50%]
test_002_mtu.py . [100%]
===================================================================== FAILURES =====================================================================
____________________________________________________________________ test_vlan _____________________________________________________________________
test_001_vlan.py:5: 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
003_fixtures/001_intro/test_001_vlan.py::test_vlan
SETUP F expected_vlan
SETUP F device_vlan
003_fixtures/001_intro/test_001_vlan.py::test_vlan (fixtures used: device_vlan, expected_vlan)
TEARDOWN F device_vlan
TEARDOWN F expected_vlan
003_fixtures/001_intro/test_002_mtu.py::test_mtu
003_fixtures/001_intro/test_002_mtu.py::test_mtu
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
test_001_vlan.py:5: AssertionError