恭喜发财dife 发表于 2018-4-18 14:04:35

Pytest 测试框架入门

1、 Pytest 介绍

pytest 是 python 的一种单元测试框架,与 python 自带的 unittest 测试框架类似,但是比 unittest 框架使用
起来更简洁,效率更高。根据 pytest 的官方网站介绍,它具有如下特点:

非常容易上手,入门简单,文档丰富,文档中有很多实例可以参考

能够支持简单的单元测试和复杂的功能测试

支持参数化

执行测试过程中可以将某些测试跳过,或者对某些预期失败的 case 标记成失败

支持重复执行失败的 case

支持运行由 nose , unittest 编写的测试 case

具有很多第三方插件,并且可以自定义扩展

方便的和持续集成工具集成

由于网上 pytest 的中文文档比较少,自己学习过程中,基本上看的就是英文的官方文档,对于不想看英文的
同学们,本系列文章希望能够帮大家一马。

2、安装 pytest

与安装其他的 python 软件无异,直接使用 pip 安装。

pip install - U pytest
安装完成后,可以验证安装的版本:

py.test - -version
3、一个实例

我们可以通过下面的实例,看看使用 py.test 进行测试是多么简单。

## content of test_sample.pydef func(x):
    return x + 1ef test_func():
    assert func(3) == 5
这里我们定义了一个被测试函数 func ,该函数将传递进来的参数加1后返回。我们还定义了一个测试函数 tes
t_func 用来对 func 进行测试。 test_func 中我们使用基本的断言语句 assert 来对结果进行验证。

下面来运行这个测试:

$ py.test
== == == == == == == == == == == == == = test session starts == == == == == == == == == == == == == ==
platform linux - - Python 3.4.1 - - py - 1.4.27 - - pytest - 2.7.1rootdir:
    /tmp / doc - exec - 101, inifile:
collected 1 items
test_sample.py F
== == == == == == == == == == == == == == == == = FAILURES == == == == == == == == == == == == == == == == =
_______________________________ test_answer ________________________________def test_ans
wer():> assert func(3) == 5E assert 4 == 5E + where 4 = func(3)
test_sample.py:    5:
      AssertionError
== == == == == == == == == == == == = 1 failed in 0.01 seconds == == == == == == == == == == == == =
执行测试的时候,我们只需要在测试文件 test_sample 所在的目录下,运行 py.test 即可。 pytest 会在当前的
目录下,寻找以 test 开头的文件(即测试文件),找到测试文件之后,进入到测试文件中寻找 test_ 开头的测
试函数并执行。

通过上面的测试输出,我们可以看到该测试过程中,一个收集到了一个测试函数,测试结果是失败的(标记为
F ),并且在 FAILURES 部分输出了详细的错误信息,帮助我们分析测试原因,我们可以看到” assert
func(3) == 5”这条语句出错了,错误的原因是 func(3)=4,然后我们断言 func(3) 等于 5。

4、再一个实例

当需要编写多个测试样例的时候,我们可以将其放到一个测试类当中,如:

## content of test_class.pyclass TestClass:

    def test_one(self):
      x = "this"
      assert 'h' in x      def test_two(self):
      x = "hello"
      assert hasattr(x, 'check')
我们可以通过执行测试文件的方法,执行上面的测试:

$ py.test - q test_class.py
.F
== == == == == == == == == == == == == == == == = FAILURES == == == == == == == == =
= == == == == == == == =
____________________________ TestClass.test_two ____________________________
self = <test_class.TestClass object at 0x7fbf54cf5668 >def test_two(self):x = "hello"> assert hasattr(x, 'check')
E assert hasattr('hello', 'check')
test_class.py:    8:
      AssertionError1 failed, 1 passed in 0.01 seconds
从测试结果中可以看到,该测试共执行了两个测试样例,一个失败一个成功。同样,我们也看到失败样例的详
细信息,和执行过程中的中间结果。

5、如何编写 pytest 测试样例

通过上面2个实例,我们发现编写 pytest 测试样例非常简单,只需要按照下面的规则:

测试文件以 test_ 开头(以 _test 结尾也可以)

测试类以 Test 开头,并且不能带有 __init__ 方法

测试函数以 test_ 开头

断言使用基本的 assert 即可

6、如何执行 pytest 测试样例

执行测试样例的方法很多种,上面第一个实例是直接执行 py.test ,第二个实例是传递了测试文件给 py.test 。
其实 py.test 有好多种方法执行测试:

py.test               ## run all tests below current dirpy.test test_mod.py   # run tests in modulepy.test somepat
h      # run all tests below somepathpy.test - k stringexpr# only run tests with names that match the# the "s
tring expression", e.g. "MyClass and not method"# will select TestMyClass.test_something# but not TestMyCl
ass.test_method_simplepy.test test_mod.py:
    :
      test_func# only run tests that match the "node ID",
      # e.g "test_mod.py::test_func" will select
      # only test_func in test_mod.py
7、测试报告

pytest 可以方便的生成测试报告,即可以生成 HTML 的测试报告,也可以生成 XML 格式的测试报告用来与持
续集成工具集成。

生成 HTML 格式报告:

py.test - -resultlog = path
生成 XML 格式的报告:

py.test - -junitxml = path
8、如何获取帮助信息

py.test - -version# shows where pytest was imported frompy.test - -fixtures# show available builtin functio
n argumentspy.test - h | --help# show help on command line and config file options
9、最佳实践

其实对于测试而言,特别是在持续集成环境中,我们的所有测试最好是在虚拟环境中。这样不同的虚拟环境中
的测试不会相互干扰的。

由于我们的实际工作中,在同一个 Jekins 中,运行了好多种不同项目册的测试,因此,各个测试项目运行在各
自的虚拟环境中。

将 pytest 安装在虚拟环境中:

1、将当前目录创建为虚拟环境

virtualenv .      # create a virtualenv directory in the current directorysource bin/activate# on unix
2、在虚拟环境中安装 pytest :

pip install pytest


libingyu135 发表于 2018-4-19 17:41:15

腻害了,多谢分享

梦想家 发表于 2018-5-5 14:03:25

:victory:

梦想家 发表于 2018-5-5 14:03:33

:victory::victory:

Miss_love 发表于 2018-5-8 13:33:47

感谢分享

梦想家 发表于 2018-5-8 13:43:56

:victory:
页: [1]
查看完整版本: Pytest 测试框架入门