Have to fetch the command line arguments and assign them to global variables in the python script under nontest modules using a pytest.
Example Code:
# conftest.py
def pytest_addoption(parser):
parser.addoption("--name", action="store", default="default name")
# test_sample.py
import pytest
@pytest.fixture
def name(req):
return req.getoption("name")
# Here, I require the command line argument assigned to a global variable "glob_name"
glob_name = name
foo="""
def test_"""+glob_name+"""():
print("Inside test function with a global name")
"""
exec(foo)
Command Used:
pytest -q -s test_sample.py --name google
Error occurred:
========================================================================================== ERRORS ==========================================================================================
_____________________________________________________________________________ ERROR collecting test_sample.py ______________________________________________________________________________
test_sample.py:10: in <module>
def test_"""+glob_name+"""():
E TypeError: cannot concatenate 'str' and 'function' objects
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1 error in 0.12 seconds
Expected Output:
It should have to append the str "google" to the test function name and have to execute that function.
I would appreciate it if anyone can revert back with a solution for the above-mentioned issue.