-1

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!

Alex AL
  • 1
  • 2
  • you `import` them, which will get super annoying with lots of files, so the other option is to just load your files "as files" and parse their content into a giant dictionary keyed on filename (without the extension). – Mike 'Pomax' Kamermans Feb 25 '23 at 16:58
  • One way to achieve this is to define the TEST_NAME constant in a separate file, such as a *constants.py* file. Then you can import the constant in both your test files and the conftest.py file. – Daniel Hao Feb 25 '23 at 17:00

1 Answers1

0

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="class")
def class_setup_teardown(self, request):
    """Setup and teardown for classes"""

    # Get the name of the test module
    module_name = str(request.node.module).split("'")[1]
    # Import testmodule and get the TEST_NAME constant
    test_module = importlib.import_module(module_name)
    test_name = test_module.TEST_NAME
Alex AL
  • 1
  • 2