我嘗試在我的confest.py
中添加pytest_addoption(parser)
。 Here are the official Pytest docsPytest TypeError:__init __()得到了一個意外的關鍵字參數'browser'
但是,如果我嘗試啓動測試,我看到
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)
請檢查的縮進;這在Python中很重要。 – jonrsharpe