I am new to pytest and I have written a test function with fixture but I am receiving an error that fixture variable is not found. Here is the code:
@pytest.fixture(scope="module")
def func1(arg1,arg2):
return arg1 arg2
def test_func1(func1):
assert func1== 4
When I run this file, I get the error E fixture arg1 not found
. If I define arg1
and arg2
globally and not pass them through function, this will work fine, but how can I pass an argument to fixture function.
CodePudding user response:
You need to define arg1 and arg2 as fixtures themselves, then it should work. Do as follows:
@pytest.fixture(scope="module")
def argX():
# return 'enter definition of argX here'
CodePudding user response:
It's not completely clear how you want the arguments to be passed to the fixture. I can only guess that they are supposed to be parameters of your test function in which case you need to define them in @pytest.mark.parametrize
and set them as indirect
so that they are passed to func1
fixture:
import pytest
@pytest.fixture()
def func1(request):
return request.param[0] request.param[1]
@pytest.mark.parametrize('func1', [(1, 3)], indirect=True)
def test_func1(func1):
assert func1 == 4