stestr: Parallel Python Test Runner

4.2.1 · active · verified Thu Apr 16

stestr is a parallel Python test runner built around the `subunit` stream protocol. It provides a robust command-line interface for discovering, running, and managing tests, designed to efficiently handle large test suites and integrate well into continuous integration pipelines. The current version is 4.2.1, with minor and patch releases occurring every few months to enhance compatibility and add features.

Common errors

Warnings

Install

Quickstart

This quickstart creates a temporary directory, sets up a basic `unittest` test suite and a `setup.cfg` for stestr configuration, then runs `stestr init` to initialize the test repository and `stestr run` to execute the tests. It demonstrates the typical command-line workflow for stestr.

import os
import subprocess
import shutil
import tempfile

# Create a temporary directory for demonstration
temp_dir = tempfile.mkdtemp()
current_dir = os.getcwd()
os.chdir(temp_dir)

try:
    # 1. Create a 'tests' directory
    os.makedirs("tests")

    # 2. Create a simple unittest file
    test_code = """
import unittest

class ExampleTests(unittest.TestCase):
    def test_success(self):
        self.assertTrue(True)

    def test_failure(self):
        self.assertFalse(False, "This test is designed to fail.")

    def test_another_success(self):
        self.assertEqual(2 + 2, 4)
"""
    with open("tests/test_my_app.py", "w") as f:
        f.write(test_code)

    # 3. Create a stestr configuration file (e.g., setup.cfg or .stestr.conf)
    # Using setup.cfg for modern compatibility
    config_code = """
[stestr]
test_path = tests
test_file_prefix = test_
"""
    with open("setup.cfg", "w") as f:
        f.write(config_code)

    print(f"Working directory: {os.getcwd()}")
    print("\n--- Initializing stestr repository ---")
    subprocess.run(["stestr", "init"], check=True)
    print("\n--- Running tests with stestr ---")
    result = subprocess.run(["stestr", "run"], capture_output=True, text=True)
    print(result.stdout)
    if result.stderr:
        print("STDERR:")
        print(result.stderr)

    if result.returncode != 0:
        print("\nNote: stestr run reported failures (expected for demonstration).")
    else:
        print("\nstestr run completed successfully.")

except subprocess.CalledProcessError as e:
    print(f"Error: stestr command failed. Command: {' '.join(e.cmd)}")
    print(f"Stdout: {e.stdout}")
    print(f"Stderr: {e.stderr}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    os.chdir(current_dir)
    shutil.rmtree(temp_dir)
    print(f"\nCleaned up temporary directory: {temp_dir}")

view raw JSON →