Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Testparametrisierung | Pytest Framework
Python Fortgeschrittene Konzepte
course content

Kursinhalt

Python Fortgeschrittene Konzepte

Python Fortgeschrittene Konzepte

1. Module und Importe
2. Fehlerbehandlung
3. Dateiverwaltung
4. Pytest Framework
5. Unittest-Framework
6. Iteratoren und Generatoren

book
Testparametrisierung

@pytest.mark.parametrize

# Function to test
def calculate_average(num1, num2):
    return (num1 + num2) / 2

# Test function using pytest's parametrize
import pytest

@pytest.mark.parametrize(
    "num1, num2, expected",
    [
        (10, 20, 15),
        (20, 30, 25),
        (5, 5, 5)
    ])
def test_calculate_average(num1, num2, expected):
    assert (calculate_average(num1, num2) == expected), f"Sum of {num1} and {num2} should be equal to {expected}"

id

 @pytest.mark.parametrize("num1, num2, expected", [
    pytest.param(10, 20, 15, id="integers"),
    pytest.param(20, 30, 25, id="more integers"),
    pytest.param(5, 5, 5, id="equal numbers")
])
def test_calculate_average_with_ids(num1, num2, expected):
    assert calculate_average(num1, num2) == expected

pytest.param ids

@pytest.mark.parametrize("num1, num2, expected", [
    (10, 20, 15),
    (20, 30, 25),
    (5, 5, 5)
], ids=["integers", "more integers", "equal numbers"])
def test_calculate_average_with_ids(num1, num2, expected):
    assert calculate_average(num1, num2) == expected
===================== test session starts ======================
collected 3 items

test_example.py::test_calculate_average_with_ids[integers] FAILED
test_example.py::test_calculate_average_with_ids[more integers] PASSED
test_example.py::test_calculate_average_with_ids[equal numbers] PASSED

=========================== FAILURES ===========================
________________ test_calculate_average_with_ids[integers] ________________

num1 = 10, num2 = 20, expected = 15

    def test_calculate_average_with_ids(num1, num2, expected):
>       assert calculate_average(num1, num2) == expected
E       assert 20 == 15 
E        +  where 20 = calculate_average(10, 20)

test_example.py:10: AssertionError
================== 1 failed, 2 passed in 0.23s ==================

1. Consider a function that multiplies two numbers. Complete the test case by filling in the missing parts:

2. Was ist der Hauptvorteil der Angabe von Bezeichnern mit id oder ids in @pytest.mark.parametrize?

question mark

Consider a function that multiplies two numbers. Complete the test case by filling in the missing parts:

Select the correct answer

question mark

Was ist der Hauptvorteil der Angabe von Bezeichnern mit id oder ids in @pytest.mark.parametrize?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 4

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

course content

Kursinhalt

Python Fortgeschrittene Konzepte

Python Fortgeschrittene Konzepte

1. Module und Importe
2. Fehlerbehandlung
3. Dateiverwaltung
4. Pytest Framework
5. Unittest-Framework
6. Iteratoren und Generatoren

book
Testparametrisierung

@pytest.mark.parametrize

# Function to test
def calculate_average(num1, num2):
    return (num1 + num2) / 2

# Test function using pytest's parametrize
import pytest

@pytest.mark.parametrize(
    "num1, num2, expected",
    [
        (10, 20, 15),
        (20, 30, 25),
        (5, 5, 5)
    ])
def test_calculate_average(num1, num2, expected):
    assert (calculate_average(num1, num2) == expected), f"Sum of {num1} and {num2} should be equal to {expected}"

id

 @pytest.mark.parametrize("num1, num2, expected", [
    pytest.param(10, 20, 15, id="integers"),
    pytest.param(20, 30, 25, id="more integers"),
    pytest.param(5, 5, 5, id="equal numbers")
])
def test_calculate_average_with_ids(num1, num2, expected):
    assert calculate_average(num1, num2) == expected

pytest.param ids

@pytest.mark.parametrize("num1, num2, expected", [
    (10, 20, 15),
    (20, 30, 25),
    (5, 5, 5)
], ids=["integers", "more integers", "equal numbers"])
def test_calculate_average_with_ids(num1, num2, expected):
    assert calculate_average(num1, num2) == expected
===================== test session starts ======================
collected 3 items

test_example.py::test_calculate_average_with_ids[integers] FAILED
test_example.py::test_calculate_average_with_ids[more integers] PASSED
test_example.py::test_calculate_average_with_ids[equal numbers] PASSED

=========================== FAILURES ===========================
________________ test_calculate_average_with_ids[integers] ________________

num1 = 10, num2 = 20, expected = 15

    def test_calculate_average_with_ids(num1, num2, expected):
>       assert calculate_average(num1, num2) == expected
E       assert 20 == 15 
E        +  where 20 = calculate_average(10, 20)

test_example.py:10: AssertionError
================== 1 failed, 2 passed in 0.23s ==================

1. Consider a function that multiplies two numbers. Complete the test case by filling in the missing parts:

2. Was ist der Hauptvorteil der Angabe von Bezeichnern mit id oder ids in @pytest.mark.parametrize?

question mark

Consider a function that multiplies two numbers. Complete the test case by filling in the missing parts:

Select the correct answer

question mark

Was ist der Hauptvorteil der Angabe von Bezeichnern mit id oder ids in @pytest.mark.parametrize?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 4
some-alt