• Pytest 学习(二十五)- 解决pytest参数化测试标题都一样问题


    一、前言

    使用参数化测试化后,allure的报告如下显示:

    源代码如下:

    # -*- coding: utf-8 -*-
    # @Time    : 2020/12/13 17:27
    # @Author  : longrong.lang
    # @FileName: test_param_title.py
    # @Software: PyCharm
    # @Cnblogs :https://www.cnblogs.com/longronglang
    import allure
    import pytest
    
    
    @pytest.fixture()
    def loginTo(request):
        # 传入参数,登录
        param = request.param
        print(f"
    userName : {param['userName']} ,passWord : {param['passWord']}")
        # 登录成功,返回
        return {"code": "0", "msg": "success!"}
    
    
    data = [{"userName": "jojo", "passWord": "1"},
            {"userName": "rongrong", "passWord": "1"},
            {"userName": "lilei", "passWord": "1"}
            ]
    
    
    @allure.story("讲述登录成功,返回成功的故事!")
    @allure.title("测试登录接口")
    @pytest.mark.parametrize("loginTo", data, indirect=True)
    def test_loginTo(loginTo):
        assert loginTo['msg'] == 'success!'
    
    

    从代码看出,其实就是@allure.title("测试登录接口"),写死的呀。

    二、如何解决

    那么怎么搞?

    这个问题问的好呀,动态的标题可以实现吗?必须能呀。

    方案一

    直接拼接loginTo
    示例代码如下:

    # -*- coding: utf-8 -*-
    # @Time    : 2020/12/13 17:27
    # @Author  : longrong.lang
    # @FileName: test_param_title.py
    # @Software: PyCharm
    # @Cnblogs :https://www.cnblogs.com/longronglang
    import allure
    import pytest
    
    
    @pytest.fixture()
    def loginTo(request):
        # 传入参数,登录
        param = request.param
        print(f"
    userName : {param['userName']} ,passWord : {param['passWord']}")
        # 登录成功,返回
        return {"code": "0", "msg": "success!"}
    
    
    data = [{"userName": "jojo", "passWord": "1"},
            {"userName": "rongrong", "passWord": "1"},
            {"userName": "lilei", "passWord": "1"}
            ]
    
    
    @allure.story("讲述登录成功,返回成功的故事!")
    @allure.title("测试登录接口 {loginTo}")
    @pytest.mark.parametrize("loginTo", data, indirect=True)
    def test_loginTo(loginTo):
        assert loginTo['msg'] == 'success!'
    
    

    allure报告

    这样会使得报告,更具可读性,直观一些

    方案二

    使用元祖来做参数化,加一个参数为标题
    示例代码如下:

    # -*- coding: utf-8 -*-
    # @Time    : 2020/12/13 17:27
    # @Author  : longrong.lang
    # @FileName: test_param_title.py
    # @Software: PyCharm
    # @Cnblogs :https://www.cnblogs.com/longronglang
    import allure
    import pytest
    
    data = [("jojo", "1", "登录成功的用例"),
            ("rongrong", "1", "登录成功的用例"),
            ("lilei", "1", "登录失败的用例")
            ]
    
    
    @allure.story("讲述登录成功,返回成功的故事!")
    @allure.title("测试登录接口 {title}")
    @pytest.mark.parametrize("userName,password,title", data)
    def test_loginTo(userName, password, title):
        print(userName + "	" + password + "	" + title)
    
    

    allure报告

    三、这种做法的优点

    • 可以自定义各式各样的标题
    • 单独一个值去维护标题值
    • 可读性比较好,容易维护
  • 相关阅读:
    CF1260F
    牛客挑战赛34 A~E
    CSP-S2019游记&拆塔记
    6424. 【NOIP2019模拟2019.11.13】我的订书机之恋
    CF1257E/F
    6423. 【NOIP2019模拟11.11】画
    1222/2516. Kup
    Comet OJ
    浅析CSS定位
    css文字颜色渐变的3种实现
  • 原文地址:https://www.cnblogs.com/longronglang/p/14129955.html
Copyright © 2020-2023  润新知