0
0
PyTesttesting~8 mins

JUnit XML reporting for CI in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - JUnit XML reporting for CI
Folder Structure
project-root/
├── tests/
│   ├── test_example.py
│   └── test_login.py
├── reports/
│   └── junit-report.xml
├── conftest.py
├── pytest.ini
└── requirements.txt
  
Test Framework Layers
  • Tests: Located in tests/ folder, contains test scripts like test_example.py.
  • Fixtures & Config: conftest.py holds reusable setup/teardown code and fixtures.
  • Reports: reports/ folder stores generated JUnit XML reports for CI consumption.
  • Configuration: pytest.ini configures pytest options including JUnit XML output.
  • Dependencies: requirements.txt lists Python packages like pytest.
Configuration Patterns

Use pytest.ini to configure JUnit XML reporting and other options:

[pytest]
addopts = --junitxml=reports/junit-report.xml
  

This tells pytest to generate a JUnit XML report in the reports/ folder after tests run.

For environment-specific settings (like test URLs or credentials), use environment variables or separate config files loaded in conftest.py.

Test Reporting and CI/CD Integration
  • JUnit XML report (reports/junit-report.xml) is a standard format understood by most CI tools (Jenkins, GitHub Actions, GitLab CI).
  • CI pipelines run pytest with the configured --junitxml option to generate the report.
  • CI tools parse the XML report to show test results, failures, and trends in their dashboards.
  • Reports help quickly identify failing tests and maintain test quality over time.
Best Practices
  1. Keep test reports in a dedicated reports/ folder to separate outputs from source code.
  2. Use pytest.ini or command-line options to control report generation consistently.
  3. Integrate report generation into CI pipelines to automate test result collection.
  4. Use environment variables or config files for sensitive data, never hard-code credentials.
  5. Regularly review CI test reports to catch flaky or failing tests early.
Self Check

Where in this folder structure would you add a new test file for user registration?

Key Result
Use pytest with a dedicated reports folder and pytest.ini config to generate JUnit XML reports for CI integration.