pythonpytestfixturesconftest

How obtain a variable into the conftest file from a test file?


Advice me please how can I obtain my TEST_NAME constant into the conftest.py file from my test files?

Let's say I have many test files which contain the same constant TEST_NAME like as follows dummy example:

# test_01.py file
TEST_NAME = "C4901. Get results using lookup feature"
...
# test_02.py file
TEST_NAME = "C4902. Verify Lookup Home Screen for test number"
...

How can I obtain the constant from each test file into the conftest.py file for using it in setup/teardown, for example?

# conftest.py file
@pytest.fixture(scope="class")
def class_setup_teardown(self, request):
    # this one I can't realize:
    test_name = how_can_I_get_this_data.TEST_NAME
    print(f"Attempting to run {test_name} test case")

I will be grateful for all your advices!

Thank you!


Solution

  • At this time, I created and used the following solution, perhaps it will be useful to someone:

    # A part of the conftest.py file:
    @pytest.fixture(scope="module")
    def get_module_info(self, request):
        test_name = getattr(request.module, "TEST_NAME", {})