赠送录播笔记
官网:pytest.org
测试类中不能包含__init__方法
运行:

测试用例常用参数:


pytest框架结构
unittest官网:https://docs.python.org/3/library/unittest.html


参数化:
* 单个参数化:参数名称写在字符串中,参数值用列表传递
* 多个参数,参数名称写在字符串中,参数值用列表或者元祖的方式传递
* 测试用例取别名:ids=
* 笛卡尔积:用两个装饰器分别传入参数
* 从yaml中读取参数:数据读取成为参数化中需要的参数形式
参数化:
@pytest.mark.parametrize( "a,b,expect", [ (2,3,5), (-2,-3,-5), (-0.2,-0.3,-0.5) ],ids=['int','float','negative'] ) def test_add1(self,a,b,expect): result = Calculator.add(0,a,b) assert result ==expect print("这是test_add1")
其中
ids=['int','float','negative']
输出结果如下,给每一组用例重新命名
注意:有几组用例,就要命名几个,否则会报错,比如上面是三组用例,那么重新命名就要写三个。


对于浮点数的运算,需要如下写断言
assert round(result,2) ==expect
加入round意为保留两位小数,这样就避免了报上述错误;
课程代码
# Author:Han # python:3.9 import pytest import yaml from python_code.calc import Calculator with open('./datas/calc.yaml') as f: data = yaml.safe_load(f)['add'] print(data) add_datas = data['datas'] print(add_datas) add_myid = data['myid'] print(add_myid) class TestCalc: def setup_class(self): self.calc = Calculator() print("计算开始!!!") def teardown_class(self): print("计算结束啦!!!") @pytest.mark.parametrize( "a,b,expect", add_datas,ids = add_myid ) def test_add1(self,a,b,expect): result = Calculator.add(0,a,b) if isinstance(result,float): result = round(result,2) assert result ==expect print("这是test_add1") @pytest.mark.parametrize('a',[1,2,3]) @pytest.mark.parametrize('b',[4,5,6]) def test_demo(self,a,b): print(f"a = {a},b = {b}")
yaml文件写法:
add:
datas:
- - 1
- 1
- 2
- - 100
- 100
- 200
- [0.1,0.2,0.3]
- [-1,-1,-2]
myid:
- 'int'
- 'float'
- 'negative'
- 'banana'
业务代码
# Author:Han
# python:3.9
class Calculator:
#加法
def add(self,a,b):
return a + b
#减法
def sub(self,a,b):
return a-b
#乘法
def mul(self,a,b):
return a*b
#除法
def div(self,a,b):
return a/b
课后作业,补全计算器测试用例
测试用例:
# Author:Han
# python:3.9
import pytest
import yaml
from python_code.calc import Calculator
with open('./datas/calc.yaml') as f:
data = yaml.safe_load(f)
add_datas = data['add']['datas']
add_myid = data['add']['myid']
sub_datas = data['sub']['datas']
sub_myid = data['sub']['myid']
mul_datas = data['mul']['datas']
mul_myid = data['mul']['myid']
div_datas = data['div']['datas']
div_myid = data['div']['myid']
print(data)
class TestCalc:
def setup_class(self):
self.calc = Calculator()
print("计算开始!!!")
def teardown_class(self):
print("计算结束啦!!!")
@pytest.mark.parametrize(
"a,b,expect",
add_datas,ids = add_myid
)
def test_add(self,a,b,expect):
result = Calculator.add(0,a,b)
if isinstance(result,float):
result = round(result,2)
assert result ==expect
print("这是test_add")
@pytest.mark.parametrize(
"a,b,expect",
sub_datas, ids=sub_myid
)
def test_sub(self,a,b,expect):
result = Calculator.sub(0, a, b)
if isinstance(result, float):
result = round(result, 2)
assert result == expect
print("这是test_sub")
@pytest.mark.parametrize(
"a,b,expect",
mul_datas, ids=mul_myid
)
def test_mul(self,a,b,expect):
result = Calculator.mul(0, a, b)
if isinstance(result, float):
result = round(result, 2)
assert result == expect
print("这是test_mul")
@pytest.mark.parametrize(
"a,b,expect",
div_datas, ids=div_myid
)
def test_div(self,a,b,expect):
result = Calculator.div(0, a, b)
if isinstance(result, float):
result = round(result, 2)
assert result == expect
print("这是test_div")
业务代码:
# Author:Han
# python:3.9
class Calculator:
#加法
def add(self,a,b):
return a + b
#减法
def sub(self,a,b):
return a-b
#乘法
def mul(self,a,b):
return a*b
#除法
def div(self,a,b):
return a/b
yaml文件
add: datas: - - 1 - 1 - 2 - - 100 - 100 - 200 - [0.1,0.2,0.3] - [-1,-1,-2] myid: - 'int' - 'bigint' - 'float' - 'negetive' sub: datas: - - 1 - 1 - 0 - - 200 - 100 - 100 - [0.2,0.1,0.1] - [-1,-4,3] myid: - 'int' - 'bigint' - 'float' - 'negetive' mul: datas: - - 1 - 1 - 1 - - 100 - 100 - 10000 - [0.1,0.2,0.02] - [-1,-1,1] myid: - 'int' - 'bigint' - 'float' - 'negetive' div: datas: - - 4 - 2 - 2 - - 1000 - 100 - 10 - [0.2,0.1,2] - [-4,-1,4] myid: - 'int' - 'bigint' - 'float' - 'negetive'
浙公网安备 33010602011771号