Is there a way to pass input to pytest.mark.parametrize()?
If I try…
import pytest
from typing import List
@pytest.mark.parametrize("rotation", range(len(input_sequence)))
def test_sequence_rotation(
input_sequence: List[float],
rotation: int,
) -> None:
sequence = input_sequence[rotation:] + input_sequence[:rotation]
print(f"Testing sequence {sequence}")
… I get NameError: name 'input_sequence' is not defined
.
For some context, I have input_sequence
defined as a pytest command option in conftest.py:
import pytest
from typing import List
from _pytest.config.argparsing import Parser
from _pytest.fixtures import SubRequest
def pytest_addoption(parser: Parser) -> None:
parser.addoption("--sequence-csv", type=str, required=True)
@pytest.fixture()
def input_sequence(request: SubRequest) -> List[float]:
csv = request.config.getoption("--sequence-csv")
return [float(i) for i in csv.split(",")]
You're referring to a variable
input_sequence
in your call to@pytest.mark.parametrize
, but no such variable is in scope. You would need to define this at the module level (either directly or by importing it from somewhere).You would also need to remove it from the parameter list of
test_sequence_rotation
.E.g., something like:
The above code will produce output like this when run via
pytest -v
: