开头

经过前面几章的学习,这时候要来个测试实战会比较好巩固一下学过的知识

任务要求

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\

最后完结。

pytest测试实战和练习的更多相关文章

  1. 关于《精通移动App测试实战:技术、工具和案例》图书勘误信息

    首先,对由于我们工作的疏忽向<精通移动App测试实战:技术.工具和案例>读者朋友们表示歉意,同时已将这些问题反馈给了出版社编辑同志,再版时将会统一修正: 其次,勘误信息请参看附件pdf文档 ...

  2. Jmeter分布式测试实战

    一.Jmeter分布式测试基础 1.Jmeter分布式测试原因: 在使用Jmeter进行接口的性能测试时,由于Jmeter 是JAVA应用,对负载机的CPU和内存消耗比较大.所以当需要模拟数以万计的并 ...

  3. Pytest 测试框架

    一 . Pytest 简介 Pytest是python的一种单元测试框架. 1. pytest 特点 入门简单,文档丰富 支持单元测试,功能测试 支持参数化,重复执行,部分执行,测试跳过 兼容其他测试 ...

  4. 《Web安全攻防 渗透测试实战指南 》 学习笔记 (五)

    Web安全攻防 渗透测试实战指南   学习笔记 (五)   第四章 Web安全原理解析  (一) (一)SQL注入的原理 1.web应用程序对用户输入数据的合法性没有判断. 2.参数用户可控:前端传给 ...

  5. 《Web安全攻防 渗透测试实战指南》 学习笔记 (四)

    Web安全攻防 渗透测试实战指南   学习笔记 (四) Nmap                                       Network  Mapper    是一款开放源代码的网 ...

  6. 《Web安全攻防 渗透测试实战指南 》 学习笔记 (三)

    Web安全攻防 渗透测试实战指南   学习笔记 (三) burp suite详解                                                 是一款集成化渗透测试工 ...

  7. 《Web安全攻防 渗透测试实战指南》 学习笔记 (二)

    Web安全攻防 渗透测试实战指南   学习笔记 (二)   第二章  漏洞环境及实践  

  8. 《Web安全攻防 渗透测试实战指南》 学习笔记(一)

    Web安全攻防 渗透测试实战指南   学习笔记 (一) 第一章   信息收集     在信息收集中,最重要是收集服务器的配置信息和网站敏感信息(域名及子域名信息目标网站系统.CMS指纹.目标网站真实I ...

  9. pytest测试框架 -- 简介

    一.pytest测试框架简介: (1)pytest是python的第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效. (2)pytest框架可以兼容unittest用 ...

  10. pytest测试框架入门

    安装pytest 命令行输入: pip install -U pytest 检查是否安装了正确的版本: λ pytest --version This is pytest version 5.3.5, ...

随机推荐

  1. 【StoneDB 模块介绍】服务器模块

    [StoneDB 模块介绍]服务器模块 一.介绍 客户端程序和服务器程序本质上都是计算机上的一个进程,客户端进程向服务器进程发送请求的过程本质上是一种进程间通信的过程,StoneDB 数据库服务程序作 ...

  2. 《MySQL是怎样运行的》第三章小结

  3. AttributeError: module 'torch._six' has no attribute 'PY3'

    修改:进到torch._six源码,看看里面是PY的哪个对象,修改成这对象名试试 _six.py 即将PY3修改为PY37

  4. 磁盘IO 基本常识

    计算机硬件性能在过去十年间的发展普遍遵循摩尔定律,通用计算机的 CPU主频早已超过3GHz,内存也进入了普及DDR4的时代.然而传统硬盘虽然在存储容量上增长迅速,但是在读写性能上并无明显提升,同时SS ...

  5. 记一次在forEach中使用aynac/await中的坑

    1.背景 在写一个对齐脚本时 发现下列问题 const timeList = await imageList.map( (item,index)=>{ return item.identify_ ...

  6. SwitchHosts operation not permitted 解决方案--亲测有效

    SwitchHost!是帮助我们管理Hosts的工具,可以帮助我们做域名解析, 弥补了如果要修改域名还要改计算机C:\Windows\System32\drivers\etc位置下的hosts文件的弊 ...

  7. C++库封装JNI接口——实现java调用c++

    1. JNI原理概述 通常为了更加灵活高效地实现计算逻辑,我们一般使用C/C++实现,编译为动态库,并为其设置C接口和C++接口.用C++实现的一个库其实是一个或多个类的简单编译链接产物.然后暴露其实 ...

  8. vue之箭头函数

    目录 说明 解决方法一 重新定义this 解决方法二 使用箭头函数 无参数的箭头函数 有一个参数的箭头函数 有两个参数的箭头函数 有一个参数一个返回值的箭头函数 说明 当在一个方法(函数)里面再定义一 ...

  9. flask-wtfwkfom使用

    我们在使用flask框架来搭建自己的博客,只要是设涉及到表单相关,必然会想起Flask-WTF与WTForms.对于flask初学者来说,比较容易混淆两者.今天想来一一解释两者的用法. Flask-W ...

  10. [NotePad++]NotePad++实用技巧

    2 应用技巧 2.1 匹配并捕获/截取 截取第1列的数据 截取前 "(.*)", "(.*)", "(.*)"\)\); 截取后: 2.2 ...