(一)介绍

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的更多相关文章

  1. 【Pytest】python单元测试框架pytest简介

    1.Pytest介绍 pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高.根据pytest的官方网站介绍 ...

  2. python单元测试框架pytest

    首先祝大家国庆节日快乐,这个假期因为我老婆要考注会,我也跟着天天去图书馆学了几天,学习的感觉还是非常不错的,这是一篇总结. 这篇博客准备讲解一下pytest测试框架,这个框架是当前最流行的python ...

  3. python单元测试框架——pytest

    官网:https://docs.pytest.org/en/latest/ pytest帮你写出更好的程序 1.An example of a simple test:(一个简单的例子),命名为tes ...

  4. python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)

    pytest的setup和teardown函数(曾被一家云计算面试官问到过). pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作.与python自带的unitest ...

  5. Python单元测试框架pytest常用测试报告类型

    先前博客有介绍pytest测试框架的安装及使用,现在来聊聊pytest可以生成哪些测试报告 1.allure测试报告 关于allure报告参见先前的一篇博文:https://www.cnblogs.c ...

  6. Python单元测试框架之pytest 4 -- 断言

    From: https://www.cnblogs.com/fnng/p/4774676.html Python单元测试框架之pytest -- 断言 2015-08-31 23:57 by 虫师, ...

  7. Python单元测试框架之pytest 3 -- fixtures

    From: https://www.cnblogs.com/fnng/p/4769020.html Python单元测试框架之pytest -- fixtures 2015-08-29 13:05 b ...

  8. Python单元测试框架之pytest 2 -- 生成测试报告

    From: https://www.cnblogs.com/fnng/p/4768239.html Python单元测试框架之pytest -- 生成测试报告 2015-08-29 00:40 by ...

  9. python单元测试框架笔记

    目录 单元测试概述 什么是单元测试 单元测试什么进行? 单元测试由谁负责? 单元测试需要注意 单元测试覆盖类型 python 单元测试框架 unittest pytest 测试框架 单元测试概述 什么 ...

  10. [译]PyUnit—Python单元测试框架(1)

    1. 原文及参考资料 原文链接:http://docs.python.org/2/library/unittest.html# 参考文档: http://pyunit.sourceforge.net/ ...

随机推荐

  1. 使用dynamic和MEF实现轻量级的AOP组件 (2)

    转摘 https://www.cnblogs.com/niceWk/archive/2010/07/21/1782092.html 偷梁换柱 上一篇我们初试了DynamicAspect这把小刀,如果你 ...

  2. Vue自定义指令 数据传递

    在项目开发过程中,难免会遇到各种功能需要使用Vue自定义指令--directive 去实现 .关于directive的使用方式这里就不做过多的介绍了,Vue官方文档中说的还是听明白的.今天讲讲在使用V ...

  3. 详解 Discuz 的 PHP经典加密解密函数 authcode

    函数注释: // $string: 明文 或 密文 // $operation:DECODE表示解密,其它表示加密 // $key: 密匙 // $expiry:密文有效期 function auth ...

  4. 共享文件夹下其他文件可以访问但php文件访问不了的原因

    刚开始的问题是在virtualbox里的共享文件夹下的项目运行不了,原因是宝塔下nginx的用户和用户组默认是www 和 www 需要改成www vboxsf(因为自动挂载的目录为/media/sf_ ...

  5. /proc/[pid]/status

    http://man7.org/linux/man-pages/man5/proc.5.html /proc/[pid]/status Provides much of the information ...

  6. Linux系统进入救援模式

    由于现在很多的服务器都是用的RedHat,CentOS也比较多,这里就介绍CentOS6.6的救援模式. 有很多人的linux在用的时候不小心修改了某个权限,导致系统启动不起来,下面我就来为大家解决一 ...

  7. java中CyclicBarrier的使用

    文章目录 CyclicBarrier的方法 CyclicBarrier的使用 java中CyclicBarrier的使用 CyclicBarrier是java 5中引入的线程安全的组件.它有一个bar ...

  8. ERC20 Short Address Attack

    ERC20 Short Address Attack 什么是ERC20 Application Binary Interface(ABI) ERC20 Short Address Attack 开始攻 ...

  9. SpringCloudAlibaba实战教程系列

    一.简介 Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案.此项目包含开发分布式应用服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开 ...

  10. 计算机网络 之 Cisco packet tracer 的安装及汉化

    可以去官网下载最新版本的Cisco packet tracer 免费 汉化包及7.1版本百度云链接:链接: https://pan.baidu.com/s/1XudelgnMu6XysCZ36csl7 ...