L2_计算器实战
概述内容
- 添加了allure标识
- 添加了异常处理
- 添加了raisers自定义失败内容
- 添加了预期失败用例
- 添加了跳过用例
标识@allure
项目名称:@allure.epic(‘项目名称’)
模块名称:@allure.feature(‘模块名称’)在类的上方
接口名称: @allure.story(‘用例名称:第一条小白1’) 再用用例上面
测试用例标题:@allure.title(‘测试用例标题第一种方式,打印信息小白’)
allure.dynamic.title(‘测试用例标题第二种方式,打印信息小白’)
标记为跳过: @pytest.mark.skip(reason="不需要执行test_01")
标记为预期失败:@pytest.mark.xfail(reason="bug待修复")
pytests.py 工程代码
import os
import allure
import yaml
import pytest
'''
项目名称:@allure.epic(‘项目名称’)
模块名称:@allure.feature(‘模块名称’)在类的上方
接口名称: @allure.story(‘用例名称:第一条小白1’) 再用用例上面
测试用例标题:@allure.title(‘测试用例标题第一种方式,打印信息小白’)
allure.dynamic.title(‘测试用例标题第二种方式,打印信息小白’)
标记为跳过: @pytest.mark.skip(reason="不需要执行test_01")
标记为预期失败:@pytest.mark.xfail(reason="bug待修复")
'''
'''被测类'''
class Calculator:
def add(self, a, b):
if a > 99 or a < -99 or b > 99 or b < -99:
print("请输入范围为【-99, 99】的整数或浮点数")
return "参数大小超出范围"
return a + b
def div(self, a, b):
if a > 99 or a < -99 or b > 99 or b < -99:
print("请输入范围为【-99, 99】的整数或浮点数")
return "参数大小超出范围"
return a / b
'''读取yaml用例'''
with open("data.yaml", "r" ,encoding='utf-8',errors='ignore') as file:
data = yaml.safe_load(file)
'''执行yaml用例'''
@allure.epic('霍格沃兹')
@allure.feature('加除运算模块')
class TestParams:
shili = Calculator()
@allure.story('标记跳过用例')
@allure.title('跳过用例')
@pytest.mark.skip(reason="不需要执行")
def test_Calculator_add(self,case):
# print(case)
'''验证add方法'''
assert 1 == 1
@allure.story('标记预期失败用例')
@allure.title('预期失败用例')
@pytest.mark.xfail(reason="预期失败")
def test_Calculator_add(self,case):
# print(case)
'''验证add方法'''
assert 1 == 3
@pytest.mark.parametrize("case", data)
@allure.story('add')
@allure.title('加法运算')
def test_Calculator_add(self,case):
# print(case)
'''验证add方法'''
assert self.shili.add(case[1], case[2]) == case[3]
@pytest.mark.parametrize("case", data)
@allure.story('div')
@allure.title('除法运算')
def test_Calculator_div(self,case):
# try 对异常用例进行处理
try:
# print(case)
'''验证div方法'''
assert self.shili.div(case[1], case[2]) == case[4]
except:
# 异常处理,使用rasises 定义错误值
with pytest.raises(ValueError) as exc_info:
raise ValueError("需要输入数字")
assert exc_info.type is ValueError
assert exc_info.value.args[0] == "需要输入数字"
if __name__ == '__main__':
'''执行命令
pytest pytests.py --alluredir=./result/1
allure serve ./result/1
'''
os.system('pytest pytests.py')
yaml文件内容
-
- add-正确入参整数测试-等价类
- 10
- 20
- 30
- 0.5
-
- add-正确入参小数测试-等价类
- 10.6
- 20.87
- 31.47
- 0.5079060852898898
- - add-输入字符入参测试-等价类
- ands
- ands
- 不可输入字符串
- 不可输入字符串
- - add-输入特殊字符入参测试-错误推测法
- ands
- ands
- 不可输入特殊字符
- 不可输入特殊字符
- - add-a为100入参测试-边界值
- 100
- 20
- 参数大小超出范围
- 参数大小超出范围
- - add-a为99入参测试-边界值
- 99
- 20
- 119
- 4.95
- - add-a为-99入参测试-边界值
- -99
- 20
- -79
- -4.95
- - add-a为-100入参测试-边界值
- -100
- 20
- 参数大小超出范围
- 参数大小超出范围
- - add-b为100入参测试-边界值
- 20
- 100
- 参数大小超出范围
- 参数大小超出范围
- - add-b为99入参测试-边界值
- 20
- 99
- 119
- 0.20202020202020202
- - add-b为-99入参测试-边界值
- 20
- -99
- -79
- -0.20202020202020202
- - add-b为-100入参测试-边界值
- 20
- -100
- 参数大小超出范围
- 参数大小超出范围
报告展示