zoukankan      html  css  js  c++  java
  • 参数化(parametrize)allure用例描述的两种方式 第二种重点

    第一种方式:ids

    1.参数化(pytest.mark.parametrize)

    # test_a.py
    import pytest
    import allure
    
    
    
    def login(username, password):
        '''登录'''
        print("输入账号:%s" % username)
        print("输入密码:%s" % password)
        # 返回
        return {"code": 0, "msg": "success!"}
    
    
    # 测试数据
    test_datas = [
        ({"username": "yoyo1", "password": "123456"}, "success!"),
        ({"username": "yoyo2", "password": "123456"}, "failed!"),
        ({"username": "yoyo3", "password": "123456"}, "success!"),
    ]
    
    
    @allure.story("登录用例")
    @pytest.mark.parametrize("test_input,expected",
                             test_datas
                             )
    def test_login(test_input, expected):
        '''测试登录用例'''
        # 获取函数返回结果
        result = login(test_input["username"], test_input["password"])
        # 断言
        assert result["msg"] == expected

    运行用例

    > pytest --alluredir ./report test_a.py
    > allure serve ./report

     这样生成的报告在用例列表里面并不能很友好的展示出每个用例的执行场景,只知道哪个用例报错了。
    于是需要对每个用例加上描述,加一个 ids 参数

    2.ids参数使用

    在用例部分代码里面加个 ids 参数,用于描述每个用例的运行场景。

    @allure.story("登录用例")
    @pytest.mark.parametrize("test_input,expected",
                             test_datas,
                             ids=[
                                 "输入正确账号,密码,登录成功",
                                 "输入错误账号,密码,登录失败",
                                 "输入正确账号,密码,登录成功",
                             ]
                             )
    def test_login(test_input, expected):
        '''测试登录用例'''
        # 获取函数返回结果
        result = login(test_input["username"], test_input["password"])
        # 断言
        assert result["msg"] == expected

    第二种方式:allure.title

    @allure.title("用例描述,测试输入:{test_input}")

    在 allure_pytest/utils.py 源码里面可以找到对应的代码

    # allure_pytest/utils.py
    
    def allure_name(item, parameters):
        name = escape_name(item.name)
        title = allure_title(item)
        return title.format(**parameters) if title else name

    当没有加allure.title()时候,用例的描述就是 item.name 值(也就是上面的 ids 用例的名称),
    如果加了allure.title(),那么用例的描述就是添加的title值,这两个地方取其中的一个。

    重点来了:把用例描述当成参数传给测试用例

    import pytest
    import allure
    # 作者:上海-悠悠 QQ交流群:779429633
    
    def login(username, password):
        '''登录'''
        print("输入账号:%s" % username)
        print("输入密码:%s" % password)
        # 返回
        return {"code": 0, "msg": "success!"}
    
    
    # 测试数据
    test_datas = [
        ({"username": "yoyo1", "password": "123456"}, "success!", "输入正确账号,密码,登录成功"),
        ({"username": "yoyo2", "password": "123456"}, "failed!", "输入错误账号,密码,登录失败"),
        ({"username": "yoyo3", "password": "123456"}, "success!", "输入正确账号,密码,登录成功"),
    ]
    
    
    @allure.story("登录用例")
    @allure.title("{title}")
    @pytest.mark.parametrize("test_input,expected,title",
                             test_datas
                             )
    def test_login(test_input, expected, title):
        '''测试登录用例'''
        # 获取函数返回结果
        result = login(test_input["username"], test_input["password"])
        # 断言
        assert result["msg"] == expected

  • 相关阅读:
    如何计算两个日期之间相差天数
    解决并发问题的小技巧
    Linq实现下拉框绑定
    No DataType in DataTemplate in Windows Phone(二)
    使用TOAD操作oracle初步
    使用log4net记录server Log
    尘世一场烟火
    No DataType in DataTemplate in Windows Phone(—)
    MVC设置初始页时发生的无法找到资源的简单错误
    oracle 使用in的灵异事件
  • 原文地址:https://www.cnblogs.com/bigbox/p/13138552.html
Copyright © 2011-2022 走看看