Back to Repositories

Testing Reload Configuration Management in Gradio-App

This test suite validates the reload functionality in Gradio applications, focusing on configuration setup and directory watching capabilities. It ensures proper handling of demo applications and their reload behavior.

Test Coverage Overview

The test suite covers configuration management and directory watching functionality for Gradio applications.

Key areas tested include:
  • Default application configuration setup
  • Custom demo name handling
  • Gradio directory watching
  • Application directory monitoring

Implementation Analysis

The implementation uses pytest fixtures and parametrization to test various configuration scenarios. The testing approach leverages mock objects and fixtures to isolate the reload functionality and verify directory watching behavior.

Key patterns include:
  • Fixture-based test configuration
  • Monkeypatch usage for system arguments
  • Parametrized test cases
  • Clean teardown with fixture cleanup

Technical Details

Testing tools and configuration:
  • pytest framework for test organization
  • dataclasses for configuration management
  • Path from pathlib for directory handling
  • Custom fixtures for reloader and configuration setup
  • Monkeypatch for system argument modification

Best Practices Demonstrated

The test suite demonstrates several testing best practices including proper test isolation, fixture usage, and clean setup/teardown patterns.

Notable practices:
  • Modular test case organization
  • Effective use of pytest fixtures
  • Clear test naming conventions
  • Comprehensive configuration testing
  • Proper resource cleanup

gradio-app/gradio

test/test_reload.py

            
import dataclasses
from pathlib import Path

import pytest

import gradio
import gradio as gr
from gradio.cli.commands.reload import _setup_config
from gradio.http_server import Server


def build_demo():
    with gr.Blocks() as demo:
        gr.Textbox("")

    return demo


@dataclasses.dataclass
class Config:
    module_name: str
    path: Path
    watch_dirs: list[str]
    demo_name: str


class TestReload:
    @pytest.fixture(autouse=True)
    def argv(self):
        return ["demo/calculator/run.py"]

    @pytest.fixture
    def config(self, monkeypatch, argv) -> Config:
        monkeypatch.setattr("sys.argv", ["gradio"] + argv)
        name = argv[1].replace("--demo-name", "").strip() if len(argv) > 1 else "demo"
        return Config(*_setup_config(argv[0], name))

    @pytest.fixture(params=[{}])
    def reloader(self, config):
        reloader = Server(config)
        reloader.should_exit = True
        yield reloader
        reloader.close()

    def test_config_default_app(self, config):
        assert config.module_name == "demo.calculator.run"

    @pytest.mark.parametrize("argv", [["demo/calculator/run.py", "--demo-name test"]])
    def test_config_custom_app(self, config):
        assert config.module_name == "demo.calculator.run"
        assert config.demo_name == "test"

    def test_config_watch_gradio(self, config):
        gradio_dir = str(Path(gradio.__file__).parent)
        assert gradio_dir in config.watch_dirs

    def test_config_watch_app(self, config):
        demo_dir = str(Path("demo/calculator/run.py").resolve().parent)
        assert demo_dir in config.watch_dirs