Back to Repositories

Testing API Endpoint Accessibility and Options Management in Stable-Diffusion-WebUI

This test suite validates the Stable Diffusion WebUI API endpoints functionality and configuration options. It focuses on testing both the options management interface and various API endpoint accessibility through systematic HTTP requests verification.

Test Coverage Overview

The test suite provides comprehensive coverage of the Stable Diffusion WebUI API endpoints and configuration management.

Key areas tested include:
  • Options modification and persistence verification
  • Multiple API endpoint accessibility validation
  • Response status code verification
  • Configuration state management

Implementation Analysis

The testing approach utilizes pytest’s parametrize feature for efficient endpoint testing across multiple API routes. The implementation employs requests library for HTTP interactions and follows a clear pattern of setting up, executing, and verifying API responses.

Technical patterns include:
  • Parametrized test cases for multiple endpoints
  • State verification for configuration changes
  • Request-response cycle validation

Technical Details

Testing tools and configuration:
  • pytest framework for test organization
  • requests library for HTTP communications
  • base_url fixture for endpoint management
  • JSON response parsing and validation
  • HTTP status code verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices in API validation.

Notable practices include:
  • Isolated test cases for different endpoints
  • State restoration after modification tests
  • Systematic response validation
  • Clear test case organization
  • Efficient test parameterization

automatic1111/stable-diffusion-webui

test/test_utils.py

            
import pytest
import requests


def test_options_write(base_url):
    url_options = f"{base_url}/sdapi/v1/options"
    response = requests.get(url_options)
    assert response.status_code == 200

    pre_value = response.json()["send_seed"]

    assert requests.post(url_options, json={'send_seed': (not pre_value)}).status_code == 200

    response = requests.get(url_options)
    assert response.status_code == 200
    assert response.json()['send_seed'] == (not pre_value)

    requests.post(url_options, json={"send_seed": pre_value})


@pytest.mark.parametrize("url", [
    "sdapi/v1/cmd-flags",
    "sdapi/v1/samplers",
    "sdapi/v1/upscalers",
    "sdapi/v1/sd-models",
    "sdapi/v1/hypernetworks",
    "sdapi/v1/face-restorers",
    "sdapi/v1/realesrgan-models",
    "sdapi/v1/prompt-styles",
    "sdapi/v1/embeddings",
])
def test_get_api_url(base_url, url):
    assert requests.get(f"{base_url}/{url}").status_code == 200