CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-absl-py

Abseil Python Common Libraries providing application startup utilities, command-line flag management, enhanced logging, and comprehensive testing utilities.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

testing.mddocs/

Testing Utilities

Comprehensive testing framework extending Python's unittest with additional assertions, parameterized testing, temporary file management, flag state management, and enhanced test discovery and execution capabilities.

Capabilities

Enhanced Test Case

Extended test case class with additional assertions and utilities for absl-based applications.

class TestCase(unittest.TestCase):
    """
    Enhanced test case class extending unittest.TestCase.
    
    Provides additional assertions, temporary file management,
    and integration with absl flags and logging systems.
    """
    
    def setUp(self):
        """Set up test fixtures before each test method."""
    
    def tearDown(self):
        """Clean up after each test method."""
    
    def create_tempdir(self, name=None, cleanup=TempFileCleanup.ALWAYS):
        """
        Create a temporary directory for the test.
        
        Args:
            name (str): Optional name for the directory
            cleanup (TempFileCleanup): When to clean up the directory
            
        Returns:
            _TempDir: Temporary directory object
        """
    
    def create_tempfile(self, file_path=None, content=None, mode='w', encoding='utf-8', 
                       errors='strict', cleanup=TempFileCleanup.ALWAYS):
        """
        Create a temporary file for the test.
        
        Args:
            file_path (str): Optional path within temp directory
            content (str): Optional content to write to file
            mode (str): File open mode
            encoding (str): File encoding
            errors (str): Error handling mode
            cleanup (TempFileCleanup): When to clean up the file
            
        Returns:
            _TempFile: Temporary file object
        """
    
    # Additional assertion methods
    def assertEmpty(self, container, msg=None):
        """Assert that a container is empty."""
    
    def assertNotEmpty(self, container, msg=None):
        """Assert that a container is not empty."""
    
    def assertLen(self, container, expected_len, msg=None):
        """Assert that a container has the expected length."""
    
    def assertStartsWith(self, actual, expected_start, msg=None):
        """Assert that a string starts with expected prefix."""
    
    def assertEndsWith(self, actual, expected_end, msg=None):
        """Assert that a string ends with expected suffix."""
    
    def assertSequenceStartsWith(self, prefix, whole, msg=None):
        """Assert that a sequence starts with expected prefix sequence."""
    
    def assertContainsSubset(self, expected_subset, actual_container, msg=None):
        """Assert that actual_container contains all elements in expected_subset."""
    
    def assertNoCommonElements(self, expected_seq, actual_seq, msg=None):
        """Assert that two sequences have no common elements."""
    
    def assertItemsEqual(self, expected_seq, actual_seq, msg=None):
        """Assert that two sequences have the same elements (ignoring order)."""
    
    def assertCountEqual(self, expected_seq, actual_seq, msg=None):
        """Assert that two sequences have the same elements (ignoring order)."""
    
    def assertSameElements(self, expected_seq, actual_seq, msg=None):
        """Assert that two sequences have the same elements (ignoring order)."""
    
    def assertSetEqual(self, expected_set, actual_set, msg=None):
        """Assert that two sets are equal."""
    
    def assertListEqual(self, expected_list, actual_list, msg=None):
        """Assert that two lists are equal."""
    
    def assertTupleEqual(self, expected_tuple, actual_tuple, msg=None):
        """Assert that two tuples are equal."""
    
    def assertSequenceEqual(self, expected_seq, actual_seq, msg=None, seq_type=None):
        """Assert that two sequences are equal."""
    
    def assertDictEqual(self, expected_dict, actual_dict, msg=None):
        """Assert that two dictionaries are equal."""
    
    def assertDictContainsSubset(self, expected_subset, actual_dict, msg=None):
        """Assert that actual_dict contains all key-value pairs in expected_subset."""
    
    def assertUrlEqual(self, expected_url, actual_url, msg=None):
        """Assert that two URLs are equal (handling URL encoding differences)."""
    
    def assertSameStructure(self, expected, actual, abridge=True, msg=None):
        """Assert that two nested structures have the same shape."""
    
    def assertJsonEqual(self, expected, actual, msg=None):
        """Assert that two JSON-serializable objects are equal."""
    
    def assertBetween(self, value, lower_bound, upper_bound, msg=None):
        """Assert that a value is between lower_bound and upper_bound (inclusive)."""
    
    def assertLoggedLines(self, expected_logs, stream=None, min_level=None):
        """Assert that specific log lines were logged."""
    
    def assertRaisesWithLiteralMatch(self, expected_exception, expected_exception_message, callable_obj=None, *args, **kwargs):
        """Assert that an exception is raised with exact message match."""
    
    def assertRaisesWithPredicateMatch(self, exception_predicate, callable_obj=None, *args, **kwargs):
        """Assert that an exception is raised matching a predicate function."""

Test Runner

Main entry point for running absl-based tests with enhanced features.

def main(*args, **kwargs):
    """
    Main test runner function.
    
    Automatically discovers and runs tests with absl integration,
    flag parsing, and enhanced output formatting.
    
    Args:
        *args: Arguments passed to unittest.main
        **kwargs: Keyword arguments passed to unittest.main
    """

def run_tests(test_runner=None, argv=None, args=None, **kwargs):
    """
    Run tests with custom test runner and arguments.
    
    Args:
        test_runner: Custom test runner class
        argv: Command line arguments
        args: Additional arguments
        **kwargs: Additional keyword arguments
        
    Returns:
        unittest.TestResult: Test execution results
    """

class TestLoader(unittest.TestLoader):
    """
    Enhanced test loader with additional discovery capabilities.
    """
    
    def loadTestsFromModule(self, module, *args, **kwargs):
        """Load tests from a module with enhanced discovery."""

Temporary File Management

Utilities for managing temporary files and directories in tests.

class TempFileCleanup(enum.Enum):
    """Enumeration for temporary file cleanup behavior."""
    
    ALWAYS = 'always'      # Clean up after every test
    SUCCESS = 'success'    # Clean up only on test success  
    FAILURE = 'failure'    # Clean up only on test failure

class _TempDir:
    """Temporary directory manager for tests."""
    
    def __init__(self, path, cleanup):
        """Initialize temporary directory."""
    
    @property
    def full_path(self):
        """Get the full path to the temporary directory."""
    
    def create_file(self, file_path, content='', mode='w'):
        """Create a file within the temporary directory."""
    
    def mkdir(self, dir_path):
        """Create a subdirectory within the temporary directory."""

class _TempFile:
    """Temporary file manager for tests."""
    
    def __init__(self, path, cleanup):
        """Initialize temporary file."""
    
    @property  
    def full_path(self):
        """Get the full path to the temporary file."""
    
    def read_text(self, encoding='utf-8'):
        """Read the file contents as text."""
    
    def write_text(self, content, encoding='utf-8'):
        """Write text content to the file."""

Test Utilities

Additional utility functions for testing.

def expectedFailureIf(condition, reason):
    """
    Decorator to mark a test as expected failure under certain conditions.
    
    Args:
        condition (bool): If True, mark test as expected failure
        reason (str): Reason for expected failure
        
    Returns:
        Decorator function
    """

def skipThisClass(reason):
    """
    Decorator to skip an entire test class.
    
    Args:
        reason (str): Reason for skipping the class
        
    Returns:
        Decorator function
    """

def get_default_test_random_seed():
    """
    Get the default random seed for tests.
    
    Returns:
        int: Default random seed value
    """

def get_default_test_srcdir():
    """
    Get the default test source directory.
    
    Returns:
        str: Path to test source directory
    """

def get_default_test_tmpdir():
    """
    Get the default test temporary directory.
    
    Returns:
        str: Path to test temporary directory
    """

Parameterized Testing

Utilities for creating parameterized tests with multiple test cases from a single test method.

Parameterized Test Case

class parameterized.TestCase(absltest.TestCase):
    """
    Test case class with parameterized testing support.
    
    Inherits from absltest.TestCase and adds parameterized test generation.
    """

class parameterized.TestGeneratorMetaclass(type):
    """Metaclass for generating parameterized tests."""

Parameterization Decorators

def parameterized.parameters(*testcases):
    """
    Decorator to parameterize a test method.
    
    Args:
        *testcases: Test case parameters, each can be:
                   - Single value
                   - Tuple/list of values  
                   - Dictionary of keyword arguments
    
    Returns:
        Decorator function
    """

def parameterized.named_parameters(*testcases):
    """
    Decorator to parameterize a test method with named test cases.
    
    Args:
        *testcases: Named test cases, each should be a tuple/list where
                   the first element is the test name and remaining elements
                   are the test parameters
    
    Returns:
        Decorator function
    """

def parameterized.product(*kwargs_seqs, **testgrid):
    """
    Generate test parameters from the Cartesian product of parameter sets.
    
    Args:
        *kwargs_seqs: Sequences of keyword argument dictionaries
        **testgrid: Named parameter sequences for grid generation
        
    Returns:
        Generator of parameter combinations
    """

Parameterization Utilities

def parameterized.CoopTestCase(other_base_class):
    """
    Create a cooperative test case class that inherits from both
    parameterized.TestCase and another base class.
    
    Args:
        other_base_class (type): Other base class to inherit from
        
    Returns:
        type: New test case class
    """

Flag State Management

Utilities for saving and restoring flag states during testing.

Flag Saver Decorators

def flagsaver.flagsaver(func=None, **overrides):
    """
    Decorator to save and restore flag values around test execution.
    
    Can be used as @flagsaver.flagsaver or @flagsaver.flagsaver(flag_name=value)
    
    Args:
        func (callable): Function to wrap (when used without arguments)
        **overrides: Flag values to override during test execution
        
    Returns:
        Decorator function or wrapped function
    """

def flagsaver.as_parsed(*args, **kwargs):
    """
    Decorator to set flag values as if parsed from command line.
    
    Args:
        *args: Tuples of (flag_holder, value) or (flag_holder, [values])
        **kwargs: Flag name to value mappings
        
    Returns:
        Decorator function
    """

Flag State Management Functions

def flagsaver.save_flag_values(flag_values, **overrides):
    """
    Save current flag values and optionally override some values.
    
    Args:
        flag_values (FlagValues): Flag values container to save
        **overrides: Flag values to override
        
    Returns:
        dict: Saved flag state
    """

def flagsaver.restore_flag_values(saved_flag_values, flag_values):
    """
    Restore previously saved flag values.
    
    Args:
        saved_flag_values (dict): Previously saved flag state
        flag_values (FlagValues): Flag values container to restore
    """

Usage Examples

Basic Test Case

from absl.testing import absltest

class MyTest(absltest.TestCase):
    
    def test_basic_functionality(self):
        """Test basic functionality."""
        result = my_function('input')
        self.assertEqual(result, 'expected_output')
    
    def test_with_temp_files(self):
        """Test using temporary files."""
        temp_dir = self.create_tempdir()
        temp_file = temp_dir.create_file('test.txt', 'content')
        
        # Use temp_file.full_path for file operations
        result = process_file(temp_file.full_path)
        self.assertIsNotNone(result)

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

Parameterized Testing

from absl.testing import absltest
from absl.testing import parameterized

class ParameterizedTest(parameterized.TestCase):
    
    @parameterized.parameters(
        (1, 2, 3),
        (4, 5, 9),
        (10, 20, 30),
    )
    def test_addition(self, a, b, expected):
        """Test addition with multiple parameter sets."""
        result = add(a, b)
        self.assertEqual(result, expected)
    
    @parameterized.named_parameters(
        ('positive_numbers', 5, 3, 8),
        ('negative_numbers', -2, -3, -5),
        ('mixed_numbers', -1, 4, 3),
    )
    def test_named_addition(self, a, b, expected):
        """Test addition with named parameter sets."""
        result = add(a, b)
        self.assertEqual(result, expected)
    
    @parameterized.product(
        x=[1, 2, 3],
        y=[10, 20],
        operation=['add', 'multiply']
    )
    def test_operations_grid(self, x, y, operation):
        """Test operations using parameter grid."""
        if operation == 'add':
            result = x + y
        else:
            result = x * y
        self.assertGreater(result, 0)

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

Flag Testing

from absl import flags
from absl.testing import absltest
from absl.testing import flagsaver

FLAGS = flags.FLAGS
flags.DEFINE_string('test_flag', 'default', 'Test flag.')

class FlagTest(absltest.TestCase):
    
    @flagsaver.flagsaver
    def test_with_flag_override(self):
        """Test with flag value override."""
        FLAGS.test_flag = 'test_value'
        result = function_that_uses_flag()
        self.assertEqual(result, 'expected_with_test_value')
        # Flag is automatically restored after test
    
    @flagsaver.flagsaver(test_flag='override_value')
    def test_with_decorator_override(self):
        """Test with decorator-specified flag override."""
        result = function_that_uses_flag()
        self.assertEqual(result, 'expected_with_override_value')
    
    @flagsaver.as_parsed(('test_flag', 'parsed_value'))
    def test_with_parsed_flag(self):
        """Test with flag set as if parsed from command line."""
        result = function_that_uses_flag()
        self.assertEqual(result, 'expected_with_parsed_value')

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

Temporary File Testing

from absl.testing import absltest
import os

class FileTest(absltest.TestCase):
    
    def test_file_processing(self):
        """Test file processing with temporary files."""
        # Create temporary directory
        temp_dir = self.create_tempdir()
        
        # Create test files
        input_file = temp_dir.create_file('input.txt', 'test content')
        output_file_path = os.path.join(temp_dir.full_path, 'output.txt')
        
        # Test file processing
        process_file(input_file.full_path, output_file_path)
        
        # Verify output
        self.assertTrue(os.path.exists(output_file_path))
        with open(output_file_path, 'r') as f:
            content = f.read()
        self.assertIn('processed', content)

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

Test Discovery and Execution

# test_runner.py
from absl.testing import absltest

if __name__ == '__main__':
    # Automatically discover and run all tests
    absltest.main()

Command line usage:

# Run all tests
python test_runner.py

# Run specific test class
python test_runner.py MyTest

# Run specific test method  
python test_runner.py MyTest.test_method

# Run with verbose output
python test_runner.py --verbose

# Run with specific flags
python test_runner.py --test_flag=value

Install with Tessl CLI

npx tessl i tessl/pypi-absl-py

docs

app.md

flags.md

index.md

logging.md

testing.md

tile.json