Framework Mode - Dynamic fixture selection
Folder Structure
tests/ ├── test_login.py ├── test_checkout.py ├── conftest.py utilities/ ├── browser_manager.py ├── data_loader.py config/ ├── config.yaml reports/ ├── latest_report.html pytest.ini
tests/ ├── test_login.py ├── test_checkout.py ├── conftest.py utilities/ ├── browser_manager.py ├── data_loader.py config/ ├── config.yaml reports/ ├── latest_report.html pytest.ini
conftest.py, provides dynamic fixtures that select resources based on test parameters or environment variables.tests/ use fixtures to get test data or browser instances dynamically.browser_manager.py manage browser setup and teardown, supporting dynamic fixture behavior.config/config.yaml holds environment-specific settings used by fixtures to decide which resources to provide.pytest.ini or environment variables to specify parameters like --env or --browser.conftest.py reads these parameters and selects fixtures dynamically using request.config.getoption().@pytest.fixture(params=[...]) or conditional logic inside fixtures to provide different resources.pytest-html or pytest-allure plugins to generate readable HTML or Allure reports.reports/ folder with timestamps for history.request.param and request.config.getoption() for flexibility: This allows tests to control fixture behavior without code changes.Where in this framework structure would you add a new fixture that selects a database connection dynamically based on the test environment?
pytest?request.getfixturevalue()?getfixturevalue, not getfixture.test_dynamic_fixture?
import pytest
@pytest.fixture
def fixture_a():
return 'A'
@pytest.fixture
def fixture_b():
return 'B'
@pytest.mark.parametrize('use_a', [True, False])
def test_dynamic_fixture(request, use_a):
fixture_name = 'fixture_a' if use_a else 'fixture_b'
value = request.getfixturevalue(fixture_name)
print(value)
assert value in ['A', 'B']
import pytest
@pytest.fixture
def fix1():
return 10
@pytest.fixture
def fix2():
return 20
@pytest.mark.parametrize('name', ['fix1', 'fix2'])
def test_select(request, name):
value = request.getfixture(name)
assert value in [10, 20]
getfixturevalue, not getfixture.DB_TYPE. Which approach correctly implements dynamic fixture selection in pytest?
A) Use request.getfixturevalue() inside the test to select the fixture by name.
B) Use @pytest.mark.usefixtures() with all fixtures and skip unwanted ones.
C) Use pytest_generate_tests() hook to parametrize fixtures.
D) Use yield in fixtures to switch dynamically.request.getfixturevalue() to pick the fixture by name at runtime, which fits the need.Pass the test and this pattern moves unknown