pytest fixture传参和参数化

1、fixture传参:可直接在方法上传入参数,fixture也可作为其他fixture的参数实现相互调用

@pytest.fixture(scope='session')
#方法下注释内容为连接selenoid
def browser():
    # capabilities = {
    #     "browserName": "chrome",
    #     "browserVersion": "latest",
    #     "selenoid:options": {
    #         "enableVNC": True,
    #         "enableVideo": False
    #     }
    # }
    # option=webdriver.ChromeOptions()
    # option.add_argument('--headless')
    # option.add_argument('--no-sandbox')
    # option.add_argument('--disable-dev-shm-usage')
    # # option.binary_location(chrome_path)
    # driver = webdriver.Remote(command_executor="http://test:用户名@ip:port/wd/hub",
    # desired_capabilities=capabilities)
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)
    driver.maximize_window()
    # return driver
    yield driver
    driver.quit()



#可以直接传入fixture:browser,可以传入参数request接收
@pytest.fixture(scope='session')
def login(request, browser):
    login_type = request.param
    lc = LoginCase().login_success(browser, login_type)
    if LoginCase().is_pm_user(login_type):
        return lc
    else:
        return lc

2、fixture嵌套使用,如下:fixture open_configuration使用fixture login

#fixture嵌套使用
#login fixture不传参
@pytest.fixture(scope='module')
def open_configuration(login):

    ConfigurationCase().open_configuration(login)
    return login

#login fixture传参
@pytest.fixture(scope='module')
def check_configuration_list(request, open_configuration):
    login_type = request.param
    ConfigurationCase().check_configuration_list(open_configuration, login_type)
    return open_configuration

3、case中使用,如下:先参数化fixture,然后直接在方法中使用

indirect=True:login作为函数调用

login_type = ["seller_login_data", "pm_login_data"]    


@pytest.mark.run(order=1)
# login fixture传入参数login_type[1]
@pytest.mark.parametrize("login", [login_type[1]], indirect=True)
# check_configuration_list fixture传入参数login_type[0]
@pytest.mark.parametrize("check_configuration_list", [login_type[0]], indirect=True)
#设置普通参数
@pytest.mark.parametrize("review", ["pmReviewConfigurationApprove"])
#设置失败重1次:reruns=1,下次重试延迟时间3s:reruns_delay=3
@pytest.mark.flaky(reruns=1, reruns_delay=3)
def test_review_configuration_approve(self, check_configuration_list, review):
#传入fixture可以为上面参数化的fixture,也可以是使用了上面参数化的fixture
#如:fixture A使用了fixture check_configuration_list
    cpe = ConfigurationPage(check_configuration_list)

4、fixture参数化

4.1

@pytest.mark.parametrize('dirname, expected', [
    ('dir1_fixture', 'expected1'),
    ('dir2_fixture', 'expected2')])
def test_directory_command(dirname, expected, request):
    result = my_package.directory_command(request.getfixturevalue(dirname))
    assert result == expected

4.2 另一种方式

@pytest.mark.parametrize('dirname, expected', [
    (pytest.lazy_fixture('dir1_fixture'), 'expected1'),
    (pytest.lazy_fixture('dir2_fixture'), 'expected2')])
def test_directory_command(dirname, expected):
    result = my_package.directory_command(dirname)
    assert result == expected
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值