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
- Python3.14.4
- Control nodeUbuntu 26.04 LTS
- Managed hostsRHEL 10.0
- requests2.34.2
- paramiko5.0.0
- pytest9.1.1
- PyYAML6.0.3
- boto3 / botocore1.43.78
- TimeAbout 22 min
- Reviewed24 August 2026
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.
| 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 |
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