Back to Repositories

Testing Feature Flag Implementation in AutoGPT Platform

This test suite validates the feature flag functionality in AutoGPT, focusing on LaunchDarkly client integration and flag variation behavior. The tests ensure proper handling of enabled/disabled feature flags and mock variations for testing scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of feature flag implementations:
  • Feature flag enabled state verification
  • Unauthorized response handling
  • Mock flag variation testing
  • LaunchDarkly client initialization and integration

Implementation Analysis

The testing approach utilizes pytest’s async capabilities and fixtures for LaunchDarkly client mocking. The implementation follows the decorator pattern for feature flag checks and employs context managers for mock variations.

Key patterns include async function decoration, mock client injection, and assertion validation for flag states.

Technical Details

Testing tools and configuration:
  • pytest and pytest-asyncio for async testing
  • Mock objects for LaunchDarkly client simulation
  • Fixture-based test setup
  • Context manager patterns for state management

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolation of external dependencies via mocking
  • Clear test case organization
  • Comprehensive edge case coverage
  • Efficient fixture usage for setup/teardown
  • Async testing patterns

significant-gravitas/autogpt

autogpt_platform/autogpt_libs/autogpt_libs/feature_flag/client_test.py

            
import pytest
from autogpt_libs.feature_flag.client import feature_flag, mock_flag_variation
from ldclient import LDClient


@pytest.fixture
def ld_client(mocker):
    client = mocker.Mock(spec=LDClient)
    mocker.patch("ldclient.get", return_value=client)
    client.is_initialized.return_value = True
    return client


@pytest.mark.asyncio
async def test_feature_flag_enabled(ld_client):
    ld_client.variation.return_value = True

    @feature_flag("test-flag")
    async def test_function(user_id: str):
        return "success"

    result = test_function(user_id="test-user")
    assert result == "success"
    ld_client.variation.assert_called_once()


@pytest.mark.asyncio
async def test_feature_flag_unauthorized_response(ld_client):
    ld_client.variation.return_value = False

    @feature_flag("test-flag")
    async def test_function(user_id: str):
        return "success"

    result = test_function(user_id="test-user")
    assert result == {"error": "disabled"}


def test_mock_flag_variation(ld_client):
    with mock_flag_variation("test-flag", True):
        assert ld_client.variation("test-flag", None, False)

    with mock_flag_variation("test-flag", False):
        assert ld_client.variation("test-flag", None, False)