python单元测试之pytest的使用


Posted in Python onJune 07, 2021

一、前提准备

1、前提:需要安装pytest和pytest-html(生成html测试报告)

pip install pytest 和 pip install pytest-html 

安装插件:pip install 插件名

2、命名规范

 Pytest单元测试中的类名和方法名必须是以test开头,执行中只能找到test开头的类和方法,比unittest更加严谨

Pytest: setup, setup_class 和 teardown, teardown_class 函数 ( 和 unittest 执行效果一样 ) 运行于测试方法的始末,即 : 运行一次测试函数会运行一次 setup 和 teardown 运行于测试方法的始末 , 但是不管有多少测试函数都只执行一次 setup_class 和 teardown_class

二、pytest生成自带的html测试报告

前提条件:需要下载pytest-html模块(python自带的生成测试报告模块)

pip install pytest-html

如果不安装pytest-html会报:

python单元测试之pytest的使用

案例: 1)

pytest.main("模块.py")【运行指定模块下,运行所有test开头的类和测试用例】 

 pytest.main(["--html=./report.html","模块.py"])

import pytest
class Test():
    def test1(self):
        print("这是测试1")
    def test1(self):
        print("这是测试2")
if __name__ == '__main__':
    pytest.main(["--html=./report.html", "test_004.py"])

结果:

python单元测试之pytest的使用

python单元测试之pytest的使用

2)运行指定模块指定类指定用例,冒号分割,并生成测试报告

pytest.main([‘--html=./report.html',‘模块.py::类::test_a_001'])

import pytest
class Test():
    def test1(self):
        print("这是测试1")
    def test2(self):
        print("这是测试2")
if __name__ == '__main__':
    pytest.main(["--html=./report.html", "test_004.py::Test::test1"])

结果:

python单元测试之pytest的使用

3)直接执行pytest.main() 【自动查找当前目录下,以test 开头的文件或者以test结尾的py文件】

pytest.main([‘--html=./report.html'])

语句: pytst.main(['-x','--html=./report.html','t12est000.py'])

-x出现一条测试用例失败就退出测试
-s:显示print内容

三、pytest运行方式

. 点号,表示用例通过
F 表示失败 Failure
E 表示用例中存在异常 Error

四、allure  

Allure是一款轻量级并且非常灵活的开源测试报告框架。 它支持绝大多数测试框架, 例如TestNG、Pytest、JUint等。它简单易用,易于集成  

1、Allure常用的几个特性

@allure.feature # 用于描述被测试产品需求

@allure.story # 用于描述 feature 的用户场景,即测试需求

with allure.step (): # 用于描述测试步骤,将会输出到报告中

allure.attach # 用于向测试报告中输入一些附加的信息,通常是一些测试数据,截图等

案例1:关于pytest与Allure生成html测试用例 rr.csv

2,3,5
5,6,11

readCsv

import csv  # 导入csv模块
 
 
class ReadCsv():
    def read_csv(self):
        item = []  # 定义一个空列表
        c = csv.reader(open("../dataDemo/rr.csv", "r"))  # 得到csv文件对象
        for csv_i in c:
            item.append(csv_i)  # 将获取的数据添加到列表中
        return item
 
 
r = ReadCsv()
print(r.read_csv())

开发代码:

class Cale():
    def jia(self,a,b):
        c=a+b
        return c
    def jian(self,a,b):
        c=a-b
        return c
    def cheng(self,a,b):
        c=a*b
        return c
    def chu(self,a,b):
        c=a/b
        return c

生成html代码:

import pytest
from pytest01.readDemo.readCsv import ReadCsv
from pytest01.demo.cale import Cale
import os
import allure
r=ReadCsv()
cc=r.read_csv()
d=Cale()
class Test():
    @allure.story("加法函数测试正确")
    def test001(self):
        for i in cc:
            dd=d.jia(int(i[0]),int(i[1]))
            assert dd==int(i[2])
if __name__ == '__main__':
    pytest.main(['--alluredir', 'report/result', 'test_02.py'])
    split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
    os.system(split)

python单元测试之pytest的使用

python单元测试之pytest的使用

到此这篇关于python单元测试之pytest的使用的文章就介绍到这了,更多相关pytest的使用内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python利用elaphe制作二维条形码实现代码
May 25 Python
跟老齐学Python之从格式化表达式到方法
Sep 28 Python
使用django-crontab实现定时任务的示例
Feb 26 Python
使用python读取.text文件特定行的数据方法
Jan 28 Python
python原类、类的创建过程与方法详解
Jul 19 Python
python基于pdfminer库提取pdf文字代码实例
Aug 15 Python
Pycharm最新激活码2019(推荐)
Dec 31 Python
pycharm运行程序时看不到任何结果显示的解决
Feb 21 Python
Python脚本去除文件的只读性操作
Mar 05 Python
Jupyter notebook命令和编辑模式常用快捷键汇总
Nov 17 Python
Python使用Opencv实现边缘检测以及轮廓检测的实现
Dec 31 Python
Python基本的内置数据类型及使用方法
Apr 13 Python
Python趣味实战之手把手教你实现举牌小人生成器
怎么用Python识别手势数字
利用python调用摄像头的实例分析
Jun 07 #Python
python通过opencv调用摄像头操作实例分析
Jun 07 #Python
Python爬虫之用Xpath获取关键标签实现自动评论盖楼抽奖(二)
Jun 07 #Python
还在手动盖楼抽奖?教你用Python实现自动评论盖楼抽奖(一)
Jun 07 #Python
Pytorch中Softmax和LogSoftmax的使用详解
Jun 05 #Python
You might like
类的另类用法--数据的封装
2006/10/09 PHP
10 个经典PHP函数
2013/10/17 PHP
php类中的各种拦截器用法分析
2014/11/03 PHP
php图片添加水印例子
2016/07/20 PHP
php 截取中英文混合字符串的方法
2018/05/31 PHP
遍历DOM对象内的元素属性示例代码
2014/02/08 Javascript
JavaScript中实现继承的三种方式和实例
2015/01/29 Javascript
JavaScript数据类型学习笔记分享
2016/09/01 Javascript
BootStrap glyphicon图标无法显示的解决方法
2016/09/06 Javascript
JavaScript ES6中CLASS的使用详解
2016/11/22 Javascript
完美解决node.js中使用https请求报CERT_UNTRUSTED的问题
2017/01/08 Javascript
canvas实现图像截取功能
2017/02/06 Javascript
详解数组Array.sort()排序的方法
2020/05/09 Javascript
js+canvas实现动态吃豆人效果
2017/03/22 Javascript
jQuery+HTML5实现WebGL高性能烟花绽放动画效果【附demo源码下载】
2017/08/18 jQuery
React教程之Props验证的具体用法(Props Validation)
2017/09/04 Javascript
jQuery Dom元素操作技巧
2018/02/04 jQuery
VUE实现可随意拖动的弹窗组件
2018/09/25 Javascript
Nuxt.js实现一个SSR的前端博客的示例代码
2019/09/06 Javascript
Python入门篇之列表和元组
2014/10/17 Python
Python中的两个内置模块介绍
2015/04/05 Python
python爬取微信公众号文章
2018/08/31 Python
python动态进度条的实现代码
2019/07/03 Python
python函数局部变量、全局变量、递归知识点总结
2019/11/15 Python
Python3 pickle对象串行化代码实例解析
2020/03/23 Python
Kmeans均值聚类算法原理以及Python如何实现
2020/09/26 Python
python time()的实例用法
2020/11/03 Python
美国最大的万圣节服装网站:HalloweenCostumes.com
2017/10/12 全球购物
安全教育实施方案
2014/03/02 职场文书
微笑服务标语
2014/06/24 职场文书
领导干部作风整顿个人剖析材料
2014/10/11 职场文书
2014年社区综治工作总结
2014/11/17 职场文书
2015大学生党员自我评价范文
2015/03/03 职场文书
煤矿百日安全活动总结
2015/05/07 职场文书
Python激活Anaconda环境变量的详细步骤
2021/06/08 Python
nginx rewrite功能使用场景分析
2022/05/30 Servers