• 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报告

    三、这种做法的优点

    • 可以自定义各式各样的标题
    • 单独一个值去维护标题值
    • 可读性比较好,容易维护
  • 相关阅读:
    idea jsp无法加载<c:foreach>循环遍历数据
    java POI读取Excel文件
    Javaweb中请求的资源[/Servlet]不可用解决方案
    大作业第一阶段冲刺(一)
    hive中sql左外连接查询列值为null
    关于ECharts在jsp页面无法显示的问题
    echarts通过ajax实现数据加载
    读书笔记
    解决:[Err] 1064
    idea启动Tomcat报错Artifact testdemo1:war exploded: Error during artifact deployment. See server log for details.问题解决
  • 原文地址:https://www.cnblogs.com/longronglang/p/14129955.html
Copyright © 2020-2023  润新知