我尝试在pytest_addoption(parser)
中添加confest.py
。 Here are the official Pytest docs
但如果我尝试开始测试,我会看到
TypeError: __init__() got an unexpected keyword argument 'browser'
Confest.py
import pytest
from fixture.application import Application
__author__ = 'Max'
fixture = None
@pytest.fixture
def app(request):
global fixture
browser = request.config.getoption("--browser")
if fixture is None:
fixture = Application(browser=browser)
else:
if not fixture.is_valid:
fixture = Application(browser=browser)
fixture.session.ensure_login(username="somename", password="somepassword")
return fixture
def pytest_addoption(parser):
# hooks for browsers
parser.addoption("--browser", action="store", default="chrome")
fixture/application.py
from selenium import webdriver
class Application:
def __init__(self,browser):
if browser == "chrome":
self.wd = webdriver.Chrome()
elif browser == "firefox":
self.wd = webdriver.Firefox()
else:
raise ValueError("Unrecognized browser %s" % browser)
答案 0 :(得分:0)
您应该使用Application(browser)
(在Confest.py)。
另一个类似问题:__init__() got an unexpected keyword argument 'user'
当您Application(browser=browser)
时,您正尝试使用keyword parameters。
from selenium import webdriver
class Application:
def __init__(self, *args, **kwargs):
if kwargs['browser'] == "chrome":
self.wd = webdriver.Chrome()
elif kwargs['browser'] == "firefox":
self.wd = webdriver.Firefox()
else:
raise ValueError("Unrecognized browser %s" % kwargs['browser'])