Skip to content

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:

bash
pytest --junitxml=results/junit-report.xml

Option B: pytest.ini Configuration ​

Create or update your pytest.ini file:

ini
[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:

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:

bash
# 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:

bash
npx testbeats@latest publish \
  --api-key '<your-api-key>' \
  --slack '<your-webhook-url>' \
  --junit 'results/junit-report.xml'

For more control and CI/CD integration, create a testbeats.config.json:

json
{
  "api_key": "<your-api-key>",
  "targets": [
    {
      "name": "slack",
      "inputs": {
        "url": "<your-webhook-url>"
      }
    }
  ],
  "results": [
    {
      "type": "junit",
      "files": ["results/junit-report.xml"]
    }
  ]
}

Then run:

bash
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:

makefile
test-report:
	pytest
	npx testbeats@latest publish -c testbeats.config.json

Using shell script (test-report.sh):

bash
#!/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:

yaml
- 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:

  1. Verify the file path in your configuration:

    bash
    # Check if file exists
    ls results/junit-report.xml
  2. Ensure pytest.ini or pyproject.toml has correct configuration

  3. Create results directory if it doesn't exist:

    bash
    mkdir -p results

Tests Run But No Report Sent ​

Problem: TestBeats runs without errors but no report appears.

Solutions:

  1. Verify webhook URL is correct and accessible
  2. Check API key is valid (if using one)
  3. 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 ​


Need help with a different framework? Check Framework Integrations →

Released under the MIT License.