Repository navigation
How to call fixtures if cli is not given and call fixtures when cli is given. #10197
Replies: 2 comments
|
Use one indirectly parametrized # conftest.py
import pytest
from selenium import webdriver
BROWSERS = ("chrome", "firefox", "edge")
def pytest_addoption(parser):
for name in BROWSERS:
parser.addoption(f"--{name}", action="store_true", default=False)
def pytest_generate_tests(metafunc):
if "setup" in metafunc.fixturenames:
selected = [name for name in BROWSERS if metafunc.config.getoption(name)]
metafunc.parametrize("setup", selected or list(BROWSERS),
indirect=True, scope="session")
@pytest.fixture(scope="session")
def setup(request):
constructors = {"chrome": webdriver.Chrome,
"firefox": webdriver.Firefox,
"edge": webdriver.Edge}
driver = constructors[request.param]()
yield driver
driver.quit()Your test can keep using
Pass your existing options/service arguments to the corresponding constructors as needed. Creating the driver inside the fixture also lets the I checked the collection, session reuse and teardown behavior on pytest 9.2.0 using stub drivers: two tests produced 6, 2 and 4 passing cases for those three commands, with one construction and cleanup per selected browser. Actual browsers were not launched. Reference: custom parametrization with Prepared with Codex assistance; the selection behavior above was checked locally. |
|
Use def pytest_addoption(parser):
parser.addoption("--browser", action="store", default=None
choices=["chrome", "firefox", "edge"])
def pytest_generate_tests(metafunc):
if "driver" in metafunc.fixturenames:
opt = metafunc.config.getoption("--browser")
metafunc.parametrize("driver", [opt] if opt else
["chrome", "firefox", "edge"], indirect=True)
@pytest.fixture
def driver(request):
d = build_driver(request.param) # your per-browser webdriver code
yield d
d.quit()Then — |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I made these fixtures in my
conftest.py:And I have a test case in my test folder like this:
How can i make a combined fixture which i can even use in my test cases, so that when i don't input any cli command like :
pytest --chromeit runs the setup fixture and when i run a cli command it ignores the setup fixture and runs the specific fixture that i've mentioned.eg:
pytest --chrome- > test runs with chrome fixturepytest --chrome --firfox- > test runs with chrome as well as firefox fixturepytest- > test runs only setup fixture.All reactions