Green is a clean, colorful, fast python test runner.
—
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.
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}")
"""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
"""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)
"""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}")
"""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.
"""[green]
verbose = 2
processes = 4
run-coverage = true
omit-patterns = */tests/*,*/migrations/*
junit-report = test-results.xml
debug = false
keep-reports = false# 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=4Green resolves configuration in this exact order (later settings override earlier ones):
# 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')verbose (int): Verbosity level 0-4no_color (bool): Disable colored outputdisable_windows (bool): Disable Windows-specific formattingdebug (int): Debug output levelfile_pattern (str): Test file pattern (default: 'test*.py')test_pattern (str): Test method patterntargets (list): Test targets to runprocesses (int): Number of parallel processes (0 = auto-detect)initializer (str): Worker process initializer functionfinalizer (str): Worker process finalizer functionrun_coverage (bool): Enable coverage reportingcoverage_config_file (str): Coverage config file pathomit_patterns (str): Coverage omit patterns (comma-separated)junit_report (str): JUnit XML report file pathkeep_reports (bool): Keep temporary report filesdjango_verbosity (int): Django-specific verbosity leveldevelopment.cfg:
[green]
verbose = 3
debug = 1
keep-reports = true
processes = 2ci.cfg:
[green]
verbose = 1
junit-report = test-results.xml
run-coverage = true
processes = 0 # auto-detect for CIproduction-test.cfg:
[green]
verbose = 0
processes = 8
omit-patterns = */tests/*,*/dev_tools/*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)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 = trueInstall with Tessl CLI
npx tessl i tessl/pypi-green