Skip to main content

A test runner for python unittest with async support.

Project description

rut - Run Unit Tests

rut is a modern and fully-featured test runner for Python's unittest framework, with simplicity as a core design goal.

Sponsored by asserto.ai

Features

  • Blazingly Fast (to type): rut is only 3 characters.
  • Built-in support for async code.
  • Test discovery with keyword-based filtering.
  • Code coverage support.

Why use rut?

For the unittest User: A Modern Upgrade

Are you a fan of the stability and explicitness of unittest, but wish you had a better runner experience? rut is your drop-in upgrade.

  • Zero-Config Discovery: Just run rut. No more writing if __name__ == '__main__': boilerplate.
  • First-Class asyncio Support: rut automatically detects and runs async tests correctly. No more asyncio.run() wrappers.
  • Powerful Filtering: Run exactly the tests you want with simple keyword filtering (-k "my_feature").
  • Integrated Coverage: Get a full coverage report with a single flag (--cov).

For the pytest User: Simplicity and Power, Reunited

Do you appreciate the power of modern test runners, but find yourself wrestling with the complexity and "magic" of third-party frameworks? rut offers a compelling alternative, embracing the explicitness of the standard library.

  • No Magic, Just Python: rut is built directly on unittest. There are no hidden hooks, complex fixture systems, or assertion rewriting. Your tests are plain, debuggable Python.
  • Familiar, Powerful Features: Get the features you rely on, like keyword filtering (-k), failing fast (-x), and seamless asyncio support, without the overhead of a large framework.
  • A Leaner Dependency Tree: rut is a single, focused tool, not a sprawling ecosystem. Keep your project's dependencies clean and understandable.

Installation

Add rut as a development dependency to your project:

uv add --dev rut

Usage

rut [options] [path]

Example

To run all tests in the tests/ directory that have the word "feature" in their name, you would run:

rut -k "feature"

Options

Option Short Description
--keyword -k Only run tests that match the given keyword.
--exitfirst -x Exit on the first failure.
--capture -s Disable all output capturing.
--cov Run with code coverage.
--test-base-dir The base directory for conftest.py discovery.

Positional Arguments

Argument Description Default
path The path to the tests to be discovered. tests

Configuration

rut can be configured via the [tool.rut] section in your pyproject.toml file.

coverage_source

To specify the source directories for coverage reporting, use the coverage_source key. If not configured, the default source is ["src", "tests"].

[tool.rut]
coverage_source = ["my_app", "libs/my_lib"]

warning_filters

To add custom warning filters, use the warning_filters key. The format for each filter is a string that follows the warnings.filterwarnings format: action:message:category:module.

[tool.rut]
warning_filters = [
    "error::UserWarning:pydantic",
]

test_base_dir

To specify the base directory for conftest.py discovery, use the test_base_dir key.

[tool.rut]
test_base_dir = "my_tests"

Writing Tests

Basic Tests

For standard, synchronous tests, you can use the standard unittest.TestCase.

import unittest

class MyTest(unittest.TestCase):
    def test_something(self):
        self.assertEqual(1 + 1, 2)

Async Tests

To write an asynchronous test, you must use unittest.IsolatedAsyncioTestCase as the base class for your test case. rut will automatically detect and correctly run async def test methods within these classes.

import asyncio
import unittest

class MyAsyncTest(unittest.IsolatedAsyncioTestCase):
    async def test_something_async(self):
        await asyncio.sleep(0.01)
        self.assertTrue(True)

Advanced

Session-Level Setup and Teardown

For more complex testing scenarios, you may need to run setup code once before any tests start and teardown code once after all tests have finished. rut supports this with special, automatically-discovered "hook" functions.

To use this feature, create a conftest.py file in your test base directory (usually tests/). rut will automatically find and execute the following functions if they are defined:

  • rut_session_setup(): Executed once before the test session begins.
  • rut_session_teardown(): Executed once after the test session ends, even if tests fail.

Both of these functions can be synchronous (def) or asynchronous (async def).

Example conftest.py

# tests/conftest.py
import asyncio
from my_app.database import connect_db, disconnect_db

async def rut_session_setup():
    print("Setting up the test database...")
    await connect_db()

async def rut_session_teardown():
    print("Tearing down the test database...")
    await disconnect_db()

Detecting rut

When rut is running, it sets the environment variable TEST_RUNNER to the value rut. You can use this in your test code to enable or disable functionality that is specific to the test runner.

import os

if os.environ.get('TEST_RUNNER') == 'rut':
    # Do something specific to rut
    pass

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rut-0.2.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rut-0.2.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file rut-0.2.1.tar.gz.

File metadata

  • Download URL: rut-0.2.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for rut-0.2.1.tar.gz
Algorithm Hash digest
SHA256 708d59bf0ff70b388ca9eb13d3fad5303e0e6dd73c9a3b8bb72f5134348d742c
MD5 57c772afc9fbe2534d6bd4a0f56fe128
BLAKE2b-256 2af9a7fbd85c22f9ffa68e2968f67c8060075ab201edff2017506f82dac99557

See more details on using hashes here.

File details

Details for the file rut-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: rut-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for rut-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bb06b2b12b59ebc11b5c055d75e2eb5e6af8d781363ba45b5fb9d2012ed34bbd
MD5 c63c2806094be67aa77babe7f5b8695f
BLAKE2b-256 da859153450adcf824b23698386f21df5cb097d17f716fb369cb6e14628e07fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page