I run a set of pytest testcases contained in several folders. The tests use parameterization based on user input from commandline. Each folder looks roughly like this.
TESTCASES
┣ Folder_A
┣ configs
┃ ┗ Folder_A_Config.json
┣ conftest.py
┣ pytest.ini
┣ common_func.py
┗ test_folder_a.py
Like this there are 5 folders Folder_A, Folder_B and so on. Eg: Folder_B will have Folder_B_Config.json, test_folder_b.py etc.
contents of Folder_A_Config.json
{
"param1": "value1",
"param2": "value2",
"param3": "value3",
"param4": "value4"
}
contents of conftest.py
import pytest
import json
# from common_func import get_users
def pytest_addoption(parser):
parser.addoption("--config", action="store", default=None, help="Specify the config file to use")
parser.addoption("--print_values", action="store", help="Specify which values from the config file to print")
@pytest.fixture(scope="session")
def config(request):
config_file = request.config.getoption("--config")
with open(f"configs/{config_file}_Config.json", "r") as f:
config_data = json.load(f)
return config_data
def pytest_generate_tests(metafunc):
if 'print_value' in metafunc.fixturenames:
print_values = metafunc.config.option.print_values
metafunc.parametrize("print_value", print_values)
workers = ['gw%d' % i for i in range(0, 10)]
# Users = get_users()
Users = [('%d' % i, 'user%d' % j, '12345') for i, j in zip (range(10001, 10011), range(1, 11))]
@pytest.fixture()
def user_account(worker_id):
""" use a different account in each xdist worker """
if worker_id in workers:
return Users[workers.index(worker_id)]
contents of pytest.ini file
[pytest]
markers=
Functional: Functional test cases
Sanity: Sanity test cases
python_files=module_*
contents of test_folder_a.py
import pytest
@pytest.mark.Sanity
def test_one(config, print_value, user_account):
id, user, password = user_account
print(id, user, config[print_value])
assert print_value in config
@pytest.mark.Sanity
@pytest.mark.Functional
def test_two(config, print_value, user_account):
id, user, password = user_account
print(id, user, config[print_value])
assert print_value in config
@pytest.mark.Functional
def test_three(config, print_value, user_account):
id, user, password = user_account
print(id, user, config[print_value])
assert print_value in config
Now to run the functional tests in folder_A, I can use pytest --config=Folder_A --print_values=param1 -m Functional test_folder_a.py -n 2. I have to repeat this from inside the other folders to the tests in those folders.
I would like to know how to run all the functional/sanity tests in all these folders at once.
Suppose if there are 100 functional tests in all of these folders, from the TESTCASES folder, I would like to do something like this, pytest --config=all --print_values=param1 -m Functional -n 10 to make 10 tests run at once and then proceed with the next 10 tests. Also from here, I need to be able to run tests from individual folders as well.
The idea is to remove the assigning of users from the individual conftest files and put it in a single conftest file inside TESTCASES folder, from where the users should be assigned to individual tests.
I don't know how to achieve this. I have checked several stack overflow threads and other online resources but I haven't been able to achieve this. Any pointers would be very helpful.
tried to create a conftest.py and pytest.ini files in TESTCASES folder, but no luck.