How to Integrate PyTest with TestBeats ​
This guide shows you how to send your PyTest test results to Slack, Microsoft Teams, or Google Chat using TestBeats.
What You'll Achieve ​
After following this guide, your PyTest tests will automatically send results to your team communication channels, complete with AI-powered failure analysis and detailed reporting.
Prerequisites ​
Before starting, ensure you have:
- A Python project with PyTest tests
- TestBeats installed (
npx testbeats@latest --version
) - A webhook URL for your communication platform (Slack, Teams, or Chat)
- (Optional) TestBeats API key from app.testbeats.com
Step 1: Configure PyTest to Generate JUnit XML Reports ​
PyTest needs to output test results in JUnit XML format for TestBeats to process.
Option A: Command Line Flag ​
Run your tests with the --junitxml
flag:
pytest --junitxml=results/junit-report.xml
Option B: pytest.ini Configuration ​
Create or update your pytest.ini
file:
[tool:pytest]
junit_family = xunit2
junit_logging = all
addopts = --junitxml=results/junit-report.xml
Option C: pyproject.toml Configuration ​
For modern Python projects using pyproject.toml
:
[tool.pytest.ini_options]
junit_family = "xunit2"
junit_logging = "all"
addopts = "--junitxml=results/junit-report.xml"
Step 2: Run Your Tests ​
Execute your PyTest tests as usual:
# Run all tests
pytest
# Run specific test file
pytest tests/test_example.py
# Run with coverage
pytest --cov=src
This will generate a JUnit XML file at results/junit-report.xml
.
Step 3: Choose Your Integration Method ​
You have two options to send results to TestBeats:
Option A: Command Line (Quick Setup) ​
For quick testing or simple setups:
npx testbeats@latest publish \
--api-key '<your-api-key>' \
--slack '<your-webhook-url>' \
--junit 'results/junit-report.xml'
Option B: Configuration File (Recommended) ​
For more control and CI/CD integration, create a testbeats.config.json
:
{
"api_key": "<your-api-key>",
"targets": [
{
"name": "slack",
"inputs": {
"url": "<your-webhook-url>"
}
}
],
"results": [
{
"type": "junit",
"files": ["results/junit-report.xml"]
}
]
}
Then run:
npx testbeats@latest publish -c testbeats.config.json
Step 4: Integrate with Your Workflow ​
For Local Development ​
Add a script to your project:
Using Makefile:
test-report:
pytest
npx testbeats@latest publish -c testbeats.config.json
Using shell script (test-report.sh
):
#!/bin/bash
pytest
npx testbeats@latest publish -c testbeats.config.json
For CI/CD Pipeline ​
Add TestBeats to your CI workflow after running tests:
GitHub Actions:
- name: Run PyTest Tests
run: pytest
- name: Publish Results
run: npx testbeats@latest publish -c testbeats.config.json
env:
TEST_BEATS_API_KEY: ${{ secrets.TESTBEATS_API_KEY }}
Troubleshooting ​
No JUnit XML File Generated ​
Problem: TestBeats can't find the JUnit XML file.
Solutions:
Verify the file path in your configuration:
bash# Check if file exists ls results/junit-report.xml
Ensure pytest.ini or pyproject.toml has correct configuration
Create results directory if it doesn't exist:
bashmkdir -p results
Tests Run But No Report Sent ​
Problem: TestBeats runs without errors but no report appears.
Solutions:
- Verify webhook URL is correct and accessible
- Check API key is valid (if using one)
- Ensure JUnit XML file contains test data:bash
cat results/junit-report.xml
Example Integration ​
For a complete working example, see the PyTest TestBeats example repository.
Next Steps ​
- Configure additional targets for multiple communication channels
- Set up CI/CD integration for automated reporting
- Explore advanced extensions for enhanced reporting
- Learn about TestBeats Portal features
Need help with a different framework? Check Framework Integrations →