pytest框架_@pytest.mark.parametrize()

这个博客展示了如何使用Python的pytest库进行参数化测试。通过`@pytest.mark.parametrize`装饰器,实现了单参数单值、单参数多值以及多参数多值的测试用例。每个测试用例在运行时都会启动浏览器并验证传入值的正确性,例如算术运算的预期结果。这些测试用例覆盖了不同类型的输入,包括字符串、整数、字典和元组,确保了代码的全面测试。
摘要由CSDN通过智能技术生成

@pytest.mark.parametrize()
怎么说,三大功能:
查看测试用例test_case_9、10、11

# coding=utf-8
import pytest

class Testlogin001:

    # 单参数单值
    @pytest.mark.parametrize('arg', [1])
    def test_case_9(self, arg):
        print("传入的值为:{}".format(arg))
        assert arg == 1

    # 单参数多值
    @pytest.mark.parametrize('arg',['abc',1,{'a':1,'b':3},(4,5)])
    def test_case_10(self, arg):
        print(f"传入的值为:{arg}")

    # 多参数多值
    @pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("5-2", 3), ("5*2", 10)])
    def test_case_11(self, test_input, expected):
        print(f"原值:{test_input} 期望值{expected}")
        assert eval(test_input) == expected


if __name__ == "__main__":
    pytest.main(['-vs', 'test_1.py'])

C:\Python39\python.exe D:/se_frame/Cases/MapAaaCases/test_1.py
test_1.py::Testlogin001::test_case_9[1] 
启动浏览器
传入的值为:1
PASSED
退出浏览器

test_1.py::Testlogin001::test_case_10[abc] 
启动浏览器
传入的值为:abc
PASSED
退出浏览器

test_1.py::Testlogin001::test_case_10[1] 
启动浏览器
传入的值为:1
PASSED
退出浏览器

test_1.py::Testlogin001::test_case_10[arg2] 
启动浏览器
传入的值为:{'a': 1, 'b': 3}
PASSED
退出浏览器

test_1.py::Testlogin001::test_case_10[arg3] 
启动浏览器
传入的值为:(4, 5)
PASSED
退出浏览器

test_1.py::Testlogin001::test_case_11[3+5-8] 
启动浏览器
原值:3+5 期望值8
PASSED
退出浏览器

test_1.py::Testlogin001::test_case_11[5-2-3] 
启动浏览器
原值:5-2 期望值3
PASSED
退出浏览器

test_1.py::Testlogin001::test_case_11[5*2-10] 
启动浏览器
原值:5*2 期望值10
PASSED
退出浏览器

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿_焦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值