0
0
PyTesttesting~3 mins

Why Single parameter in PyTest? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could test many inputs with just one simple test function?

The Scenario

Imagine you have a function that needs to be tested with many different input values. You write a separate test for each input by hand, repeating almost the same code again and again.

The Problem

This manual way is slow and boring. You might forget to test some inputs or make mistakes copying code. It's hard to keep tests organized and update them when the function changes.

The Solution

Using a single parameter with pytest's @pytest.mark.parametrize lets you write one test that runs many times with different inputs automatically. This saves time, reduces errors, and keeps tests clean.

Before vs After
Before
def test_add():
    assert add(1) == 2
    assert add(2) == 3
    assert add(3) == 4
After
import pytest

@pytest.mark.parametrize('input', [1, 2, 3])
def test_add(input):
    assert add(input) == input + 1
What It Enables

You can easily test many input cases with one simple test, making your testing faster and more reliable.

Real Life Example

Testing a calculator app's add function with many numbers without writing repetitive tests for each number.

Key Takeaways

Manual repetitive tests are slow and error-prone.

Single parameter testing runs one test many times with different inputs.

This makes tests easier to write, read, and maintain.