CertGrid CertGrid
Hands-on Lab·Python Automation for IT

Testing Python Automation with pytest

Automation is exactly the code nobody watches run, which makes it exactly the code worth testing. pytest needs no boilerplate: a function whose name starts with `test_` and a plain `assert`. What you get back is the interesting part - it rewrites the assertion so a failure shows both values and the difference, and it has four distinct exit codes that a CI step should treat differently.

Remote Hosts and CI/CD Guide 35 of 39 Intermediate

Written against the versions above. pytest 9.1.1. `tmp_path` (a `pathlib.Path`) superseded `tmpdir` years ago. Exit code 5 for "no tests collected" has been stable for a long time and is the one that catches CI configurations.

Everything on this page runs on the control node. Any machine with Python 3 will do.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSControl node - every script in this path runs here2 Core4 GB50 GB

Before you start

  1. The thing worth testing

  2. A first test file

  3. What a failure actually tells you

  4. The same failures, shorter

  5. One test, many cases

  6. A fixture, and a real temporary directory

  7. The flags worth knowing

  8. pytest's own exit codes

  9. conftest.py, for what every test needs

  10. And what the tests do not cover

  11. A config file so nobody has to remember the flags

Official sources