2017-05-08 100 views
1

我尝试在我的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) 
+0

请检查的缩进;这在Python中很重要。 – jonrsharpe

回答

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']) 
+0

谢谢!作品!问题解决了。 –

相关问题