API Referenceļ
Complete reference documentation for all FailExtract classes, functions, and interfaces.
Note
FailExtract follows a progressive enhancement model. Core functionality is always available, with optional features providing additional capabilities.
Main Moduleļ
Failextract - Comprehensive test failure extraction and reporting library.
This library provides advanced capabilities for extracting, analyzing, and reporting test failures in pytest-based testing environments. It automatically captures detailed failure context including fixture information, source code, and dependency chains to aid in debugging and analysis.
- Key Features:
Automatic fixture extraction and dependency analysis
Multiple output formats (JSON always available, others via extras)
Thread-safe singleton design for concurrent test execution
Performance-optimized with intelligent caching
Extensible formatter system for custom output formats
Memory management with configurable limits
Integration with pytest through decorators
- Optional Feature Extras:
Formatters: YAML formatter (pip install failextract[formatters])
Config: Advanced configuration validation (pip install failextract[config])
CLI: Rich terminal output (pip install failextract[cli])
Example
Basic usage with the decorator:
>>> from failextract import extract_on_failure
>>>
>>> @extract_on_failure
>>> def test_example():
... assert 1 == 2, "This will be captured with full context"
Generate comprehensive reports:
>>> from failextract import FailureExtractor, OutputConfig
>>>
>>> extractor = FailureExtractor()
>>> config = OutputConfig("failures.json", format="json") # JSON always available
>>> extractor.save_report(config)
Author: Failextract Contributors License: Apache 2.0
- class failextract.CodeContextExtractor[source]ļ
Bases:
objectExtract and manage code context for test failures.
This class provides intelligent code context extraction with configurable depth and context lines. It implements caching for performance and thread-safe operation consistent with FailExtractās singleton patterns.
Features: - Configurable context lines before/after failure points - Intelligent source code caching with memory management - Thread-safe operation for concurrent test execution - Enhanced error handling for edge cases - Integration with existing source extraction functions
- _lockļ
Thread safety lock for cache operations
- Type:
- analyze_dependencies(filename, max_depth=3)[source]ļ
Analyze file dependencies and create dependency graph.
Performs recursive dependency analysis starting from a file to build a comprehensive dependency graph including direct imports, transitive dependencies, and circular dependency detection.
- Parameters:
- Returns:
ādirect_importsā: List of directly imported modules
ādependency_graphā: Nested dict of dependencies by depth level
ācircular_dependenciesā: List of detected circular dependencies
āunresolved_modulesā: List of modules that couldnāt be resolved
ātotal_dependenciesā: Count of all unique dependencies
- Return type:
Dictionary containing dependency analysis with keys
Example
>>> extractor = CodeContextExtractor() >>> deps = extractor.analyze_dependencies("test_file.py") >>> print(deps['total_dependencies']) 15
- analyze_imports(filename)[source]ļ
Analyze imports in a Python file using static analysis.
Extracts and categorizes all import statements from a Python file, providing detailed information about each import including line numbers, module names, aliases, and import types.
- Parameters:
filename (
str) ā Path to the Python file to analyze- Returns:
ātypeā: Import type (āimportā, āfrom_importā)
āmoduleā: Module name being imported
ānamesā: List of imported names (for from imports)
āaliasā: Alias used (as clause)
ālineā: Line number of the import statement
ālevelā: Relative import level (0 for absolute)
- Return type:
List of dictionaries containing import information with keys
Example
>>> extractor = CodeContextExtractor() >>> imports = extractor.analyze_imports("test_file.py") >>> print(imports[0]) {'type': 'import', 'module': 'os', 'names': [], 'alias': None, 'line': 1, 'level': 0}
- build_dependency_graph(root_file, max_depth=3)[source]ļ
Build a comprehensive dependency graph starting from a root file.
Creates a structured dependency graph that can be used for visualization, analysis, and understanding test code relationships.
- clear_cache()[source]ļ
Clear all caches including source, imports, dependencies, AST, execution trace, and coverage.
- combine_coverage_with_dependencies(frame)[source]ļ
Combine coverage data with dependency analysis.
Provides comprehensive analysis combining static dependencies, execution traces, and coverage data for complete context.
- extract_function_source_with_context(frame, context_lines=5, include_line_numbers=True, max_lines=None)[source]ļ
Extract function source with configurable context lines.
Enhanced version of extract_function_source that provides context lines around the failure point and additional metadata for better presentation.
- Parameters:
- Returns:
āsourceā: Source code with context (string)
ālinesā: Source code lines (list of strings)
āfailure_lineā: Line number where failure occurred
āstart_lineā: Starting line number of the extracted context
āend_lineā: Ending line number of the extracted context
āfilenameā: Path to the source file
āfunction_nameā: Name of the function containing the failure
ācontext_typeā: Type of context (āfunctionā, āclassā, āmoduleā)
āinclude_line_numbersā: Whether line numbers were included
āerrorā: Error message if extraction failed
- Return type:
Dictionary containing
Example
>>> extractor = CodeContextExtractor() >>> context = extractor.extract_function_source_with_context(frame, context_lines=3) >>> print(context['source']) def test_example(): x = 1 y = 2 assert x == y # <- Failure line return True
- get_executed_lines_for_function(frame)[source]ļ
Get coverage information for the specific function context.
Analyzes coverage data to determine which lines were executed in the context of the failing function.
- get_function_dependencies(frame)[source]ļ
Get dependencies specific to a functionās execution context.
Analyzes the imports and dependencies relevant to a specific functionās execution, providing contextual dependency information for failure analysis.
- integrate_coverage_data(coverage_data_file=None)[source]ļ
Integrate with coverage.py data when available.
Loads and processes coverage data to provide insights into which code paths were executed during test runs. This complements static analysis with runtime execution information.
- integrate_trace_with_dependencies(frame)[source]ļ
Integrate execution trace data with static dependency analysis.
Combines static analysis results with runtime execution information to provide comprehensive dependency context for failure analysis.
- start_execution_trace(file_filters=None, max_trace_entries=1000, mode='static', sample_rate=1)[source]ļ
Start optimized execution tracing for dependency analysis.
Enables execution tracing to track function calls and imports during test execution. This provides runtime dependency information to complement static analysis.
- Parameters:
file_filters (
Optional[List[str]]) ā List of file paths/patterns to trace (None for all files)max_trace_entries (
int) ā Maximum number of trace entries to keepmode (
str) ā āstaticā (no runtime, 0% overhead), āprofileā (functions), ātraceā (lines)sample_rate (
int) ā Sample every Nth call (1=no sampling, 10=sample 10%)
- Return type:
Note
Defaults to āstaticā mode for 0% overhead. Use āprofileā for <10% overhead with runtime dependency tracking.
- class failextract.FixtureExtractor[source]ļ
Bases:
objectExtract fixture definitions and their dependencies.
This class provides functionality to analyze test functions and extract information about pytest fixtures they depend on, including fixture definitions, dependencies, and scope information. It uses caching for performance optimization.
- _fixture_cacheļ
Internal cache for fixture definitions to improve performance on repeated lookups.
- __init__()[source]ļ
Initialize the FixtureExtractor with an empty cache.
Creates a new instance with an empty fixture cache. The cache will be populated as fixtures are discovered and analyzed.
Example
>>> extractor = FixtureExtractor() >>> fixtures = extractor.get_fixture_info(my_test_function) >>> print(len(fixtures)) 3
- get_fixture_info(test_func, frame_locals=None)[source]ļ
Extract all fixtures used by a test function.
Analyzes the test functionās signature to identify fixture dependencies, then recursively extracts information about each fixture including its definition, scope, dependencies, and source code.
- Parameters:
- Returns:
ānameā: Fixture name
ātypeā: Always āfixtureā
āscopeā: Fixture scope (āfunctionā, āclassā, āmoduleā, āsessionā)
āsourceā: Source code of the fixture function
ādependenciesā: List of other fixtures this fixture depends on
āfileā: Path to file containing the fixture definition
- Return type:
List of dictionaries containing fixture information with keys
Example
>>> def test_example(user_fixture, db_fixture): ... pass >>> extractor = FixtureExtractor() >>> fixtures = extractor.get_fixture_info(test_example) >>> print(fixtures[0]['name']) 'user_fixture' >>> print(fixtures[0]['scope']) 'function'
- class failextract.FailureExtractor[source]ļ
Bases:
objectEnhanced thread-safe singleton to collect test failures with flexible output.
This class implements a singleton pattern to provide centralized collection of test failure data across the application. It maintains thread-safe access to failure collections and supports configurable memory limits for large test suites.
- fixture_extractorļ
Instance for extracting fixture context
- Type:
Example
>>> extractor = FailureExtractor() >>> failure_data = { ... 'test_name': 'test_example', ... 'test_module': 'test_module', ... 'exception_type': 'AssertionError', ... 'exception_message': 'Test failed', ... 'timestamp': '2024-01-01T12:00:00' ... } >>> extractor.add_failure(failure_data) >>> print(len(extractor.failures)) 1
- add_failure(failure_data)[source]ļ
Add a test failure to the collection.
Stores test failure data in the internal collection with thread-safe access. Automatically enforces memory limits if configured.
- Parameters:
failure_data (
Dict[str,Any]) ā Dictionary containing failure information with keys: - ātest_nameā: Name of the failed test - ātest_moduleā: Module containing the test - ātest_fileā: File path of the test - āexception_typeā: Type of exception raised - āexception_messageā: Exception message - ātimestampā: When the failure occurred - Additional context data from fixture extraction
Example
>>> extractor = FailureExtractor() >>> failure_data = { ... 'test_name': 'test_example', ... 'test_module': 'test_module', ... 'exception_type': 'AssertionError', ... 'exception_message': 'Test failed', ... 'timestamp': '2024-01-01T12:00:00' ... } >>> extractor.add_failure(failure_data) >>> print(len(extractor.failures)) 1
- add_passed(test_data)[source]ļ
Add a passed test to the collection.
Stores passed test data in the internal collection with thread-safe access. Used when include_passed=True in configuration. Automatically enforces memory limits if configured.
- Parameters:
test_data (
Dict[str,Any]) ā Dictionary containing test information with keys: - ātest_nameā: Name of the passed test - ātest_moduleā: Module containing the test - ātest_fileā: File path of the test - ātimestampā: When the test passed - Additional context data from fixture extraction
Example
>>> extractor = FailureExtractor() >>> test_data = { ... 'test_name': 'test_successful', ... 'test_module': 'test_module', ... 'test_file': '/path/to/test.py', ... 'timestamp': '2024-01-01T12:00:00' ... } >>> extractor.add_passed(test_data) >>> print(len(extractor.passed)) 1
- class failextract.OutputConfig(output=None, format=None, append=False, include_passed=False, max_failures=None, include_source=True, include_fixtures=True, code_context_lines=5, max_code_lines=100, include_line_numbers=True, enhanced_context=True, include_traceback=True, include_metadata=True, timestamp_format='%Y-%m-%dT%H:%M:%S')[source]ļ
Bases:
objectConfiguration for output handling and report generation.
This class manages all configuration options for generating test failure reports, including output format, file paths, append behavior, content filtering options, and enhanced code inclusion settings.
- formatļ
Output format for the report
- Type:
- __init__(output=None, format=None, append=False, include_passed=False, max_failures=None, include_source=True, include_fixtures=True, code_context_lines=5, max_code_lines=100, include_line_numbers=True, enhanced_context=True, include_traceback=True, include_metadata=True, timestamp_format='%Y-%m-%dT%H:%M:%S')[source]ļ
Initialize output configuration with validation.
Creates a new output configuration with automatic format detection from file extensions and comprehensive parameter validation.
- Parameters:
output (
Union[str,Path,OutputFormat,None]) ā Output destination - file path (str/Path) or format enum. If None, outputs to stdout with JSON format.format (
Union[str,OutputFormat,None]) ā Explicit format specification, overrides format detection from file extension. Can be OutputFormat enum or string.append (
bool) ā If True, append to existing file instead of overwritinginclude_passed (
bool) ā If True, include passed tests in the reportmax_failures (
Optional[int]) ā Maximum number of failures to include in reportinclude_source (
bool) ā If True, include source code in failure reportsinclude_fixtures (
bool) ā If True, include fixture information in reportscode_context_lines (
int) ā Number of lines to include before/after failure pointsmax_code_lines (
int) ā Maximum total lines of code per failure (performance limit)include_line_numbers (
bool) ā If True, add line numbers to source code displayenhanced_context (
bool) ā If True, use enhanced CodeContextExtractor for better context
- Raises:
ValueError ā If output format cannot be determined or is invalid
TypeError ā If parameters have incorrect types
Example
>>> # Output to JSON file >>> config = OutputConfig("failures.json") >>> print(config.format) OutputFormat.JSON
>>> # Explicit format override >>> config = OutputConfig("report.txt", format="markdown") >>> print(config.format) OutputFormat.MARKDOWN
>>> # Append mode with filtering >>> config = OutputConfig( ... "results.json", ... append=True, ... max_failures=50 ... ) >>> print(config.append) True
- class failextract.OutputFormat(value)[source]ļ
Bases:
EnumSupported output formats.
- JSON = 'json'ļ
- MARKDOWN = 'md'ļ
- YAML = 'yaml'ļ
- XML = 'xml'ļ
- CSV = 'csv'ļ
- CUSTOM = 'custom'ļ
- class failextract.OutputFormatter[source]ļ
Bases:
ABCBase class for output formatters.
This abstract base class defines the interface that all output formatters must implement. It provides the foundation for creating custom formatters that can render test failure data in various formats.
Subclasses must implement the format() and get_extension() methods to provide format-specific rendering and file extension handling.
- abstractmethod get_extension()[source]ļ
Get default file extension for this format.
- Return type:
- Returns:
File extension string (e.g., ājsonā, āmdā, āxmlā)
- Raises:
NotImplementedError ā If not implemented by subclass
- class failextract.JSONFormatter[source]ļ
Bases:
OutputFormatterJSON output formatter for test failure reports.
This formatter produces structured JSON output suitable for programmatic processing, integration with tools, and serving as input for other processing systems. The JSON format is always available in FailExtract and does not require any optional dependencies.
The output includes full test failure information with proper JSON serialization handling for Python-specific types like datetime objects.
Example
>>> formatter = JSONFormatter() >>> failures = [{'test_name': 'test_example', 'timestamp': '2024-01-01'}] >>> json_output = formatter.format(failures) >>> print(json_output) [ { "test_name": "test_example", "timestamp": "2024-01-01" } ]
- format(failures)[source]ļ
Format failure data as JSON.
- Parameters:
failures (
List[Dict[str,Any]]) ā List of failure dictionaries containing test failure information- Return type:
- Returns:
Pretty-formatted JSON string with 2-space indentation
Note
Uses json.dumps with default=str to handle non-serializable types like datetime objects by converting them to strings.
- failextract.extract_on_failure(output=None, format=None, include_locals=False, include_fixtures=True, max_depth=10, skip_stdlib=True, extract_classes=True, append=False, custom_formatter=None, code_context_lines=5, enhanced_context=True, include_line_numbers=True, max_code_lines=100)[source]ļ
Enhanced decorator with flexible output options.
- Parameters:
output (
Union[str,Path,OutputFormat,OutputConfig,None]) ā Output configuration - can be: - str/Path: File path (format detected from extension) - OutputFormat: Format type (uses default filename) - OutputConfig: Full configuration object - None: Use global collectionformat (
Union[str,OutputFormat,None]) ā Override format detection (when output is a path)include_locals (
bool) ā Include local variablesinclude_fixtures (
bool) ā Extract fixture definitionsmax_depth (
int) ā Maximum traceback depthskip_stdlib (
bool) ā Skip standard library modulesextract_classes (
bool) ā Extract class definitionsappend (
bool) ā Append to existing file instead of overwritingcustom_formatter (
Optional[OutputFormatter]) ā Custom formatter instancecode_context_lines (
int) ā Number of context lines around failure pointsenhanced_context (
bool) ā Whether to use enhanced CodeContextExtractorinclude_line_numbers (
bool) ā Whether to include line numbers in source codemax_code_lines (
int) ā Maximum lines of code per failure frame
Examples
@extract_on_failure(āfailures.jsonā) @extract_on_failure(āreport.mdā) @extract_on_failure(output=āresults.xmlā, format=āxmlā) @extract_on_failure(OutputFormat.YAML) @extract_on_failure(output=OutputConfig(āreport.xmlā, append=True))
- failextract.extract_failure_info(test_func_or_exception, exception_or_frame=None, args=None, kwargs=None, frame_locals=None, include_locals=False, include_fixtures=True, max_depth=10, skip_stdlib=True, extract_classes=True, code_context_lines=5, enhanced_context=True, include_line_numbers=True, max_code_lines=100, config=None)[source]ļ
Extract comprehensive failure information including fixtures and enhanced code context.
Supports both new signature: extract_failure_info(test_func, exception, args, kwargs, ā¦) And legacy signature: extract_failure_info(exception, frame, config=ā¦)
- Parameters:
test_func_or_exception ā The test function that failed OR exception (for backward compatibility)
exception_or_frame ā The exception that was raised OR frame (for backward compatibility)
args (
tuple) ā Arguments passed to the test functionkwargs (
dict) ā Keyword arguments passed to the test functionframe_locals (
dict) ā Local variables from the test frameinclude_locals (
bool) ā Whether to include local variables in the outputinclude_fixtures (
bool) ā Whether to extract fixture informationmax_depth (
int) ā Maximum traceback depth to analyzeskip_stdlib (
bool) ā Whether to skip standard library framesextract_classes (
bool) ā Whether to extract class source for methodscode_context_lines (
int) ā Number of context lines around failure pointsenhanced_context (
bool) ā Whether to use enhanced CodeContextExtractorinclude_line_numbers (
bool) ā Whether to include line numbers in sourcemax_code_lines (
int) ā Maximum lines of code per frameconfig ā OutputConfig for backward compatibility
- Return type:
- Returns:
Dictionary containing comprehensive failure information with enhanced code context
- failextract.extract_function_source(frame)[source]ļ
Extract complete function source from a frame.
- failextract.save_failure_report(filename='test_failures_report.json', format='json')[source]ļ
Save all collected failures to a file.
- failextract.generate_session_report(output='test_failures_report.md', format=None, clear=True)[source]ļ
Generate a report for all collected failures.
- failextract.get_available_features()[source]ļ
Get information about available features based on installed extras.
- Return type:
- Returns:
Dictionary with feature availability information.
Core Classesļ
FailureExtractorļ
The main singleton class for managing failure data throughout a session.
- class failextract.FailureExtractor[source]ļ
Bases:
objectEnhanced thread-safe singleton to collect test failures with flexible output.
This class implements a singleton pattern to provide centralized collection of test failure data across the application. It maintains thread-safe access to failure collections and supports configurable memory limits for large test suites.
- fixture_extractorļ
Instance for extracting fixture context
- Type:
Example
>>> extractor = FailureExtractor() >>> failure_data = { ... 'test_name': 'test_example', ... 'test_module': 'test_module', ... 'exception_type': 'AssertionError', ... 'exception_message': 'Test failed', ... 'timestamp': '2024-01-01T12:00:00' ... } >>> extractor.add_failure(failure_data) >>> print(len(extractor.failures)) 1
- add_failure(failure_data)[source]ļ
Add a test failure to the collection.
Stores test failure data in the internal collection with thread-safe access. Automatically enforces memory limits if configured.
- Parameters:
failure_data (
Dict[str,Any]) ā Dictionary containing failure information with keys: - ātest_nameā: Name of the failed test - ātest_moduleā: Module containing the test - ātest_fileā: File path of the test - āexception_typeā: Type of exception raised - āexception_messageā: Exception message - ātimestampā: When the failure occurred - Additional context data from fixture extraction
Example
>>> extractor = FailureExtractor() >>> failure_data = { ... 'test_name': 'test_example', ... 'test_module': 'test_module', ... 'exception_type': 'AssertionError', ... 'exception_message': 'Test failed', ... 'timestamp': '2024-01-01T12:00:00' ... } >>> extractor.add_failure(failure_data) >>> print(len(extractor.failures)) 1
- add_passed(test_data)[source]ļ
Add a passed test to the collection.
Stores passed test data in the internal collection with thread-safe access. Used when include_passed=True in configuration. Automatically enforces memory limits if configured.
- Parameters:
test_data (
Dict[str,Any]) ā Dictionary containing test information with keys: - ātest_nameā: Name of the passed test - ātest_moduleā: Module containing the test - ātest_fileā: File path of the test - ātimestampā: When the test passed - Additional context data from fixture extraction
Example
>>> extractor = FailureExtractor() >>> test_data = { ... 'test_name': 'test_successful', ... 'test_module': 'test_module', ... 'test_file': '/path/to/test.py', ... 'timestamp': '2024-01-01T12:00:00' ... } >>> extractor.add_passed(test_data) >>> print(len(extractor.passed)) 1
FixtureExtractorļ
Specialized extractor for pytest fixture analysis.
- class failextract.FixtureExtractor[source]ļ
Bases:
objectExtract fixture definitions and their dependencies.
This class provides functionality to analyze test functions and extract information about pytest fixtures they depend on, including fixture definitions, dependencies, and scope information. It uses caching for performance optimization.
- _fixture_cacheļ
Internal cache for fixture definitions to improve performance on repeated lookups.
- __init__()[source]ļ
Initialize the FixtureExtractor with an empty cache.
Creates a new instance with an empty fixture cache. The cache will be populated as fixtures are discovered and analyzed.
Example
>>> extractor = FixtureExtractor() >>> fixtures = extractor.get_fixture_info(my_test_function) >>> print(len(fixtures)) 3
- get_fixture_info(test_func, frame_locals=None)[source]ļ
Extract all fixtures used by a test function.
Analyzes the test functionās signature to identify fixture dependencies, then recursively extracts information about each fixture including its definition, scope, dependencies, and source code.
- Parameters:
- Returns:
ānameā: Fixture name
ātypeā: Always āfixtureā
āscopeā: Fixture scope (āfunctionā, āclassā, āmoduleā, āsessionā)
āsourceā: Source code of the fixture function
ādependenciesā: List of other fixtures this fixture depends on
āfileā: Path to file containing the fixture definition
- Return type:
List of dictionaries containing fixture information with keys
Example
>>> def test_example(user_fixture, db_fixture): ... pass >>> extractor = FixtureExtractor() >>> fixtures = extractor.get_fixture_info(test_example) >>> print(fixtures[0]['name']) 'user_fixture' >>> print(fixtures[0]['scope']) 'function'
OutputConfigļ
Configuration class for report generation.
- class failextract.OutputConfig(output=None, format=None, append=False, include_passed=False, max_failures=None, include_source=True, include_fixtures=True, code_context_lines=5, max_code_lines=100, include_line_numbers=True, enhanced_context=True, include_traceback=True, include_metadata=True, timestamp_format='%Y-%m-%dT%H:%M:%S')[source]ļ
Bases:
objectConfiguration for output handling and report generation.
This class manages all configuration options for generating test failure reports, including output format, file paths, append behavior, content filtering options, and enhanced code inclusion settings.
- formatļ
Output format for the report
- Type:
- __init__(output=None, format=None, append=False, include_passed=False, max_failures=None, include_source=True, include_fixtures=True, code_context_lines=5, max_code_lines=100, include_line_numbers=True, enhanced_context=True, include_traceback=True, include_metadata=True, timestamp_format='%Y-%m-%dT%H:%M:%S')[source]ļ
Initialize output configuration with validation.
Creates a new output configuration with automatic format detection from file extensions and comprehensive parameter validation.
- Parameters:
output (
Union[str,Path,OutputFormat,None]) ā Output destination - file path (str/Path) or format enum. If None, outputs to stdout with JSON format.format (
Union[str,OutputFormat,None]) ā Explicit format specification, overrides format detection from file extension. Can be OutputFormat enum or string.append (
bool) ā If True, append to existing file instead of overwritinginclude_passed (
bool) ā If True, include passed tests in the reportmax_failures (
Optional[int]) ā Maximum number of failures to include in reportinclude_source (
bool) ā If True, include source code in failure reportsinclude_fixtures (
bool) ā If True, include fixture information in reportscode_context_lines (
int) ā Number of lines to include before/after failure pointsmax_code_lines (
int) ā Maximum total lines of code per failure (performance limit)include_line_numbers (
bool) ā If True, add line numbers to source code displayenhanced_context (
bool) ā If True, use enhanced CodeContextExtractor for better context
- Raises:
ValueError ā If output format cannot be determined or is invalid
TypeError ā If parameters have incorrect types
Example
>>> # Output to JSON file >>> config = OutputConfig("failures.json") >>> print(config.format) OutputFormat.JSON
>>> # Explicit format override >>> config = OutputConfig("report.txt", format="markdown") >>> print(config.format) OutputFormat.MARKDOWN
>>> # Append mode with filtering >>> config = OutputConfig( ... "results.json", ... append=True, ... max_failures=50 ... ) >>> print(config.append) True
Decorators and Functionsļ
extract_on_failureļ
Primary decorator for automatic failure capture.
- failextract.extract_on_failure(output=None, format=None, include_locals=False, include_fixtures=True, max_depth=10, skip_stdlib=True, extract_classes=True, append=False, custom_formatter=None, code_context_lines=5, enhanced_context=True, include_line_numbers=True, max_code_lines=100)[source]ļ
Enhanced decorator with flexible output options.
- Parameters:
output (
Union[str,Path,OutputFormat,OutputConfig,None]) ā Output configuration - can be: - str/Path: File path (format detected from extension) - OutputFormat: Format type (uses default filename) - OutputConfig: Full configuration object - None: Use global collectionformat (
Union[str,OutputFormat,None]) ā Override format detection (when output is a path)include_locals (
bool) ā Include local variablesinclude_fixtures (
bool) ā Extract fixture definitionsmax_depth (
int) ā Maximum traceback depthskip_stdlib (
bool) ā Skip standard library modulesextract_classes (
bool) ā Extract class definitionsappend (
bool) ā Append to existing file instead of overwritingcustom_formatter (
Optional[OutputFormatter]) ā Custom formatter instancecode_context_lines (
int) ā Number of context lines around failure pointsenhanced_context (
bool) ā Whether to use enhanced CodeContextExtractorinclude_line_numbers (
bool) ā Whether to include line numbers in source codemax_code_lines (
int) ā Maximum lines of code per failure frame
Examples
@extract_on_failure(āfailures.jsonā) @extract_on_failure(āreport.mdā) @extract_on_failure(output=āresults.xmlā, format=āxmlā) @extract_on_failure(OutputFormat.YAML) @extract_on_failure(output=OutputConfig(āreport.xmlā, append=True))
generate_session_reportļ
Utility function for session-level reporting.
Enums and Typesļ
OutputFormatļ
Enumeration of supported output formats.
Formatter Classesļ
Base Formatterļ
- class failextract.OutputFormatter[source]ļ
Bases:
ABCBase class for output formatters.
This abstract base class defines the interface that all output formatters must implement. It provides the foundation for creating custom formatters that can render test failure data in various formats.
Subclasses must implement the format() and get_extension() methods to provide format-specific rendering and file extension handling.
- abstractmethod get_extension()[source]ļ
Get default file extension for this format.
- Return type:
- Returns:
File extension string (e.g., ājsonā, āmdā, āxmlā)
- Raises:
NotImplementedError ā If not implemented by subclass
JSON Formatterļ
- class failextract.JSONFormatter[source]ļ
Bases:
OutputFormatterJSON output formatter for test failure reports.
This formatter produces structured JSON output suitable for programmatic processing, integration with tools, and serving as input for other processing systems. The JSON format is always available in FailExtract and does not require any optional dependencies.
The output includes full test failure information with proper JSON serialization handling for Python-specific types like datetime objects.
Example
>>> formatter = JSONFormatter() >>> failures = [{'test_name': 'test_example', 'timestamp': '2024-01-01'}] >>> json_output = formatter.format(failures) >>> print(json_output) [ { "test_name": "test_example", "timestamp": "2024-01-01" } ]
- format(failures)[source]ļ
Format failure data as JSON.
- Parameters:
failures (
List[Dict[str,Any]]) ā List of failure dictionaries containing test failure information- Return type:
- Returns:
Pretty-formatted JSON string with 2-space indentation
Note
Uses json.dumps with default=str to handle non-serializable types like datetime objects by converting them to strings.
Markdown Formatterļ
- class failextract.MarkdownFormatter[source]ļ
Bases:
OutputFormatterMarkdown output formatter for test failure reports.
This formatter produces well-structured Markdown output with proper headings, code blocks, and formatting. The output includes a table of contents for reports with multiple failures and uses GitHub-flavored Markdown syntax for optimal display.
Features: - Automatic table of contents for reports with 3+ failures - Syntax-highlighted code blocks for source code - Structured sections with clear headings and metadata - Anchor links for easy navigation - Compatible with GitHub, GitLab, and other Markdown renderers
Example
>>> formatter = MarkdownFormatter() >>> failures = [{'test_name': 'test_example', 'exception_type': 'AssertionError'}] >>> markdown_output = formatter.format(failures) >>> print(markdown_output[:30]) # Test Failure Report
Generated
- format(failures)[source]ļ
Format failure data as Markdown.
- Parameters:
failures (
List[Dict[str,Any]]) ā List of failure dictionaries containing test failure information- Return type:
- Returns:
Well-formatted Markdown string with headings, code blocks, and metadata
Note
Automatically generates a table of contents for reports with 3 or more failures. Uses GitHub-flavored Markdown syntax for code highlighting.
XML Formatterļ
- class failextract.XMLFormatter[source]ļ
Bases:
OutputFormatterXML output formatter for test failure reports.
This formatter produces well-formed XML output with proper escaping and CDATA sections for source code. The XML structure includes metadata about the report generation and detailed information for each test failure.
The XML schema includes: - Report metadata (generation time, failure count) - Individual failure elements with full test information - Proper XML escaping for special characters - CDATA sections for source code to preserve formatting
Example
>>> formatter = XMLFormatter() >>> failures = [{'test_name': 'test_example', 'exception_type': 'AssertionError'}] >>> xml_output = formatter.format(failures) >>> print(xml_output[:50]) <?xml version="1.0" encoding="UTF-8"?> <testFailure
- format(failures)[source]ļ
Format failure data as XML.
- Parameters:
failures (
List[Dict[str,Any]]) ā List of failure dictionaries containing test failure information- Return type:
- Returns:
Well-formed XML string with proper escaping and CDATA sections
Note
Uses XML escaping for text content and CDATA sections for source code to preserve formatting and special characters.
CSV Formatterļ
- class failextract.CSVFormatter[source]ļ
Bases:
OutputFormatterCSV output formatter for test failure reports.
This formatter produces comma-separated values output suitable for spreadsheet applications, data analysis tools, and tabular reporting. The CSV format includes essential test failure information in a structured tabular format.
The CSV includes these columns: - Test Name: Name of the failed test - Module: Python module containing the test - File: File path where the test is located - Timestamp: When the failure occurred - Exception Type: Type of exception raised - Exception Message: Error message from the exception - Line Number: Line number where failure occurred (if available)
Example
>>> formatter = CSVFormatter() >>> failures = [{'test_name': 'test_example', 'test_module': 'test_mod'}] >>> csv_output = formatter.format(failures) >>> print(csv_output.split('\n')[0]) Test Name,Module,File,Timestamp,Exception Type,Exception Message,Line Number
- format(failures)[source]ļ
Format failure data as CSV.
- Parameters:
failures (
List[Dict[str,Any]]) ā List of failure dictionaries containing test failure information- Return type:
- Returns:
CSV-formatted string with headers and failure data rows
Note
Uses Pythonās csv module for proper escaping and formatting. Extracts line numbers from extracted_code if available.
YAML Formatter (Optional)ļ
Available with pip install failextract[formatters]:
- class failextract.YAMLFormatter[source]ļ
Bases:
OutputFormatterYAML output formatter for test failure reports.
This formatter produces human-readable YAML output with proper structure and formatting. YAML is often preferred over JSON for configuration files and human-readable data exchange due to its clean syntax and comment support.
The formatter requires PyYAML as an optional dependency and will raise a helpful error message if the dependency is not installed.
Features: - Clean, human-readable YAML structure - Metadata section with generation time and failure count - Structured failure data with proper YAML formatting - Helpful error message if PyYAML is not installed
Example
>>> formatter = YAMLFormatter() >>> failures = [{'test_name': 'test_example', 'timestamp': '2024-01-01'}] >>> yaml_output = formatter.format(failures) >>> print(yaml_output[:50]) test_failure_report: metadata: generated: 2024-01-01
- format(failures)[source]ļ
Format failure data as YAML.
- Parameters:
failures (
List[Dict[str,Any]]) ā List of failure dictionaries containing test failure information- Return type:
- Returns:
Human-readable YAML string with structured failure data
- Raises:
ImportError ā If PyYAML is not installed with helpful installation message
Note
Structures data with metadata section and organized failure information. Uses PyYAMLās default_flow_style=False for readable multi-line output.
Configuration Classesļ
Configuration system is always available as part of core functionality:
- class failextract.ConfigurationManager[source]ļ
Bases:
objectMain configuration manager that handles loading and merging configurations.
- load_from_file(file_path)[source]ļ
Load configuration from a file.
- Parameters:
- Raises:
ConfigurationError ā If file cannot be parsed
- Return type:
- load_from_environment()[source]ļ
Load configuration from environment variables.
Environment variables should be prefixed with FAILEXTRACT_ and use underscore notation for nested sections: FAILEXTRACT_OUTPUT_DEFAULT_FORMAT
- Return type:
- get_effective_config()[source]ļ
Get the effective configuration after all merging.
- Return type:
- Returns:
Current effective configuration
- class failextract.ProjectConfig(config_data=None)[source]ļ
Bases:
objectMain project configuration class containing all sections.
- __init__(config_data=None)[source]ļ
Initialize project configuration.
- Parameters:
config_data (
Optional[Dict[str,Any]]) ā Dictionary containing configuration data- Raises:
ConfigurationError ā If configuration is invalid
CLI Moduleļ
Command-line interface is always available as part of core functionality:
Command-line interface for FailExtract.
This module provides a CLI for the FailExtract library, allowing users to: - Generate reports from existing failure data - Configure output formats and destinations - Validate and analyze test failure patterns - Export failure data in various formats - Check available features and installation status
The CLI is designed to work with both interactive usage and automation/CI/CD pipelines, providing structured output and exit codes for scripting.
The CLI automatically adapts based on installed optional features: - Core features (JSON output, basic commands) are always available - Advanced features require extras (e.g., pip install failextract[formatters]) - Helpful error messages guide users to install missing features
Example
Generate a JSON report (always available):
$ failextract report āformat json āoutput failures.json
Generate a markdown report:
$ failextract report āformat markdown āoutput failures.md
Check which features are available:
$ failextract features
Advanced failure analysis (requires analytics extra):
$ failextract analyze ātrends
Author: FailExtract Contributors License: Apache 2.0
- failextract.cli.create_parser()[source]ļ
Create and configure the argument parser.
- Returns:
Configured parser with all commands and options.
- Return type:
- failextract.cli.format_table_output(data, headers)[source]ļ
Format data as a simple ASCII table for terminal output.
Creates a formatted table with aligned columns suitable for display in terminals. Automatically calculates column widths based on content and provides clean separation between columns.
- Parameters:
- Return type:
- Returns:
Formatted ASCII table string with headers and aligned data rows
Example
>>> data = [{'name': 'test1', 'status': 'passed'}, {'name': 'test2', 'status': 'failed'}] >>> headers = ['name', 'status'] >>> table = format_table_output(data, headers) >>> print(table) name status ----- ------ test1 passed test2 failed
- failextract.cli.cmd_features(args)[source]ļ
Execute the features command to show available and missing features.
Displays information about which FailExtract features are currently available based on installed optional dependencies, and provides installation instructions for missing features.
- Parameters:
args (
Namespace) ā Parsed command line arguments (currently unused for this command)- Returns:
0 for success, 1 for error
- Return type:
Exit code
- Example Output:
Available Features: - json (always available) - xml (always available) - csv (always available) - markdown (always available)
Missing Features: - yaml (install with: pip install failextract[formatters]) - config (install with: pip install failextract[config])
Exception Classesļ
Base Exceptionsļ
Constants and Settingsļ
Version Informationļ
- failextract.__version__ = '1.0.0'ļ
str(object=āā) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to āstrictā.
Built-in Fixturesļ
- failextract.BUILTIN_FIXTURES = {'cache': 'Store and retrieve values across test runs', 'capfd': 'Capture file descriptors stdout/stderr', 'capfdbinary': 'Capture binary file descriptors', 'caplog': 'Capture log messages', 'capsys': 'Capture system stdout/stderr', 'capsysbinary': 'Capture binary system output', 'doctest_namespace': 'Namespace for doctest execution', 'monkeypatch': 'Modify objects, dict items, os.environ, etc.', 'pytestconfig': 'Access to configuration values and plugin manager', 'pytester': 'Plugin testing helper', 'recwarn': 'Record warnings', 'request': 'Pytest request object containing test context', 'tmp_path': 'Temporary directory unique to test invocation', 'tmp_path_factory': 'Factory for creating temporary directories', 'tmpdir': 'Temporary directory (legacy, use tmp_path)'}ļ
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping objectās
(key, value) pairs
- dict(iterable) -> new dictionary initialized as if via:
d = {} for k, v in iterable:
d[k] = v
- dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
Feature Detectionļ
Runtime Feature Detectionļ
Check what features are available in the current installation:
from failextract import get_available_features
features = get_available_features()
print(f"Available features: {features['available']}")
print(f"Missing features: {features['missing']}")
print(f"Core features: {features['core']}")
Installation Suggestionsļ
Get installation commands for missing features:
from failextract import suggest_installation
# Suggest installation for YAML support
suggestion = suggest_installation("yaml")
print(suggestion) # "pip install failextract[formatters]"
Utilities and Helpersļ
Context Analysisļ
- class failextract.CodeContextExtractor[source]ļ
Bases:
objectExtract and manage code context for test failures.
This class provides intelligent code context extraction with configurable depth and context lines. It implements caching for performance and thread-safe operation consistent with FailExtractās singleton patterns.
Features: - Configurable context lines before/after failure points - Intelligent source code caching with memory management - Thread-safe operation for concurrent test execution - Enhanced error handling for edge cases - Integration with existing source extraction functions
- _lockļ
Thread safety lock for cache operations
- Type:
- extract_function_source_with_context(frame, context_lines=5, include_line_numbers=True, max_lines=None)[source]ļ
Extract function source with configurable context lines.
Enhanced version of extract_function_source that provides context lines around the failure point and additional metadata for better presentation.
- Parameters:
- Returns:
āsourceā: Source code with context (string)
ālinesā: Source code lines (list of strings)
āfailure_lineā: Line number where failure occurred
āstart_lineā: Starting line number of the extracted context
āend_lineā: Ending line number of the extracted context
āfilenameā: Path to the source file
āfunction_nameā: Name of the function containing the failure
ācontext_typeā: Type of context (āfunctionā, āclassā, āmoduleā)
āinclude_line_numbersā: Whether line numbers were included
āerrorā: Error message if extraction failed
- Return type:
Dictionary containing
Example
>>> extractor = CodeContextExtractor() >>> context = extractor.extract_function_source_with_context(frame, context_lines=3) >>> print(context['source']) def test_example(): x = 1 y = 2 assert x == y # <- Failure line return True
- clear_cache()[source]ļ
Clear all caches including source, imports, dependencies, AST, execution trace, and coverage.
- analyze_imports(filename)[source]ļ
Analyze imports in a Python file using static analysis.
Extracts and categorizes all import statements from a Python file, providing detailed information about each import including line numbers, module names, aliases, and import types.
- Parameters:
filename (
str) ā Path to the Python file to analyze- Returns:
ātypeā: Import type (āimportā, āfrom_importā)
āmoduleā: Module name being imported
ānamesā: List of imported names (for from imports)
āaliasā: Alias used (as clause)
ālineā: Line number of the import statement
ālevelā: Relative import level (0 for absolute)
- Return type:
List of dictionaries containing import information with keys
Example
>>> extractor = CodeContextExtractor() >>> imports = extractor.analyze_imports("test_file.py") >>> print(imports[0]) {'type': 'import', 'module': 'os', 'names': [], 'alias': None, 'line': 1, 'level': 0}
- analyze_dependencies(filename, max_depth=3)[source]ļ
Analyze file dependencies and create dependency graph.
Performs recursive dependency analysis starting from a file to build a comprehensive dependency graph including direct imports, transitive dependencies, and circular dependency detection.
- Parameters:
- Returns:
ādirect_importsā: List of directly imported modules
ādependency_graphā: Nested dict of dependencies by depth level
ācircular_dependenciesā: List of detected circular dependencies
āunresolved_modulesā: List of modules that couldnāt be resolved
ātotal_dependenciesā: Count of all unique dependencies
- Return type:
Dictionary containing dependency analysis with keys
Example
>>> extractor = CodeContextExtractor() >>> deps = extractor.analyze_dependencies("test_file.py") >>> print(deps['total_dependencies']) 15
- get_function_dependencies(frame)[source]ļ
Get dependencies specific to a functionās execution context.
Analyzes the imports and dependencies relevant to a specific functionās execution, providing contextual dependency information for failure analysis.
- start_execution_trace(file_filters=None, max_trace_entries=1000, mode='static', sample_rate=1)[source]ļ
Start optimized execution tracing for dependency analysis.
Enables execution tracing to track function calls and imports during test execution. This provides runtime dependency information to complement static analysis.
- Parameters:
file_filters (
Optional[List[str]]) ā List of file paths/patterns to trace (None for all files)max_trace_entries (
int) ā Maximum number of trace entries to keepmode (
str) ā āstaticā (no runtime, 0% overhead), āprofileā (functions), ātraceā (lines)sample_rate (
int) ā Sample every Nth call (1=no sampling, 10=sample 10%)
- Return type:
Note
Defaults to āstaticā mode for 0% overhead. Use āprofileā for <10% overhead with runtime dependency tracking.
- integrate_trace_with_dependencies(frame)[source]ļ
Integrate execution trace data with static dependency analysis.
Combines static analysis results with runtime execution information to provide comprehensive dependency context for failure analysis.
- build_dependency_graph(root_file, max_depth=3)[source]ļ
Build a comprehensive dependency graph starting from a root file.
Creates a structured dependency graph that can be used for visualization, analysis, and understanding test code relationships.
- integrate_coverage_data(coverage_data_file=None)[source]ļ
Integrate with coverage.py data when available.
Loads and processes coverage data to provide insights into which code paths were executed during test runs. This complements static analysis with runtime execution information.
- get_executed_lines_for_function(frame)[source]ļ
Get coverage information for the specific function context.
Analyzes coverage data to determine which lines were executed in the context of the failing function.
Registry Managementļ
Examplesļ
Basic Usage Example:
from failextract import extract_on_failure, FailureExtractor, OutputConfig
@extract_on_failure
def test_example():
assert 1 == 2, "This will fail and be captured"
# Run test (it will fail and be captured)
try:
test_example()
except AssertionError:
pass
# Generate report
extractor = FailureExtractor()
config = OutputConfig("failures.json", format="json")
extractor.save_report(config)
Advanced Configuration Example:
from failextract import extract_on_failure
@extract_on_failure(
include_locals=True, # Capture local variables
include_fixtures=True, # Capture pytest fixtures
max_depth=15, # Variable inspection depth
skip_stdlib=False, # Include all stack frames
enhanced_context=True, # Enhanced context analysis
code_context_lines=10 # Lines of code context
)
def test_detailed():
user_data = {"id": 123, "name": "Alice"}
config = {"timeout": 30, "retries": 3}
assert False, "Detailed context will be captured"
Memory Management Example:
from failextract import FailureExtractor
extractor = FailureExtractor()
# Set memory limits
extractor.set_memory_limits(max_failures=500, max_passed=100)
# Check usage
stats = extractor.get_stats()
print(f"Usage: {stats['failures_count']} failures, {stats['passed_count']} passed")
CLI Usage Examples:
# Generate different format reports
failextract report --format json --output failures.json
failextract report --format markdown --output failures.md
failextract report --format csv --output failures.csv
failextract report --format yaml --output failures.yaml
failextract report --format xml --output failures.xml
# List captured failures
failextract list --format table
failextract list --format json
# Show statistics and analysis
failextract stats --format table
failextract stats --format json
# Check available features
failextract features --format table
# Clear all data
failextract clear --confirm
# Advanced analysis (if analytics extra installed)
# failextract analyze --trends --days 30
Error Handlingļ
Import Error Handling:
try:
from failextract import extract_on_failure
except ImportError as e:
print(f"FailExtract not available: {e}")
print("Install with: pip install failextract")
Feature Availability Checking:
from failextract import OutputConfig, FailureExtractor
def safe_yaml_report():
"""Generate YAML report if possible, JSON otherwise."""
extractor = FailureExtractor()
try:
config = OutputConfig("report.yaml", format="yaml")
extractor.save_report(config)
print("Generated YAML report")
except ImportError:
print("YAML not available, generating JSON instead")
config = OutputConfig("report.json", format="json")
extractor.save_report(config)
Configuration Error Handling:
from failextract import OutputConfig, ConfigurationError
try:
config = OutputConfig("invalid.txt", format="unsupported_format")
except ValueError as e: # OutputConfig raises ValueError for invalid parameters
print(f"Configuration error: {e}")
# Fall back to default
config = OutputConfig("backup.json")
Migration Notesļ
From Version 1.x to 2.x:
FailureExtractoris now a singletonOutputConfigconstructor has simplified parametersYAML formatter moved to optional
[formatters]extraCLI moved to optional
[cli]extra
Backward Compatibility:
All core APIs remain compatible
Optional features gracefully degrade if not installed
Configuration format remains unchanged
See Alsoļ
Tutorials - Step-by-step learning guides
How-To Guides - Task-focused solutions
Discussions - In-depth design discussions