Back to Repositories

Testing Spinner Component Context Manager Implementation in AutoGPT

This test suite validates the Spinner component functionality in AutoGPT, focusing on initialization, customization, and context manager behavior. The tests ensure proper loading animation display and control during application execution.

Test Coverage Overview

The test suite provides comprehensive coverage of the Spinner component’s core functionality.

Key areas tested include:
  • Default initialization parameters
  • Custom message and delay settings
  • Spinner state management (start/stop)
  • Context manager implementation
Edge cases cover proper cleanup and resource management when using the spinner as a context manager.

Implementation Analysis

The testing approach utilizes Python’s context manager protocol with the ‘with’ statement to ensure proper spinner lifecycle management. The implementation follows a clear pattern of setup, verification, and cleanup, leveraging Python’s built-in assert statements for validation.

Technical patterns include:
  • Context manager testing using with blocks
  • Time-based operation verification
  • State tracking through boolean flags

Technical Details

Testing tools and configuration:
  • Python’s built-in unittest framework
  • time module for delay simulation
  • Context manager protocol (@contextmanager)
  • Automated cleanup through context manager exit

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python unit testing.

Notable practices include:
  • Isolated test cases with clear purpose
  • Descriptive test method names
  • Proper resource cleanup
  • Effective use of context managers
  • Clear assertions and expectations

significant-gravitas/autogpt

classic/original_autogpt/tests/unit/test_spinner.py

            
import time

from autogpt.app.spinner import Spinner

ALMOST_DONE_MESSAGE = "Almost done..."
PLEASE_WAIT = "Please wait..."


def test_spinner_initializes_with_default_values():
    """Tests that the spinner initializes with default values."""
    with Spinner() as spinner:
        assert spinner.message == "Loading..."
        assert spinner.delay == 0.1


def test_spinner_initializes_with_custom_values():
    """Tests that the spinner initializes with custom message and delay values."""
    with Spinner(message=PLEASE_WAIT, delay=0.2) as spinner:
        assert spinner.message == PLEASE_WAIT
        assert spinner.delay == 0.2


#
def test_spinner_stops_spinning():
    """Tests that the spinner starts spinning and stops spinning without errors."""
    with Spinner() as spinner:
        time.sleep(1)
    assert not spinner.running


def test_spinner_can_be_used_as_context_manager():
    """Tests that the spinner can be used as a context manager."""
    with Spinner() as spinner:
        assert spinner.running
    assert not spinner.running