Я новичок в pytest и, возможно, я не так хорошо разбираюсь в параметризации, как хотелось бы.Моя цель - передать имя браузера во время выполнения теста, чтобы сообщить pytest, какой веб-драйвер я хочу использовать.
Это мой код:
conftest.py
def pytest_addoption(parser):
parser.addoption("--browser", action="store", default="chrome")
webdriver.py
@pytest.fixture(params=["chrome", "firefox", "safari"])
def whichDriver(request):
browser_list = request.config.getoption("--browser")
driver_map = {
"chrome": webdriver.Chrome(),
"firefox": webdriver.Firefox(),
"safari": webdriver.Safari()
}
if request.param in browser_list:
driver = driver_map[request.param]
return driver
else:
pytest.skip("Not running tests")
class Driver:
def __init__(self):
self.instance = whichDriver
def navigate(self, url):
if isinstance(url, str):
self.instance.get(url)
else:
raise TypeError("URL should be a string.")
test_login.py
class TestLogin(unittest.TestCase):
def setUp(self):
self.driver = Driver()
self.driver.navigate(constants.login_url)
def test_login(self):
login_screen = LoginScreen(self.driver)
login_screen.log_in(user_name=constants.my_login, user_password=constants.my_password)
def tearDown(self):
self.driver.instance.quit()
Когда я бегу:
pytest --browser=firefox TC/test_login.py
Я получаю:
AttributeError: 'function' object has no attribute 'get'