pytest学习记录(七)参数化测试

使用@pytest.mark.parametrize(argnames, argvalues, indirect=False, ids=None, scope=None)对测试进行参数化

1、常用参数:

argnames:参数名     
argvalues:参数对应值,类型必须为list                 
    当参数为一个时格式:[value]                 
    当参数个数大于一个时,格式为:[(param_value1,param_value2.....),(param_value1,param_value2.....)]
    参数值为N个,测试方法就会运行N次

2、作用范围设置:

参数化某个函数:

在测试函数上加@pytest.mark.parametrize()

	# -*- coding: utf-8 -*-
	import pytest
	from operator import add
	class TestAdd():
    @pytest.mark.parametrize("test_input1,test_input2,expected1", [(3, 5, 8),(2, 4, 6),(6, 9, 42)])                                                                                                              
    def test_test07(self, test_input1, test_input2, expected1):
        result = add(test_input1, test_input2)
        assert result == expected1

运行命令:
pytest -v .\test_case_demo_pytest.py::TestAdd::test_test07
运行结果:
在这里插入图片描述

对类下的所有函数参数化:

在测试类上加@pytest.mark.parametrize(),类下的所有测试函数均可使用参数获得值

	# -*- coding: utf-8 -*-
	import pytest
	from operator import add
	@pytest.mark.parametrize("test_input1,test_input2,expected1", [(3, 5, 8),(2, 4, 6),(6, 9, 42)])
	class TestAdd():
	    def test_test07(self, test_input1, test_input2, expected1):
	        result = add(test_input1, test_input2)
	        assert result == expected1
	
	    def test_test08(self, test_input1, test_input2, expected1):
	        result = add(test_input1, test_input2)
	        assert result == expected1	

运行命令:
pytest -v .\test_case_demo_pytest.py::TestAdd
运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值