2022-08-16 20:45:15 +02:00
|
|
|
"""
|
2022-08-25 13:46:19 +02:00
|
|
|
Pytest feature: Parametrization
|
|
|
|
===============================
|
|
|
|
|
|
|
|
**Module:** ``tests.poc.test_parametrize_with_generator``
|
|
|
|
|
2022-08-16 20:45:15 +02:00
|
|
|
We are using a pytest feature heavily: Parametrization. These tests make sure this feature works still as expected.
|
2022-08-25 13:46:19 +02:00
|
|
|
|
|
|
|
Tests in this module run in a predefined order!
|
2022-08-16 20:45:15 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
from string import ascii_letters
|
|
|
|
from typing import Generator, List, Type
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from .conftest import CheapCounter
|
|
|
|
|
|
|
|
|
|
|
|
def data_generator() -> Generator[List[str], None, None]:
|
|
|
|
"""
|
2022-08-25 13:46:19 +02:00
|
|
|
Helper method: Create Test Data, but keep them as a generator
|
|
|
|
|
|
|
|
This helper is used by :meth:`~tests.poc.test_parametrize_with_generator.test_2_generator_parametrize`.
|
|
|
|
|
|
|
|
:returns: A bunch of test data as generator
|
2022-08-16 20:45:15 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
for val_a in ascii_letters[0:5]:
|
|
|
|
for val_b in ascii_letters[5:10]:
|
|
|
|
yield [val_a, val_b, f"{val_a}-{val_b}"]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.order(1)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
["val_a", "val_b", "val_c"],
|
|
|
|
[
|
|
|
|
["a", "b", "a:b"],
|
|
|
|
["c", "d", "c:d"],
|
|
|
|
],
|
|
|
|
)
|
2022-08-25 13:46:19 +02:00
|
|
|
def test_1_simple_parametrize(val_a: str, val_b: str, val_c: str, cheap_counter: Type[CheapCounter]) -> None:
|
2022-08-16 20:45:15 +02:00
|
|
|
"""
|
2022-08-25 13:46:19 +02:00
|
|
|
Test the simple parametrization from pytest.
|
|
|
|
|
|
|
|
:param val_a: Test value A
|
|
|
|
:param val_b: Test value B
|
|
|
|
:param val_c: Test value C
|
|
|
|
:param cheap_counter: Fixture :meth:`~tests.poc.conftest.cheap_counter`
|
2022-08-16 20:45:15 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
assert f"{val_a}:{val_b}" == val_c
|
|
|
|
cheap_counter.increment("simple")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.order(2)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
["val_a", "val_b", "val_c"],
|
|
|
|
data_generator(),
|
|
|
|
)
|
2022-08-25 13:46:19 +02:00
|
|
|
def test_2_generator_parametrize(val_a: str, val_b: str, val_c: str, cheap_counter: Type[CheapCounter]) -> None:
|
2022-08-16 20:45:15 +02:00
|
|
|
"""
|
2022-08-25 13:46:19 +02:00
|
|
|
Test the generator parametrization from pytest.
|
|
|
|
|
|
|
|
:param val_a: Test value A
|
|
|
|
:param val_b: Test value B
|
|
|
|
:param val_c: Test value C
|
|
|
|
:param cheap_counter: Fixture :meth:`~tests.poc.conftest.cheap_counter`
|
2022-08-16 20:45:15 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
assert f"{val_a}-{val_b}" == val_c
|
|
|
|
cheap_counter.increment("generator")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.order(3)
|
2022-08-25 13:46:19 +02:00
|
|
|
def test_3_evaluation(cheap_counter: Type[CheapCounter]) -> None:
|
2022-08-16 20:45:15 +02:00
|
|
|
"""
|
2022-08-25 13:46:19 +02:00
|
|
|
Evaluate the values of the :meth:`~tests.poc.conftest.cheap_counter` fixture.
|
|
|
|
|
|
|
|
:param cheap_counter: Fixture :meth:`~tests.poc.conftest.cheap_counter`
|
2022-08-16 20:45:15 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
assert cheap_counter.get_value("simple") == 2
|
|
|
|
assert cheap_counter.get_value("generator") == 25
|