ApprovalTests.Python

repository·main·Indexed 42 days ago

https://github.com/approvals/approvaltests.python

An assertion and verification library for Python that handles complex test outputs by comparing them against a 'golden master' using visual diff tools. It integrates with pytest and unittest, providing features like custom reporters, regex scrubbers, and Storyboards to manage long strings, large arrays, and complex objects.

Tokens
21.4K
Snippets
71
Records
106
Agent score
52%

What's inside ApprovalTests.Python

  1. Explore ApprovalTests.Python documentation

    main

    The ApprovalTests.Python documentation is organized into several sections to help you master approval testing:

    • How to: Practical guides for specific tasks like selecting reporters, creating custom verify() methods, testing binary data, scrubbing dates, and handling multiple approvals per test.
    • Reference: Technical details on configuration options, Storyboard, MarkdownTables, and the SimpleLogger.
    • Explanations: Conceptual deep-dives into what approvals are, the benefits of different reporters, and the theory behind approval testing.
    • Tutorial: A guided introduction to understanding and using reporters.
  2. Standard development scripts and tasks

    main

    The repository uses a standardized set of development scripts to provide a consistent interface for common tasks across different platforms. These scripts are designed to be extremely short, delegating the actual work to Mise tasks.

    Commonly available scripts include:

    • build_and_test: Runs the full build pipeline, including tests and linting. This is the primary script used by CI.
    • tidy_code: Formats code and applies automatic fixes (e.g., removing unused imports).
    • update_docs: Updates markdown documentation with code snippets.
    • run: Executes the project (for non-library projects), building as necessary.
  3. How Approvals work

    main

    Approvals verify test results by comparing them against a "golden master" (an approved snapshot).

    1. First Run: If no golden master exists, ApprovalTests creates a snapshot of the current results and uses it as the golden master.
    2. Subsequent Runs: The library compares the new results against the golden master.
    3. Failure: If the results differ, a Reporter launches an external diff utility to show the differences.
    4. Resolution: You can either update the golden master (if the change was expected and correct) or revert your code changes to match the existing master.
  4. How to use Storyboards to track object progression

    main

    Storyboards allow you to track an object through time by capturing multiple states (frames) in a single approval test. This is useful for visualizing workflows or the lifetime of an object. The output is formatted vertically (like a comic book) so that diff tools can clearly show the progression between frames.

    To use a Storyboard manually:

    1. Instantiate Storyboard().
    2. Use add_description(str) to provide context for the sequence.
    3. Use add_frame(content) to capture the state of your object at a specific point in time.
    4. Use verify(story) to perform the approval test.
    story = Storyboard()
    story.add_description("Spinning wheel")
    story.add_frame(ascii_wheel)
    # ... perform action ...
    story.add_frame(ascii_wheel)
    verify(story)
  5. Generate Group Aggregator classes

    main

    Group aggregators allow users to request a tool by its family name (e.g., ReportWithBeyondCompare) regardless of their operating system or specific version.

    Implementation Steps:

    1. Identify rows sharing the same non-empty group_name.
    2. Convert group_name from SCREAMING_SNAKE_CASE to PascalCase.
    3. Create a FirstWorkingReporter subclass named ReportWith{GroupPascalCase}.
    4. Include all reporters in that group in the order they appear in the CSV.
    5. Note: No OS-specific guard is required for group reporters, as individual reporter classes will fail gracefully if their executable is not found on the current system.

    Example: ReportWithBeyondCompare might try ReportWithBeyondCompareMac, ReportWithBeyondCompare3Windows, and ReportWithBeyondCompare4Windows in sequence.

  6. Understand the behavior of FirstWorkingReporter.toString()

    main

    The toString() method of FirstWorkingReporter provides a human-readable representation of the reporter instance. This is useful for debugging, logging, and equality comparisons. The implementation works by recursively calling the toString() method of each individual reporter contained within the FirstWorkingReporter instance to build a composite string representation.

    "FirstWorkingReporter(ReporterForTesting(True), ReporterForTesting(False))"
  7. How the AI-Powered Loop Fixer works

    main

    The AI-Powered Loop Fixer is an automation script designed to maintain code quality by continuously identifying, fixing, and verifying issues.

    The Workflow:

    1. Baseline Check: Runs an initial test to ensure the current state is stable. If the baseline fails, the process aborts.
    2. Problem Detection: Scans the codebase for issues using a find_problems script.
    3. AI Fix Generation: When a problem is found, it invokes an AI model via a fix_problem script to generate a fix.
    4. Verification (TCR): The fix is passed to a tcr (Test && Commit || Revert) script.
      • If tests pass: The changes are committed.
      • If tests fail: The changes are reverted.
    5. Iteration: The loop repeats until no more problems are detected or the maximum iteration limit (1000 runs) is reached.
    flowchart TD
        subgraph Initialization
            Start([START]) --> InitialTest{tcr}
        end
    
        InitialTest -->|FAIL| Exit([ABORT])
        InitialTest -->|PASS| FindProblems["find_problems"]
        
        subgraph Main Loop
            FindProblems --> HasProblems{has_problems?}
            HasProblems -->|NO| Done([ALL DONE])
            HasProblems -->|YES| FixProblem[fix_problem]
            
            FixProblem --> Tcr[tcr]
            Tcr --> FindProblems
        end
        
        %% Styling
        style Done fill:#2e7d32,stroke:#1b5e20,stroke-width:2px,color:#fff
        style Exit fill:#c62828,stroke:#b71c1c,stroke-width:2px,color:#fff