How to decorate a test and pass a parameter? #12569
Answered
by
RonnyPfannschmidt
Tomperez98
asked this question in
Q&A
-
# Step 1: Create a custom decorator
def generate_number(num_cases=10, min_value=1, max_value=100):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs) -> None:
for _ in range(num_cases):
random_number = random.randint(min_value, max_value)
func(random_number, *args, **kwargs)
return wrapper
return decorator
# Step 2: Decorate your test function with the custom decorator
@pytest.mark.dev()
@generate_number(num_cases=10)
def test_random(random_number: int) -> None:
# Your test logic here
assert random_number >= 1 # Example assertion This fails due to: E fixture 'random_number' not found
> available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them. How can I make this work? I need to decorate tests, and pass one parameter without triggering search for a fixture |
Beta Was this translation helpful? Give feedback.
Answered by
RonnyPfannschmidt
Jul 4, 2024
Replies: 1 comment
-
The recommended version is to use pytest- native parameterization If you roll your own, youd need to understand signature manipulation |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Tomperez98
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The recommended version is to use pytest- native parameterization
If you roll your own, youd need to understand signature manipulation