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
- Control nodeUbuntu 26.04 LTS
- Python3.14.4
- requests2.34.2
- paramiko5.0.0
- TimeAbout 22 min
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.
- pytest9.1.1
- PyYAML6.0.3
- boto3 / botocore1.43.78
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Control node - every script in this path runs here | 2 Core | 4 GB | 50 GB |
This guide includes
Use this because automation is exactly the code nobody watches run. This matters because a test suite is the only thing that notices a parser broke before the scheduled job quietly does the wrong thing.
- writing a first test file, and reading what a failure actually tells you
- turning one test function into many cases with parametrize
- using a fixture, and a real temporary directory
- learning the flags worth knowing, and pytest's four exit codes
- sharing setup in conftest.py, measuring coverage, and putting the flags in a config file
Before you start
- guide 8 - where pytest is installed.
- guide 14 - the parser under test here.
- guide 5 - and pytest's own.
-
The thing worth testing
-
A first test file
-
What a failure actually tells you
-
The same failures, shorter
-
One test, many cases
-
A fixture, and a real temporary directory
-
The flags worth knowing
-
pytest's own exit codes
-
conftest.py, for what every test needs
-
And what the tests do not cover
-
A config file so nobody has to remember the flags