Back to Repositories

Testing Number Component Implementation in Gradio

This test suite validates the Number component functionality in the Gradio framework, focusing on numeric input handling, precision control, and interface integration. The tests cover core component methods and various numeric processing scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of the Number component’s core functionality:
  • Basic numeric input processing with floating-point and integer values
  • Precision control and rounding behavior
  • Component configuration and property handling
  • Integration testing with Gradio Interface
  • Edge cases including null values and type conversions

Implementation Analysis

The testing approach implements systematic validation of the Number component’s key methods:
  • preprocess() and postprocess() method validation
  • Configuration management through get_config()
  • Precision handling with different numeric types
  • Interface integration testing for both input and output scenarios

Technical Details

Testing implementation utilizes:
  • Python’s pytest framework for test organization
  • Gradio’s gr.Number component with various configurations
  • Interface testing with lambda functions
  • Assertion-based validation for numeric processing
  • Type checking for precision control

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test cases for specific functionality
  • Comprehensive edge case coverage
  • Clear test method naming and documentation
  • Systematic validation of component properties
  • Integration testing with the broader framework

gradio-app/gradio

test/components/test_number.py

            
import gradio as gr


class TestNumber:
    def test_component_functions(self):
        """
        Preprocess, postprocess, serialize, get_config

        """
        numeric_input = gr.Number(elem_id="num", elem_classes="first")
        assert numeric_input.preprocess(3) == 3.0
        assert numeric_input.preprocess(None) is None
        assert numeric_input.postprocess(3) == 3
        assert numeric_input.postprocess(3) == 3.0
        assert numeric_input.postprocess(2.14) == 2.14
        assert numeric_input.postprocess(None) is None
        assert numeric_input.get_config() == {
            "value": None,
            "name": "number",
            "show_label": True,
            "step": 1,
            "label": None,
            "minimum": None,
            "maximum": None,
            "container": True,
            "min_width": 160,
            "scale": None,
            "elem_id": "num",
            "elem_classes": ["first"],
            "visible": True,
            "interactive": None,
            "proxy_url": None,
            "info": None,
            "precision": None,
            "_selectable": False,
            "key": None,
        }

    def test_component_functions_integer(self):
        """
        Preprocess, postprocess, serialize, get_template_context

        """
        numeric_input = gr.Number(precision=0, value=42)
        assert numeric_input.preprocess(3) == 3
        assert numeric_input.preprocess(None) is None
        assert numeric_input.postprocess(3) == 3
        assert numeric_input.postprocess(3) == 3
        assert numeric_input.postprocess(2.85) == 3
        assert numeric_input.postprocess(None) is None
        assert numeric_input.get_config() == {
            "value": 42,
            "name": "number",
            "show_label": True,
            "step": 1,
            "label": None,
            "minimum": None,
            "maximum": None,
            "container": True,
            "min_width": 160,
            "scale": None,
            "elem_id": None,
            "elem_classes": [],
            "visible": True,
            "interactive": None,
            "proxy_url": None,
            "info": None,
            "precision": 0,
            "_selectable": False,
            "key": None,
        }

    def test_component_functions_precision(self):
        """
        Preprocess, postprocess, serialize, get_template_context

        """
        numeric_input = gr.Number(precision=2, value=42.3428)
        assert numeric_input.preprocess(3.231241) == 3.23
        assert numeric_input.preprocess(None) is None
        assert numeric_input.postprocess(-42.1241) == -42.12
        assert numeric_input.postprocess(5.6784) == 5.68
        assert numeric_input.postprocess(2.1421) == 2.14
        assert numeric_input.postprocess(None) is None

    def test_precision_none_with_integer(self):
        """
        Preprocess, postprocess
        """
        numeric_input = gr.Number(precision=None)
        assert numeric_input.preprocess(5) == 5
        assert isinstance(numeric_input.preprocess(5), int)
        assert numeric_input.postprocess(5) == 5
        assert isinstance(numeric_input.postprocess(5), int)

    def test_precision_none_with_float(self):
        """
        Preprocess, postprocess
        """
        numeric_input = gr.Number(value=5.5, precision=None)
        assert numeric_input.preprocess(5.5) == 5.5
        assert isinstance(numeric_input.preprocess(5.5), float)
        assert numeric_input.postprocess(5.5) == 5.5
        assert isinstance(numeric_input.postprocess(5.5), float)

    def test_in_interface_as_input(self):
        """
        Interface, process
        """
        iface = gr.Interface(lambda x: x**2, "number", "textbox")
        assert iface(2) == "4"

    def test_precision_0_in_interface(self):
        """
        Interface, process
        """
        iface = gr.Interface(lambda x: x**2, gr.Number(precision=0), "textbox")
        assert iface(2) == "4"

    def test_in_interface_as_output(self):
        """
        Interface, process
        """
        iface = gr.Interface(lambda x: int(x) ** 2, "textbox", "number")
        assert iface(2) == 4.0

    def test_static(self):
        """
        postprocess
        """
        component = gr.Number()
        assert component.get_config().get("value") is None
        component = gr.Number(3)
        assert component.get_config().get("value") == 3.0