CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-green

Green is a clean, colorful, fast python test runner.

Pending
Overview
Eval results
Files

configuration.mddocs/

Configuration

Green's configuration system provides hierarchical configuration merging from multiple sources including command-line arguments, configuration files, and environment variables with well-defined precedence rules.

Capabilities

Argument Parsing

Parses command-line arguments and creates a configuration namespace with all available Green options.

def parseArguments(argv=None):
    """
    Parse command-line arguments for Green.
    
    Args:
        argv (list, optional): List of command-line arguments. 
                              If None, uses sys.argv[1:].
    
    Returns:
        argparse.Namespace: Parsed arguments with all Green options including:
            - targets: List of test targets to run
            - verbose: Verbosity level (0-4)
            - processes: Number of parallel processes
            - run_coverage: Enable coverage reporting
            - junit_report: Path for JUnit XML output
            - config: Custom config file path
            - and many more options
    
    Example:
        args = parseArguments(['tests/', '--verbose', '2', '--run-coverage'])
        print(f"Targets: {args.targets}")
        print(f"Verbose level: {args.verbose}")
    """

Configuration Merging

Merges configuration from multiple sources with proper precedence: config files, environment variables, and command-line arguments.

def mergeConfig(args, testing=False):
    """
    Merge configuration from files, environment, and command-line arguments.
    
    Configuration precedence (last wins):
    1. $HOME/.green
    2. Environment variable $GREEN_CONFIG file
    3. setup.cfg in current directory
    4. .green in current directory  
    5. --config FILE specified file
    6. Command-line arguments
    
    Args:
        args (argparse.Namespace): Parsed command-line arguments
        testing (bool): Enable testing mode behavior
    
    Returns:
        argparse.Namespace: Merged configuration with all sources combined
    
    Example:
        args = parseArguments(['tests/'])
        merged_args = mergeConfig(args)
        # Now merged_args contains settings from all config sources
    """

Configuration File Loading

Loads configuration from specific files using configparser format.

def getConfig(filepath=None):
    """
    Load configuration from a specific file.
    
    Args:
        filepath (str, optional): Path to configuration file. 
                                 If None, returns empty config.
    
    Returns:
        configparser.ConfigParser: Configuration object with Green settings
    
    Example:
        config = getConfig('/path/to/green.cfg')
        if config.has_section('green'):
            verbose = config.getint('green', 'verbose', fallback=1)
    """

Default Arguments

Provides default argument values for all Green configuration options.

def get_default_args():
    """
    Get default argument values for all Green options.
    
    Returns:
        argparse.Namespace: Default configuration with all options set to
                           their default values
    
    Example:
        defaults = get_default_args()
        print(f"Default verbosity: {defaults.verbose}")
        print(f"Default processes: {defaults.processes}")
    """

Shell Completion Helper

Helper class for storing option lists used in shell completion functionality.

class StoreOpt:
    """
    Helper class for storing option lists for shell completion.
    
    Used internally by Green to track available options for bash/zsh completion.
    """

Configuration File Formats

INI Format (.green, setup.cfg)

[green]
verbose = 2
processes = 4
run-coverage = true
omit-patterns = */tests/*,*/migrations/*
junit-report = test-results.xml
debug = false
keep-reports = false

Environment Variables

# Specify custom config file
export GREEN_CONFIG=/path/to/custom/green.cfg

# Alternative to config files (not recommended for complex configs)
export GREEN_VERBOSE=2
export GREEN_PROCESSES=4

Configuration Locations and Precedence

Green resolves configuration in this exact order (later settings override earlier ones):

  1. $HOME/.green - Global user configuration
  2. $GREEN_CONFIG - Custom config file from environment variable
  3. setup.cfg - Project-level configuration in current directory
  4. .green - Directory-specific configuration
  5. --config FILE - Explicitly specified config file
  6. Command-line arguments - Direct command-line options

Example Configuration Scenarios

# Scenario 1: Basic programmatic configuration
from green.config import parseArguments, mergeConfig

# Parse with defaults
args = parseArguments()
# Merge with config files
final_config = mergeConfig(args)

# Scenario 2: Custom arguments with config merging  
args = parseArguments(['tests/', '--verbose', '2'])
final_config = mergeConfig(args, testing=True)

# Scenario 3: Loading specific config file
from green.config import getConfig
config = getConfig('custom-green.cfg')

Available Configuration Options

Output Control

  • verbose (int): Verbosity level 0-4
  • no_color (bool): Disable colored output
  • disable_windows (bool): Disable Windows-specific formatting
  • debug (int): Debug output level

Test Discovery

  • file_pattern (str): Test file pattern (default: 'test*.py')
  • test_pattern (str): Test method pattern
  • targets (list): Test targets to run

Execution Control

  • processes (int): Number of parallel processes (0 = auto-detect)
  • initializer (str): Worker process initializer function
  • finalizer (str): Worker process finalizer function

Coverage Integration

  • run_coverage (bool): Enable coverage reporting
  • coverage_config_file (str): Coverage config file path
  • omit_patterns (str): Coverage omit patterns (comma-separated)

Output Formats

  • junit_report (str): JUnit XML report file path
  • keep_reports (bool): Keep temporary report files

Django Integration

  • django_verbosity (int): Django-specific verbosity level

Advanced Configuration Examples

Multi-Environment Setup

development.cfg:

[green]
verbose = 3
debug = 1
keep-reports = true
processes = 2

ci.cfg:

[green]
verbose = 1  
junit-report = test-results.xml
run-coverage = true
processes = 0  # auto-detect for CI

production-test.cfg:

[green]
verbose = 0
processes = 8
omit-patterns = */tests/*,*/dev_tools/*

Programmatic Configuration Override

from green.config import parseArguments, mergeConfig

# Start with file-based config
args = parseArguments(['tests/'])
config = mergeConfig(args)

# Override specific settings programmatically
config.verbose = 3
config.processes = 1  # Force single-process for debugging
config.debug = 2
config.junit_report = 'debug-results.xml'

# Use the modified config
from green.runner import run
from green.loader import GreenTestLoader
from green.output import GreenStream
import sys

loader = GreenTestLoader()
suite = loader.loadTargets(config.targets)
stream = GreenStream(sys.stdout)
result = run(suite, stream, config)

Complex Project Configuration

For large projects with multiple test suites:

setup.cfg:

[green]
# Base configuration
verbose = 1
processes = 0
run-coverage = true
omit-patterns = */migrations/*,*/venv/*,*/node_modules/*

# Different test patterns for different components
file-pattern = test*.py

[green:unit]
# Unit test specific settings (used with custom runner)
verbose = 2
processes = 4

[green:integration] 
# Integration test settings
verbose = 1
processes = 1
keep-reports = true

Install with Tessl CLI

npx tessl i tessl/pypi-green

docs

cli.md

configuration.md

django-integration.md

index.md

junit-xml.md

output-formatting.md

setuptools-integration.md

test-execution.md

test-loading.md

test-results.md

tile.json