In my conftest.py I added following code
import pytest
from selenium import webdriver
def pytest_addoption(parser):
parser.addoption(
"--browser_name", action="store", default="chrome"
)
@pytest.fixture(scope="class")
def setup(request):
browser_name = request.config.getoption("--browser_name")
if browser_name == "chrome":
driver = webdriver.Chrome()
elif browser_name == "firefox":
driver = webdriver.Firefox()
elif browser_name == "Edge":
driver = webdriver.Edge()
driver.get("https://rahulshettyacademy.com/angularpractice/")
driver.maximize_window()
request.cls.driver = driver
yield
driver.close()
I want to choose browser name from command line, but pytest does not recognize it when I run my tests by pytest --browser_name firefox
. What might be the problem?
ERROR message:
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --browser_name
inifile: None
rootdir: /home/*****/PycharmProjects/SeleniumFramework
CodePudding user response:
Possibly an issue with how you're using the fixture, or the location of you're conftest. Perhaps you have an issue with your version of pytest
? The following works with pytest-4.6.9
conftest.py
import pytest
def pytest_addoption(parser):
parser.addoption("--browser_name", action="store", default="chrome")
@pytest.fixture(scope="class")
def setup(request):
if (browser_name := request.config.getoption("--browser_name")) == "chrome":
print("chrome")
else:
print("Not chrome")
test_foo.py
def test_foo(setup):
pass
Then with pytest test_foo.py -s --browser_name="firefox"
I get
collected 1 item
test_foo.py Not chrome
.
===================== 1 passed in 0.07 seconds =========================