Pytest plugin for PagerDuty integration via automation testing.
Project description
pytest-pagerduty
This plugin integrates PagerDuty incident management with your pytest test suite.
It automatically triggers a PagerDuty incident when a test failure occurs, allowing for real-time monitoring and faster incident response.
Installation
pip install pytest-pagerduty
Configuration
Set the required PagerDuty credentials as environment variables or provide them via pytest options:
# Use your PagerDuty API key or OAuth token either as environment variables or pytest options
export PAGERDUTY_API_KEY="your_api_key"
export PAGERDUTY_OAUTH_TOKEN="your_oauth_token"
export PAGERDUTY_DEFAULT_FROM="your_business_email@example.com"
Alternatively, use pytest command-line options:
pytest --pagerduty_api_key=your_api_key \
--pagerduty_oauth_token=your_oauth_token \
--pagerduty_default_from=your_business_email@example.com
You can also configure options through a pytest.ini file or section in the project configuration file:
[pytest]
pagerduty_api_key = your_api_key
pagerduty_oauth_token = your_oauth_token
pagerduty_default_from = your_business_email@example.com
Plugin registration
Ensure the plugin is registered globally by adding the following line to tests/conftest.py:
pytest_plugins = ["pytest_pagerduty.plugin"]
Usage
Marking tests for incident reporting
Use the pagerduty_fixture fixture to enable the plugin and the
@pytest.mark.pagerduty_trigger_incident marker to specify tests that should trigger PagerDuty incidents on failure.
import pytest
@pytest.mark.usefixtures("pagerduty_fixture")
@pytest.mark.pagerduty_trigger_incident(service_id="XIXOOXZ", urgency="high", assignee_id="SE169QA")
def test_critical_feature():
"""Verifies the critical feature behavior."""
assert False # This will trigger a PagerDuty incident and assign it to the specified user who will be responsible for resolving the incident
@pytest.mark.usefixtures("pagerduty_fixture")
@pytest.mark.pagerduty_trigger_incident(service_id="XIXOOXZ", urgency="low")
def test_service_failure():
"""Validates the service availability."""
assert False # This will trigger a PagerDuty incident with low urgency level
@pytest.mark.usefixtures("pagerduty_fixture")
@pytest.mark.pagerduty_trigger_incident(service_id="IIXOOXZ", urgency="high")
def test_critical_flow():
"""Validates the critical flow.
Critical test case which verifies next behavior:
- Authentication to the production environment
- Data retrieval from the X service
- Data processing via the Y service
- Data validation via the Z service
- Data storage to the production database
- Sending a notification to the Billing team
"""
...
expected_status_code = 201
actual_status_code = 502
assert expected_status_code == actual_status_code # This will trigger a PagerDuty incident and docstring will be included in the incident details
Marker Parameters
-
service_id(required): The PagerDuty service ID. -
priority_id(optional): Incident priority reference. -
escalation_policy_id(optional): Escalation policy ID. -
urgency(optional): Incident urgency (e.g., "high" or "low"). -
incident_key(optional): Deduplication key to prevent duplicate incidents. -
details(optional): Custom message for the incident body. -
assignee_id(optional): User ID to assign the incident.
PagerDuty incident structure
When a test fails, an incident is created with the following information:
-
Summary: Test failure message, including the test name.
-
Details: Test docstring and assertion error traceback.
-
Service: Linked to the specified PagerDuty service.
-
Urgency, Escalation Policy: Configurable via markers.
MIT License
Distributed under the MIT License. See LICENSE for more information.
Useful links
- python-pagerduty APIs: Clients for PagerDuty’s APIs documentation.
- python-pagerduty: GitHub repository for the python-pagerduty library.
- API Reference: PagerDuty REST API documentation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_pagerduty-1.0.0.tar.gz.
File metadata
- Download URL: pytest_pagerduty-1.0.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
53f3e6bff8714fa0496d9a711823f68b1873b795b0d251138fe3dc1b69a5f036
|
|
| MD5 |
bcda2fb8b5b0cc1dc2b51f149711f65f
|
|
| BLAKE2b-256 |
f8a139bafb16e1549d75be5ca5c8104f857d789bbd9e2983df6428a11a3fe6b0
|
File details
Details for the file pytest_pagerduty-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pytest_pagerduty-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a630861a465544b9075c95511169f7eb3846a47792363941496fcf070c23f92
|
|
| MD5 |
ca6f09a68eeb7504715f96d97601a119
|
|
| BLAKE2b-256 |
bbe869d3bccea283f1820fa62c7124a356e539403a91de668adedeba96e2f18c
|