Python单元测试框架:pytest
(一)介绍
pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点:
1、简单灵活,容易上手;
2、支持参数化;
3、能够支持简单的单元测试和复杂的功能测试,还可以用来做selenium/appnium等自动化测试、接口自动化测试(pytest+requests);
4、pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)、pytest-rerunfailures(失败case重复执行)、pytest-xdist(多CPU分发)等;
5、测试用例的skip和xfail处理;
6、可以很好的和jenkins集成;
(二)安装
pip install -U pytest # 通过pip安装
pip install -U pytest-html
pip install -U pytest-rerunfailures
py.test --version # 查看pytest版本
This is pytest version 2.7.2, imported from C:\Python27\lib\site-packages\pytest.pyc
此外还有很多很好的第三方插件,请到http://plugincompat.herokuapp.com/ 和 https://pypi.python.org/pypi?%3Aaction=search&term=pytest-&submit=search 查找
(三)例子
这里列几个pytest-document中的例子
1、默认执行当前目录下的所有以test_为前缀(test_*.py)或以_test为后缀(*_test.py)的文件中以test_为前缀的函数
import pytest # content of test_sample.py
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
运行 py.test 或 指定特定文件 py.test -q test_sample.py

2、使用类来组成多个用例的
import pytest # content of test_class.py
class TestClass:
def test_one(self): x = "this"
assert 'h' in x
def test_two(self):
x = "hello"
assert hasattr(x, 'check')

3、在python中调用pytest: python test_class.py
import pytest # content of test_class.py
class TestClass:
def test_one(self):
print 'one'
x = "this"
assert 'h' in x
def test_two(self):
print 'two'
x = "hello"
assert hasattr(x, 'check')
if __name__ == '__main__':
pytest.main("-q --html=a.html")

4、来个支持参数化的例子,参数化使用pytest.mark.parametrize的参数,第一个为变量的元组,第二个是变量赋值的元组列表,具体下面的章节会仔细介绍
# content of test_time.py
import pytest
from datetime import datetime, timedelta testdata = [
(datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1)),
(datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1)),
] @pytest.mark.parametrize("a,b,expected", testdata)
def test_timedistance_v0(a, b, expected):
diff = a - b
assert diff == expected @pytest.mark.parametrize("a,b,expected", testdata, ids=["forward", "backward"])
def test_timedistance_v1(a, b, expected): diff = a - b
assert diff == expected def idfn(val):
if isinstance(val, (datetime,)):
# note this wouldn't show any hours/minutes/seconds
return val.strftime('%Y%m%d')
@pytest.mark.parametrize("a,b,expected", testdata, ids=idfn)
def test_timedistance_v2(a, b, expected):
diff = a - b
assert diff == expected

Python单元测试框架:pytest的更多相关文章
- 【Pytest】python单元测试框架pytest简介
1.Pytest介绍 pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高.根据pytest的官方网站介绍 ...
- python单元测试框架pytest
首先祝大家国庆节日快乐,这个假期因为我老婆要考注会,我也跟着天天去图书馆学了几天,学习的感觉还是非常不错的,这是一篇总结. 这篇博客准备讲解一下pytest测试框架,这个框架是当前最流行的python ...
- python单元测试框架——pytest
官网:https://docs.pytest.org/en/latest/ pytest帮你写出更好的程序 1.An example of a simple test:(一个简单的例子),命名为tes ...
- python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)
pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...
- Python单元测试框架pytest常用测试报告类型
先前博客有介绍pytest测试框架的安装及使用,现在来聊聊pytest可以生成哪些测试报告 1.allure测试报告 关于allure报告参见先前的一篇博文:https://www.cnblogs.c ...
- Python单元测试框架之pytest 4 -- 断言
From: https://www.cnblogs.com/fnng/p/4774676.html Python单元测试框架之pytest -- 断言 2015-08-31 23:57 by 虫师, ...
- Python单元测试框架之pytest 3 -- fixtures
From: https://www.cnblogs.com/fnng/p/4769020.html Python单元测试框架之pytest -- fixtures 2015-08-29 13:05 b ...
- Python单元测试框架之pytest 2 -- 生成测试报告
From: https://www.cnblogs.com/fnng/p/4768239.html Python单元测试框架之pytest -- 生成测试报告 2015-08-29 00:40 by ...
- python单元测试框架笔记
目录 单元测试概述 什么是单元测试 单元测试什么进行? 单元测试由谁负责? 单元测试需要注意 单元测试覆盖类型 python 单元测试框架 unittest pytest 测试框架 单元测试概述 什么 ...
- [译]PyUnit—Python单元测试框架(1)
1. 原文及参考资料 原文链接:http://docs.python.org/2/library/unittest.html# 参考文档: http://pyunit.sourceforge.net/ ...
随机推荐
- Python 中取代 Printf 大法的工具
「printf 大法」大概是最早期学到的 debug 方式?不同语言有不同的指令,在 Python 里对应的是 print指令 (加上%或是.format). 刚刚看到「 cool-RR/pysnoo ...
- 蒲公英 · JELLY技术周刊 Vol.03
蒲公英 · JELLY技术周刊 Vol.03 「蒲公英」期刊全新升级--JELLY技术周刊!深度挖掘业界热点动态,来自团队大咖的专业点评,带你深入了解团队研究的技术方向. 登高远眺 天高地迥,觉宇宙之 ...
- 深入理解BIO、NIO、AIO
导读:本文你将获取到:同/异步 + 阻/非阻塞的性能区别:BIO.NIO.AIO 的区别:理解和实现 NIO 操作 Socket 时的多路复用:同时掌握 IO 最底层最核心的操作技巧. BIO.NIO ...
- 一,连接Oracle 一
连接Oracle数据库方法: 一,使用sqlplus连接 二,使用第三方软件连接 sqlplus sqlplus 工具简介 (1).概述:sqlplus是在Linux下操作oracle的工具 (2). ...
- 2019-2020-1 20199328《Linux内核原理与分析》第五周作业
实验要求: 实验步骤: 这里以20号系统调用getpid为例进行实验,该函数的功能为:返回当前进程标识. getpid.c代码: 查看实验结果: 当前进程pid为:31042. 在C语言中编入汇编代码 ...
- 数据挖掘入门系列教程(十二)之使用keras构建CNN网络识别CIFAR10
简介 在上一篇博客:数据挖掘入门系列教程(十一点五)之CNN网络介绍中,介绍了CNN的工作原理和工作流程,在这一篇博客,将具体的使用代码来说明如何使用keras构建一个CNN网络来对CIFAR-10数 ...
- java之重载与重写
重写(override) 重写是子类对父类的允许访问的方法的重新编写,方法名,返回值类型和形参列表都不能改变,唯一恩能够改变的是方法体. 重写的好处是可以根据子类的需要的行为来实现父类的方法. 重写方 ...
- Linux指令面试题01-进程查看与终止
查看某一进程是否运行:ps -ef|grep 程序名 终止程序: kill pid 转载于:https://www.cnblogs.com/feihujiushiwo/p/10896636.html
- JAVA第一次blog总结
JAVA第一次blog总结 0.前言 大一下学期我们开展了OPP这门课程,这也是我们第一次接触到JAVA.与上学期我们在学校里学C语言不同的是,这学期由于疫情原因我们是以网课的方式在学习.在学习中我发 ...
- centos6.5宽带拨号上网
CentOS6以后要安装rp-pppoe这个软件,centos之前的版本是adsl-setup命令安装. (1)查看是否安装 #rpm -qa|grep rp-pppoe 没有内容输出则没安装,若可以 ...