• 接口自动化测试:pytest自定义fixture传参request


    1、自定义一个带参数的fixture

    • 用request接收参数,固定写法,换成其他的会报错
    • 如果是字典形式,按照字典取值方式获取参数值
    import pytest
    
    
    @pytest.fixture(scope="module")
    def log_fixture(request):
        print("前置条件")
        # param = request.param
        user = request.param['user']
        pwd = request.param['pwd']
        print('接收到的用户名:%s,密码:%s' % (user,pwd))
        yield
        print("后置条件")

    2、测试用例中调用fixture

    • 在向fixture传参时,多个参数可以用字典方式传入
    • @pytest.mark.parametrize中需要传入indirect=True,指定参数以函数方式运行,否则就没有fixture的作用了
    @pytest.mark.parametrize("log_fixture",[{'user':'lpj','pwd':123456}],indirect=True)
    # @pytest.mark.parametrize("log_fixture",[1,2,3])
    @pytest.mark.parametrize("sex",["M","F"])
    def test_01(log_fixture,sex):
        print("sex:",sex)
        print("测试用例01111")

    上述代码执行结果

  • 相关阅读:
    8.25 欢乐emmm赛
    树专练
    字符串知识点大集合
    8.12 小组解题
    暑假大联欢 happynk 2019.8.11
    游记-多省联考 2019
    图论-匈牙利算法模板
    数论-哈哈哈好快乐
    数论-线性基
    其他-私人♂收藏(比赛记录 Mar, 2019)
  • 原文地址:https://www.cnblogs.com/Xiaojiangzi/p/13786441.html
Copyright © 2020-2023  润新知