python pytest_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

937114-20160707150538421-1959138879.png

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')

937114-20160707151817467-228763056.png

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")

937114-20160707154217717-243962682.png

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

937114-20160707154937046-493405114.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值