pytest测试实战和练习
开头
经过前面几章的学习,这时候要来个测试实战会比较好巩固一下学过的知识
任务要求
1、实现计算器(加法,除法)的测试用例
2、使用数据驱动完成测试用例的自动生成
3、在调用测试方法之前打印【开始计算】,在调用测试方法之后打印【计算结束】
目录结构
目录解析
datas/calc_list.yaml yaml文件用来保存相关的测试用例 要使用yaml先得安装yaml相关的包 pyyaml
result目录为pytest生成的来存放测试报告的目录
calc.py 为 计算器通用函数的一个类
test_cala.py 执行pytest测试用例的文件
calc_list.yaml
# 计算器的测试用例合集
calc_case:
add_list:
- [1,2,3]
- [100,200,300]
- [0.1,0.2,0.3]
- [-1,-2,-3]
- [-0.1, 0.2, 0.3]
sub_list:
- [2,1,1]
- [300,200,100]
- [0.1,0.2,-0.1]
- [-1,-2, 1]
- [-0.1, -0.2, 0.3]
mul_list:
- [1,2,2]
- [100,200,20000]
- [0.1,0.2,0.02]
- [-1,-2,2]
- [-0.1, 0.2, 0.2]
div_list:
- [1,2,0.5]
- [100,200,0.5]
- [0.1,0.2,0.5]
- [-1,-2,0.5]
- [-0.1, 0, 0]
all_ids:
- 'int'
- 'bigint'
- 'float'
- 'negative'
- 'fail'
calc.py
# 计算器
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 division(self, a, b):
return a / b
test_calc.py
from calc import Calculator
import yaml
import pytest
import allure
with open('./datas/calc_list.yaml', 'r', encoding='utf-8') as f:
datas = yaml.safe_load(f)['calc_case']
add_list = datas['add_list'] # 加法测试用例
sub_list = datas['sub_list'] # 减法测试用例
mul_list = datas['mul_list'] # 乘法测试用例
div_list = datas['div_list'] # 除法测试用例
ids = datas['all_ids'] # 全部的标签
print(datas)
@allure.feature("计算器模块")
class TestCalc:
def setup_class(self):
print("计算器开始计算")
self.calc = Calculator()
def teardown_class(self):
print("计算器结束计算")
@allure.story("加法运算")
@pytest.mark.parametrize("a, b, expect", add_list, ids=ids)
def test_add(self, a, b, expect):
with allure.step(f"输入测试用例{a}, {b}, 预期结果为{expect}"):
result = self.calc.add(a, b)
if isinstance(result, float): # 判断浮点数
result = round(result, 2)
assert expect == result
@allure.story("减法运算")
@pytest.mark.parametrize("a, b, expect", sub_list, ids=ids)
def test_sub(self, a, b, expect):
with allure.step(f"输入测试用例{a}, {b}, 预期结果为{expect}"):
result = self.calc.sub(a, b)
if isinstance(result, float): # 判断浮点数
result = round(result, 2)
assert expect == result
@allure.story("乘法运算")
@pytest.mark.parametrize("a, b, expect", mul_list, ids=ids)
def test_mul(self, a, b, expect):
with allure.step(f"输入测试用例{a}, {b}, 预期结果为{expect}"):
result = self.calc.mul(a, b)
if isinstance(result, float): # 判断浮点数
result = round(result, 2)
assert expect == result
@allure.story("除法运算")
@pytest.mark.parametrize("a, b, expect", div_list, ids=ids)
def test_div(self, a, b, expect):
with allure.step(f"输入测试用例{a}, {b}, 预期结果为{expect}"):
result = self.calc.division(a, b)
if isinstance(result, float): # 判断浮点数
result = round(result, 2)
assert expect == result
if __name__ == '__main__':
# pytest.main(["-vs", "test_calc.py::TestCalc::test_div"]) # 不需要allure的时候执行, 指定某个测试用例
pytest.main(["--alluredir=result/2", "test_calc.py"]) # 生成allure
# 查看allure用例 allure serve .\result\2\
生成的allure如图所示
任务改写2
1、改造 计算器 测试用例,使用fixture函数获取计算器的实例
2、计算之前打印开始计算,计算之后打印结束计算
3、添加用例日志,并将日志保存到日志文件目录下
4、生成测试报告,展示测试用例的标题,用例步骤,与测试日志,截图附到课程贴下
目录结构
目录解析
datas/calc_list.yaml yaml文件用来保存相关的测试用例 要使用yaml先得安装yaml相关的包 pyyaml
result目录为pytest生成的来存放测试报告的目录
calc.py 为 计算器通用函数的一个类
test_cala2.py 执行pytest测试用例的文件
conftest.py 为所有测试用例执行前都会执行到这个的文件,要有__init__.py文件跟在同目录下
pytest.ini pytest框架的一个设置, 可以设置开启日志
conftest.py 用fixture改写:
import pytest
from .calc import Calculator
@pytest.fixture(scope="class")
def get_cal():
print("====实例化计算器, 开始计算===")
cal = Calculator()
yield cal
print("====计算完成====")
pytest.ini 增加保存的日志
[pytest]
log_cli=true
log_level=NOTSET
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S
addopts = -vs
log_file = ./test.log
log_file_level = info
log_file_format = %(asctime)s %(levelname)s %(message)s
log_file_date_format = %Y-%m-%d %H:%M:%S
test_cala2.py 改写第一部分
import yaml
import allure
import pytest
import logging
logging.basicConfig(level=logging.info)
logger = logging.getLogger()
with open('./datas/calc_list.yaml', 'r', encoding='utf-8') as f:
datas = yaml.safe_load(f)['calc_case']
add_list = datas['add_list'] # 加法测试用例
sub_list = datas['sub_list'] # 减法测试用例
mul_list = datas['mul_list'] # 乘法测试用例
div_list = datas['div_list'] # 除法测试用例
ids = datas['all_ids'] # 全部的标签
print(datas)
@allure.feature("计算器模块")
class TestCalc2:
@allure.story("加法运算")
@pytest.mark.parametrize("a, b, expect", add_list, ids=ids)
def test_add(self, a, b, expect, get_cal):
logger.info('增加加法日志')
with allure.step(f"输入测试用例{a}, {b}, 预期结果为{expect}"):
result = get_cal.add(a, b)
if isinstance(result, float): # 判断浮点数
result = round(result, 2)
assert expect == result
@allure.story("减法运算")
@pytest.mark.parametrize("a, b, expect", sub_list, ids=ids)
def test_sub(self, a, b, expect, get_cal):
with allure.step(f"输入测试用例{a}, {b}, 预期结果为{expect}"):
result = get_cal.sub(a, b)
if isinstance(result, float): # 判断浮点数
result = round(result, 2)
assert expect == result
@allure.story("乘法运算")
@pytest.mark.parametrize("a, b, expect", mul_list, ids=ids)
def test_mul(self, a, b, expect, get_cal):
with allure.step(f"输入测试用例{a}, {b}, 预期结果为{expect}"):
result = get_cal.mul(a, b)
if isinstance(result, float): # 判断浮点数
result = round(result, 2)
assert expect == result
@allure.story("除法运算")
@pytest.mark.parametrize("a, b, expect", div_list, ids=ids)
def test_div(self, a, b, expect, get_cal):
with allure.step(f"输入测试用例{a}, {b}, 预期结果为{expect}"):
result = get_cal.division(a, b)
if isinstance(result, float): # 判断浮点数
result = round(result, 2)
print(result)
assert expect == result
if __name__ == '__main__':
# pytest.main(["-vs", "test_calc2.py::TestCalc2::test_add"]) # 不需要allure的时候执行, 指定某个测试用例
pytest.main(["--alluredir=result/3", "test_calc2.py"]) # 生成allure
# 查看allure用例 allure serve .\result\2\
最后完结。