Pytest TypeError:\uyu init_u9()获得意外的关键字参数“browser”

2024-04-26 22:14:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我的confest.py中添加pytest_addoption(parser)Here are the official Pytest docs

但如果我试着开始测试我明白了

TypeError: __init__() got an unexpected keyword argument 'browser'

Confest.py

^{pr2}$

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)

Tags: thepyselfbrowserparserhereinitpytest
1条回答
网友
1楼 · 发布于 2024-04-26 22:14:22

解决方案

您应该使用Application(browser)(在忏悔.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'])

相关问题 更多 >