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/ ...
随机推荐
- 全球数字高程数据(DEM)详解,还有地形晕渲、等高线等干货
1 基本概念 DEM是数字高程模型的英文简称(Digital Elevation Model),是研究分析地形.流域.地物识别的重要原始资料.由于DEM 数据能够反映一定分辨率的局部地形特征,因此通过 ...
- XSS Cheat Sheet(basics and advanced)
XSS Cheat Sheet BASICS HTML注入 当输入位于HTML标记的属性值内或标记的外部(下一种情况中描述的标记除外)时使用.如果输入在HTML注释中,则在payload前加上&quo ...
- java学习(第二篇)语法学习
1.java标识符 类名.变量名以及方法名都被称为标识符. 关于 Java 标识符,有以下几点需要注意: 所有的标识符都应该以字母(A-Z 或者 a-z),美元符($).或者下划线(_)开始 首字符之 ...
- QString 转换成 wchar 的一个小陷阱
QString::toWCharArray(wchar_t * array) 其中 wchar_t * array 除了要分配内存之外,必须用 wmemset 初始化. 环境是 Visual Stud ...
- 反向代理负载均衡之haproxy
在上篇安装的nginx的机器环境上将nginx停掉 /usr/local/nginx/sbin/nginx -s stop 在linux-node2上编译安装haproxy作为反向代理服务器 [roo ...
- Swoole 实战:MySQL 查询器的实现(协程连接池版)
目录 需求分析 使用示例 模块设计 UML 类图 入口 事务 连接池 连接 查询器的组装 总结 需求分析 本篇我们将通过 Swoole 实现一个自带连接池的 MySQL 查询器: 支持通过链式调用构造 ...
- Linux开发初探
坚持用了十几天的Linux操作系统,学会了很多的东西,但现在必须得抉择如何选择开发工具.在这些天的开发中,各种Linux下的IDE都有 所尝试.一向看好的Code::Blocks还是过于简单,用了一阵 ...
- 老男孩Linux运维50期 --于海科--决心书
1.我叫于海科,来自于甘肃省天水市,之前就读于兰州石化职业技术学院,我是听之前的学长说老男孩教育出来就业不错,我特此来这培训希望出来能够找到一份不错的工作.2.五个月学完,目标薪资是11k.3.达到目 ...
- MySQL数据库的套接字文件和pid文件
MySQL数据库的套接字文件和pid文件 socket文件:当用Unix域套接字方式进行连接时需要的文件. pid文件:MySQL实例的进程ID文件. MySQL表结构文件:用来存放MySQL表结构定 ...
- KAFKA官方教程笔记-introduction
为什么80%的码农都做不了架构师?>>> 介绍 apache kafka是一个分布式流式处理平台,一个流式平台该有的三个关键能力: 发布.订阅流式数据.从这个角度讲类似消息队列或 ...