0
0
Selenium Pythontesting~5 mins

Data providers pattern in Selenium Python

Choose your learning style9 modes available
Introduction

Data providers help run the same test many times with different data. This saves time and finds more bugs.

When you want to test a login form with many usernames and passwords.
When you need to check a calculator app with different numbers.
When you want to verify a search feature with various keywords.
When you want to test form validation with multiple input sets.
When you want to run the same test on different browsers or devices.
Syntax
Selenium Python
import pytest

@pytest.mark.parametrize("input,expected", [
    ("data1", "result1"),
    ("data2", "result2"),
])
def test_example(input, expected):
    assert function_to_test(input) == expected

Use @pytest.mark.parametrize to supply data sets.

Each tuple in the list is one test case with inputs and expected results.

Examples
Test login with two sets of usernames and passwords.
Selenium Python
import pytest

@pytest.mark.parametrize("username,password", [
    ("user1", "pass1"),
    ("user2", "pass2"),
])
def test_login(username, password):
    assert login(username, password) == True
Test a square function with different numbers.
Selenium Python
import pytest

@pytest.mark.parametrize("number,expected", [
    (2, 4),
    (3, 9),
    (4, 16),
])
def test_square(number, expected):
    assert square(number) == expected
Sample Program

This test runs multiply() with 4 different sets of numbers. Each time it checks if the result matches the expected value.

Selenium Python
import pytest

# Function to test

def multiply(a, b):
    return a * b

# Test with data provider
@pytest.mark.parametrize("a,b,expected", [
    (2, 3, 6),
    (5, 5, 25),
    (0, 10, 0),
    (-1, 8, -8),
])
def test_multiply(a, b, expected):
    assert multiply(a, b) == expected
OutputSuccess
Important Notes

Use descriptive names for parameters to keep tests clear.

Data providers help avoid repeating similar test code.

Keep data sets small and focused for easy debugging.

Summary

Data providers run one test many times with different data.

Use @pytest.mark.parametrize in Python with pytest.

This pattern finds more bugs and saves time.