Python for IT Automation
Pytest & Security Unit-Testing Workbook
50 Original Tests | API Errors | Authentication | Remediation | Regression Testing
2026/2027 EDITION
Independent educational workbook. Not affiliated with or endorsed by Western Governors University.
It does not contain an official rubric, assessment solution, learner submission, credentials, or live lab
results.
,WGU D522 | Pytest & Security Unit-Testing Workbook | 2026/2027
Workbook Purpose
This workbook develops test-design skills for a Python IT-automation project. Each example states the behavior under test,
presents an original Pytest test, defines the expected outcome, and explains why the test matters. The examples use fictional
devices, addresses, credentials and services.
How to Work Through the Tests
• Create a separate practice repository; do not paste code into a graded submission without adapting and understanding it.
• Implement the named production functions in your own module, then run each test and interpret failures.
• Use mocks or fakes for networks, DNS, APIs and email. Never run remediation against an unauthorized system.
• Keep an error log containing the failed assertion, root cause, correction and regression test.
Suggested Practice Layout
Path Purpose
src/automation.py Validation, inventory, DNS and workflow functions
src/helpdesk.py API client and response mapping
tests/conftest.py Reusable fakes and fixtures
tests/test_validation.py Input and boundary tests
tests/test_inventory.py CSV and schema tests
tests/test_network.py Reachability and DNS tests
tests/test_workflow.py Remediation, rollback and idempotence
tests/test_security.py Credential, authorization and injection tests
Page 2
, WGU D522 | Pytest & Security Unit-Testing Workbook | 2026/2027
Shared Imports and Test Doubles
The examples assume these imports and project-defined exception types. Adapt import paths to the learner's own practice
repository.
import logging
import subprocess
import pytest
import requests
from src.automation import *
from src.helpdesk import *
# Project-defined examples:
# InventorySchemaError, InventoryRowError
# UnsupportedRecordType, HelpdeskUnavailable
# AuthenticationError, AuthorizationError
# ResponseSchemaError, CriticalRemediationError
# ConfigurationError, ImmutableAuditError
Fixture Design Guidance
Use `tmp_path` for files, `monkeypatch` for functions and environment variables, `caplog` for log assertions, `requests_mock` for
HTTP behavior, and small in-memory fakes for DNS, helpdesk and audit systems. Fixtures should begin each test in a known
state and avoid real infrastructure side effects.
Page 3