Skip to main content

A lightweight Python test runner with method-level discovery and coverage reporting

Project description

PyRules Runner

A simple, efficient Python testing framework that provides test discovery at the method level, code coverage reporting, and basic linting capabilities. Unlike more complex testing frameworks, this lightweight solution focuses on simplicity and ease of use while still providing detailed test results.

Features

  • Method-level test discovery: Identifies individual test methods within unittest.TestCase classes
  • Multiple test types: Support for unit, integration, end-to-end, and regression tests
  • Code coverage reporting: Console and HTML coverage reports
  • Basic linting: PEP8 style checking with flake8
  • Simple configuration: JSON-based configuration for test groups and patterns
  • Clear reporting: Emoji-enhanced console output with detailed error information
  • Easy installation: Installable via pip with command-line interface

Installation

From PyPI (when published)

pip pyrulesrunner

From PyPI (when published)

pip install pyrulesrunner

From Source

git clone <repository-url>
cd pyrulesrunner
pip install -e .

With Optional Dependencies

# Install with coverage support
pip install pyrulesrunner[coverage]

# Install with linting support
pip install pyrulesrunner[lint]

# Install with all optional features
pip install pyrulesrunner[all]

Legacy Installation (Single File)

You can also use the standalone testrules.py script:

# Download testrules.py to your project
# Install dependencies
pip install coverage flake8

Usage

After installation, you can use the testrules command from anywhere:

# Run all tests
testrules
testrules --all

# Run specific test types
testrules unit
testrules integration
testrules e2e
testrules regression

# Run specific modules
testrules test_module1 test_module2

# Run test groups (defined in config)
testrules core
testrules api

# Run linting only
testrules lint
testrules --lint-only

# Run comprehensive check (linting + all tests)
testrules check
testrules --check

# Use custom configuration
testrules --config my_config.json

# Disable coverage
testrules --no-coverage

# Show help
testrules --help

Alternative Usage

You can also run it as a Python module:

python -m testrules --all
python -m testrules unit

Legacy Usage (Single File)

If using the standalone script:

# Run all tests
python testrules.py

# Run specific test types
python testrules.py unit
python testrules.py integration
python testrules.py e2e
python testrules.py regression

# Run specific modules
python testrules.py test_module1 test_module2

# Run linting
python testrules.py lint
python testrules.py check

Configuration

The test runner uses a testrules.json configuration file to define test patterns, groups, and settings:

{
  "test_patterns": {
    "unit": ["test_*.py", "*_test.py", "unit_test_*.py"],
    "integration": ["integration_test_*.py", "*_integration_test.py"],
    "e2e": ["e2e_test_*.py", "*_e2e_test.py"],
    "regression": ["regression_test_*.py", "*_regression_test.py"]
  },
  "test_groups": {
    "all": [],
    "core": ["test_core1", "test_core2"],
    "api": ["test_api1", "test_api2"],
    "fast": ["test_quick1", "test_quick2"],
    "slow": ["test_slow1", "test_slow2"]
  },
  "coverage_enabled": true,
  "html_coverage": true,
  "html_coverage_dir": "htmlcov",
  "coverage_config": {
    "source": ["."],
    "omit": [
      "*/tests/*",
      "*/test_*",
      "*/__pycache__/*",
      "*/venv/*",
      "*/env/*",
      "setup.py"
    ]
  },
  "lint_config": {
    "enabled": true,
    "max_line_length": 88,
    "ignore": ["E203", "W503"]
  }
}

Using in Your Project

  1. Install the package:

    pip install pyrulesrunner[all]
    
  2. Create a testrules.json configuration file in your project root:

    {
      "test_patterns": {
        "unit": ["test_*.py", "*_test.py"],
        "integration": ["integration_*.py"],
        "e2e": ["e2e_*.py"]
      },
      "test_groups": {
        "fast": ["test_utils", "test_models"],
        "slow": ["test_integration", "test_e2e"]
      },
      "coverage_enabled": true,
      "html_coverage": true
    }
    
  3. Run your tests:

    # Run all tests
    testrules
    
    # Run only fast tests
    testrules fast
    
    # Run unit tests with coverage
    testrules unit
    
    # Run tests and linting
    testrules check
    

Example Output

๐Ÿš€ Lightweight Test Runner
==================================================
๐Ÿ“„ Loaded configuration from testrules.json
Command: all tests

๐Ÿ” Discovering tests...
๐Ÿ“ Found 4 test files

๐Ÿงช Discovering test methods...
๐ŸŽฏ Total test methods discovered: 15

๐Ÿš€ Running tests...
๐Ÿ“ฆ Running tests in module: test_calculator
  [1/15] test_calculator.TestCalculator.test_add ... โœ… PASS (0.001s)
  [2/15] test_calculator.TestCalculator.test_subtract ... โœ… PASS (0.001s)

============================================================
๐Ÿงช TEST SUMMARY
============================================================
โœ… Passed:        14
โŒ Failed:        1
๐Ÿ’ฅ Errors:        0
๐Ÿ“Š Total:         15
๐Ÿ“ˆ Success Rate:  93.33%
โฑ๏ธ  Execution Time: 0.05 seconds

๐Ÿ“Š COVERAGE REPORT
============================================================
Name                 Stmts   Miss  Cover
----------------------------------------
calculator.py           20      2    90%
utils.py               15      0   100%
----------------------------------------
TOTAL                   35      2    94%

โœ… All checks passed successfully!

Writing Tests

The test runner works with standard Python unittest framework:

import unittest

class TestExample(unittest.TestCase):
    def test_addition(self):
        """Test basic addition."""
        self.assertEqual(2 + 2, 4)

    def test_subtraction(self):
        """Test basic subtraction."""
        self.assertEqual(5 - 3, 2)

if __name__ == '__main__':
    unittest.main()

Test Types

Unit Tests

  • File patterns: test_*.py, *_test.py, unit_test_*.py
  • Purpose: Test individual functions or classes in isolation
  • Example: test_calculator.py

Integration Tests

  • File patterns: integration_test_*.py, *_integration_test.py
  • Purpose: Test interaction between multiple components
  • Example: integration_test_database.py

End-to-End Tests

  • File patterns: e2e_test_*.py, *_e2e_test.py
  • Purpose: Test complete user workflows
  • Example: e2e_test_user_registration.py

Regression Tests

  • File patterns: regression_test_*.py, *_regression_test.py
  • Purpose: Test previously fixed bugre they don't reoccur
  • Example: regression_test_issue_123.py

Programmatic Usage

You can also use the test runner programmatically:

from testrules import TestRunner, Config

# Create configuration
config = Config({
    "test_patterns": {
        "unit": ["test_*.py"]
    },
    "coverage_enabled": True
})

# Create and run test runner
runner = TestRunner(config)
test_results, coverage_obj = runner.run_tests(test_type="unit")

print(f"Tests run: {test_results.total}")
print(f"Passed: {test_results.passed}")
print(f"Failed: {test_results.failed}")

Project Structure

your_project/
โ”œโ”€โ”€ testrules.json           # Configuration file (optional)
โ”œโ”€โ”€ src/                     # Your source code
โ”‚   โ”œโ”€โ”€ module1.py
โ”‚   โ””โ”€โ”€ module2.py
โ”œโ”€โ”€ tests/                   # Your test files
โ”‚   โ”œโ”€โ”€ test_module1.py      # Unit tests
โ”‚   โ”œโ”€โ”€ test_module2.py      # Unit tests
โ”‚   โ”œโ”€โ”€ integration_test_api.py    # Integration tests
โ”‚   โ”œโ”€โ”€ e2e_test_workflow.py       # End-to-end tests
โ”‚   โ””โ”€โ”€ regression_test_bug_fix.py # Regression tests
โ””โ”€โ”€ htmlcov/                 # HTML coverage reports (generated)

Development

To contribute to the project:

git clone <repository-url>
cd pyrulesrunner
pip install -e .[dev]

# Run the test runner's own tests
testrules test_testrules

# Run all example tests
testrules comprehensive

Requirements

  • Python 3.7+
  • Optional: coverage>=6.0 for coverage reporting
  • Optional: flake8>=4.0 for linting

Troubleshooting

Common Issues

  1. Module Import Errors

    • Ensure your test files are in the Python path
    • Check that all dependencies are installed
    • Verify file names match the expected patterns
  2. Coverage Not Working

    • Install coverage: pip install pyrulesrunner[coverage]
    • Ensure coverage is enabled in configuration
  3. Linting Not Working

    • Install flake8: pip install pyrulesrunner[lint]
    • Check that your Python files are accessible

Getting Help

If you encounter issues:

  1. Check that all dependencies are installed
  2. Verify your test file naming follows the expected patterns
  3. Ensure your test files contain valid unittest.TestCase classes
  4. Check the configuration file syntax if using custom settings

Contributing

This is a lightweight test runner designed for simplicity. If you need more advanced features, consider using pytest or other full-featured testing frameworks.

License

This project is open source under the MIT License. Feel free to modify and distribut

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

pyrulesrunner-1.0.3.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

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

pyrulesrunner-1.0.3-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file pyrulesrunner-1.0.3.tar.gz.

File metadata

  • Download URL: pyrulesrunner-1.0.3.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for pyrulesrunner-1.0.3.tar.gz
Algorithm Hash digest
SHA256 072833cdb63eee80cb15c10af3015d108f0cfbe6973d1b07bfcf524a2186feeb
MD5 17b0186fca19039e8c69c8d46a34aa51
BLAKE2b-256 51346d1a002e930ea72fdd94f67e808c300fffd44b39e1d5c21d292dec3673c7

See more details on using hashes here.

File details

Details for the file pyrulesrunner-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: pyrulesrunner-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for pyrulesrunner-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 38c0591c6fdfe985c1a499829611270d8fe6a26f45b00f717e297bc7833aa7fc
MD5 79783d6dd3d897a881a0471c9e0c58a6
BLAKE2b-256 68599db9da1fa43e031b0d897ae067aef8091302fd758b7dad4e6e3880212d08

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