How to pass parameters/values to the pytest's fixture function from the test methods?

66 views Asked by At

There are some variables defined in the test methods whose value I want to use in the setup fixture I have created, to elaborate it more, following is the problem:


@pytest.fixture
def fixture_method():
  ....
  ....

  **print(variable_from_test_method)**

  ....
  ....
  

def test_case():

 variable_from_test_method = 'This is my variable's value which I want to pass to fixture'

What I want to achieve is that I want to use the variable named 'variable_from_test_method ' defined in my test method named 'test_case' into my fixure named 'fixture_method'.

I was searching for some solution by using 'request' object but unable to understand how I can bring it into use.

1

There are 1 answers

3
Guy On

You can't, the fixture runs before the test. You can define it in the fixture and pass it to the test using return

@pytest.fixture
def fixture_method():
    variable = 'This is my variable'
    return variable


def test_case(fixture_method):
    variable = fixture_method
    print(variable) # 'This is my variable'