2017-05-08 78 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

謝謝!作品!問題解決了。 –

相關問題