Pytest TypeError:__ init __()得到一个意外的关键字参数'browser'

时间:2017-05-08 23:00:04

标签: python python-3.x pytest

我尝试在pytest_addoption(parser)中添加confest.pyHere 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)

1 个答案:

答案 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'])