def fatorial(n): if n <= 1: return 1 else: return n*fatorial(n - 1) import pytest @pytest.mark.parametrize("entrada","esperado",[ (0,1), (1,1), (2,2), (3,6), (4,24), (5,120) ]) def testa_fatorial(entrada,esperado): assert fatorial(entrada) == esperado
The error:
ERROR collecting Fatorial_pytest.py ____________________________________________________________________ In testa_fatorial: indirect fixture '(0, 1)' doesn't exist
I dont know why I got "indirect fixture”. Any idea? I am using python 3.7 and windows 10 64 bits.
You can pass a keyword argument named indirect to parametrize to change how its parameters are being passed to the underlying test function. It accepts either a boolean value or a list of strings that refer to pytest.
You can pass arguments to fixtures with the params keyword argument in the fixture decorator, and you can also pass arguments to tests with the @pytest.
TL;DR -
The problem is with the line
@pytest.mark.parametrize("entrada","esperado",[ ... ])
It should be written as a comma-separated string:
@pytest.mark.parametrize("entrada, esperado",[ ... ])
You got the indirect fixture
because pytest couldn't unpack the given argvalues
since it got a wrong argnames
parameter. You need to make sure all parameters are written as one string.
Please see the documentation:
The builtin pytest.mark.parametrize decorator enables parametrization of arguments for a test function.
Parameters:
1. argnames – a comma-separated string denoting one or more argument names, or a list/tuple of argument strings.
2. argvalues – The list of argvalues determines how often a test is invoked with different argument values.
Meaning, you should write the arguments you want to parametrize as a single string and separate them using a comma. Therefore, your test should look like this:
@pytest.mark.parametrize("n, expected", [ (0, 1), (1, 1), (2, 2), (3, 6), (4, 24), (5, 120) ]) def test_factorial(n, expected): assert factorial(n) == expected
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With