I am creating automation framework where in few parameters I have kept in a separate JSON file. I have written the below fixture which would load the data from the JSON file in conftest.py.

import json

def get_config_values():
    with open('./Resources/config.json') as f:
        config_values = json.load(f)
    return config_values

I have kept the scope as session so that I do not need to load them for every test class. I am intending to use this return value in all the test classes which are in the below way

class TestConf:

    def test_conf_test1(self):
        print(f"Test for user a")

    def test_conf_test2(self):
        print(f"Test for user b")

I am unable to use the return value in the the tests. I have tried below ways-


Please can someone help me how can we use session fixture return values in tests. Or if there is any better way to achieve this.

  • Shouldn't it be defined as an argument to the test methods, def test_conf_test(self, config_values):? See e.g. docs.pytest.org/en/stable/… – jonrsharpe Aug 17 at 11:04
  • Yes. I wanted to avoid calling it for every test as there might be hundreds of tests and every time it will load the json file for the values. Hence, I had made the fixture of session scope. Should I change the scope of the fixture as well then? – Pypal Aug 17 at 20:16

If I'm not mistaken, usefixtures can't be used in this way, that is to return values to each of your test methods. You'd use this for like cleanup tasks like in the example here.

For what you're asking about, you'd need your fixture as a parameter:

class TestConf:

    def test_conf_test1(self, get_config_values):
        print(f"Test for user a")

    def test_conf_test2(self, get_config_values):
        print(f"Test for user b")
| improve this answer | |
  • Thanks for the reply. usefixture I had seen in various questions answered in this forum used for including a fixture for class. Hence, I tried using that fixture. Also, as I wanted to avoid calling the fixture for every method I was thinking to call it once using usefixtures. – Pypal Aug 17 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.