(一)介绍

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

最新文章

  1. javaweb学习总结(三十八)——事务
  2. HttpWebRequest出错 服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 LF
  3. ubuntu打不开图形界面,显示run in low_graphic mode
  4. C++ set容器简单用法
  5. LinearLayout 控件
  6. 【Search Insert Position 】cpp
  7. 【阿里云产品公测】简单日志服务SLS使用评测 + 教程
  8. Directx3D SimpleSample Sample
  9. JS定位PDF页码。
  10. SQLite学习心得
  11. 《JavaScript高级程序设计》笔记(3):传递参数
  12. Swagger 生成 ASP.NET Web API
  13. Android 通过wifi调试程序【转】
  14. ELK采集之nginx 之高德地图出城市IP分布图
  15. ArcGIS API for JavaScript 4.2学习笔记[4] 第二章其余感兴趣的例子
  16. JS 部分常见循环、分支、嵌套练习
  17. Lua5.3 注册表 _G _ENV
  18. 完成 bass 库的频谱显示效果图
  19. 视频播放flv player的使用
  20. WCF开发实战系列三:自运行WCF服务

热门文章

  1. gridview 合并单元格后,选中颜色重新绘制
  2. Python大数据与机器学习之NumPy初体验
  3. Linux-LAMP虚拟主机配置
  4. SVM之不一样的视角
  5. jQuery的attr和prop属性
  6. HTML之前端组成、标签
  7. python在linux调用shell脚本实时打印输出信息并对信息进行判断
  8. ES6新增的 Set 和 WeakSet 是什么玩意?在此揭晓
  9. js 运动函数篇(二) (加速度运动、弹性运动、重力场运动(多方向+碰撞检测+重力加速度+能量损失运动)拖拽运动)层层深入
  10. JS实现元素的全屏、退出全屏功能