0
0
PyTesttesting~8 mins

Test result publishing in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Test result publishing
Folder Structure
project-root/
├── tests/
│   ├── test_example.py
│   └── conftest.py
├── reports/
│   └── latest_report.html
├── pytest.ini
├── requirements.txt
└── ci/
    └── pipeline.yml
    
Test Framework Layers
  • Tests Layer: Contains test cases using pytest functions or classes inside tests/.
  • Fixtures & Setup: conftest.py holds reusable setup code and fixtures for tests.
  • Reporting Layer: Generates test result files (HTML, XML) saved in reports/.
  • Configuration Layer: pytest.ini configures pytest options like markers and report formats.
  • CI/CD Layer: Pipeline scripts in ci/ automate test runs and publish reports.
Configuration Patterns

Use pytest.ini to set default options, for example:

[pytest]
addopts = --html=reports/latest_report.html --self-contained-html --junitxml=reports/results.xml
markers =
    smoke: Quick smoke tests
    regression: Full regression suite
    

Manage environments and credentials via environment variables or separate config files loaded in conftest.py.

Example: Use os.environ in fixtures to select test environment or browser.

Test Reporting and CI/CD Integration
  • Generate HTML reports using pytest-html plugin for easy visual results.
  • Generate XML reports (JUnit format) for CI tools like Jenkins, GitHub Actions.
  • Store reports in reports/ folder for easy access and archiving.
  • CI pipeline (ci/pipeline.yml) runs tests, collects reports, and publishes them as build artifacts or uploads to dashboards.
  • Example: GitHub Actions workflow triggers pytest, uploads reports/latest_report.html as an artifact.
Best Practices for Test Result Publishing
  1. Consistent Report Location: Always save reports in a dedicated folder like reports/ for easy retrieval.
  2. Use Standard Formats: Generate both human-readable (HTML) and machine-readable (XML) reports.
  3. Integrate with CI/CD: Automate test runs and report publishing in your pipeline for fast feedback.
  4. Keep Reports Self-Contained: Use plugins options like --self-contained-html to avoid missing resources.
  5. Secure Sensitive Data: Avoid printing credentials or sensitive info in reports.
Self Check

Where in this folder structure would you add a new pytest plugin configuration to generate a JSON test report?

Key Result
Organize pytest tests, configs, and reports clearly; automate report generation and publishing in CI/CD pipelines.