Python Pytest:我如何将这个fixture转换为接受类的参数?
当前代码: conftest.pyPython Pytest:我如何将这个fixture转换为接受类的参数?,python,selenium,selenium-webdriver,pytest,Python,Selenium,Selenium Webdriver,Pytest,当前代码: conftest.py @pytest.fixture(scope="class") def setup_portal(request): ### NOTE: I want this to be: def setup_portal(request,login=False) "@pytest.mark.usefixtures" : applied to the class == a fixture to every test m
@pytest.fixture(scope="class")
def setup_portal(request): ### NOTE: I want this to be: def setup_portal(request,login=False)
"@pytest.mark.usefixtures" : applied to the class == a fixture to every test methods in the class.
Since the fixture "setup_portal" has a scope == "class" -> webdriver will be initialized only once per each class."""
# setup chrome driver ...
## login into portal
# TODO:
## if login: # login
request.cls.driver = chrome_driver
yield chrome_driver
chrome_driver.close()
@pytest.fixture(scope="class")
def setup_portal():
def _setup_portal(request, login=False):
"@pytest.mark.usefixtures" : applied to the class == a fixture to every test methods in the class.
Since the fixture "setup_portal" has a scope == "class" -> webdriver will be initialized only once per each class."""
# setup chrome driver ...
## login into portal
if login:
## login...
request.cls.driver = chrome_driver
yield chrome_driver
chrome_driver.close()
return _setup_portal()
test_file.py
@pytest.mark.usefixtures("setup_portal") ### initialize driver
class TestMusimotionPage:
... do stuff
@pytest.mark.usefixtures("setup_portal") ### not sure how to pass : login == True
class TestMusimotionPage:
setup_portal(login==True). ## doesn't work
... do stuff
目标:使用参数login==True请求设置门户设备,然后登录 类似问题的一种解决方案是将“设置门户”定义为夹具中的一种方法,如下所示: 问题:正在使用以下方法调用夹具: @pytest.mark.usefixtures(“设置门户”) 所以我不能通过这样的论证: conftest.py
@pytest.fixture(scope="class")
def setup_portal(request): ### NOTE: I want this to be: def setup_portal(request,login=False)
"@pytest.mark.usefixtures" : applied to the class == a fixture to every test methods in the class.
Since the fixture "setup_portal" has a scope == "class" -> webdriver will be initialized only once per each class."""
# setup chrome driver ...
## login into portal
# TODO:
## if login: # login
request.cls.driver = chrome_driver
yield chrome_driver
chrome_driver.close()
@pytest.fixture(scope="class")
def setup_portal():
def _setup_portal(request, login=False):
"@pytest.mark.usefixtures" : applied to the class == a fixture to every test methods in the class.
Since the fixture "setup_portal" has a scope == "class" -> webdriver will be initialized only once per each class."""
# setup chrome driver ...
## login into portal
if login:
## login...
request.cls.driver = chrome_driver
yield chrome_driver
chrome_driver.close()
return _setup_portal()
test_file.py
@pytest.mark.usefixtures("setup_portal") ### initialize driver
class TestMusimotionPage:
... do stuff
@pytest.mark.usefixtures("setup_portal") ### not sure how to pass : login == True
class TestMusimotionPage:
setup_portal(login==True). ## doesn't work
... do stuff
怎么办?您的返回语句应该是
return\u setup\u portal
,您需要省略parensohoops,我稍后会尝试。thanks@gold_cy我无法让它工作b/c TestMusimotionPage无法完成:setup\u portal(login=True),因此我不知道如何传递该参数。我试图对fixture调用进行参数化,并通过request.params访问它,但这不起作用,因为这是一个类定义。需要通过mark.usefixtures而不是mark.Parameterize访问