Pytest教程软件测试编程技术类

Pytest中文文档-01-安装及起步

2018-11-14  本文已影响309人  韩志超

目录:

安装及起步

Python支持版本: Python 2.6,2.7,3.3,3.4,3.5, Jython, PyPy-2.3
支持的平台: Unix/Posix and Windows
PyPI包名: pytest
依赖项: py, colorama (Windows)
PDF文档: 下载最新版本文档

pytest是一个方便创建简单、可扩展性测试用例的框架。测试用例清晰、易读而无需大量的繁琐代码。你几分钟内便可针对你的应用程序或库开展一个小型单元测试或者复杂功能测试。

安装pytest

  1. 在你的命令行执行以下命令
pip install -U pytest
  1. 检查你是否安装了正确的版本
$ pytest --version
This is pytest version 3.x.y, imported from $PYTHON_PREFIX/lib/python3.6/site-packages/pytest.py

创建你的第一个测试用例

使用简单的4行代码创建一个简单的测试方法:

# test_sample.py文件内容
def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5

就是这样。你可以执行一下这个测试方法:

$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR, inifile:
collected 1 item

test_sample.py F                                                     [100%]

================================= FAILURES =================================
_______________________________ test_answer ________________________________

    def test_answer():
>       assert func(3) == 5
E       assert 4 == 5
E        +  where 4 = func(3)

test_sample.py:5: AssertionError
========================= 1 failed in 0.12 seconds =========================

由于func(3)并不等于5,这次测试返回了一个失败报告。

注意:
你可以使用assert语句来验证你测试用例的期望结果。pytest的高级断言内省机制可以智能地报告展示断言表达式的中间值来避免来源于JUnit的方法中的变量名重复问题。

执行多条测试

pytest会执行当前目录及子目录下所有test_*.py*_test.py格式的文件。一般来说,它遵循标准的测试发现规则

断言指定异常

使用raise可以用于断言相应代码的抛出的指定异常:

# test_sysexit.py文件内容
import pytest
def f():
    raise SystemExit(1)

def test_mytest():
    with pytest.raises(SystemExit):
        f()

使用“安静”模式,执行这个测试方法:

$ pytest -q test_sysexit.py
.                                                                    [100%]
1 passed in 0.12 seconds

使用类来组织测试用例

一旦你需要开发多条测试用例,你可能会想要使用类来组织它们。使用pytest可以很轻松的创建包含多条用例的测试类:

# test_class.py文件内容
class TestClass(object):
    def test_one(self):
        x = "this"
        assert 'h' in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')

pytest可以发现所有遵循Python测试用例发现约定规则的用例,所以它能找到类外以及类中所有以test_开头的方法。测试类无需再继承任何对象。我们只需要简单地通过文件名来运行这个模块。

$ pytest -q test_class.py
.F                                                                   [100%]
================================= FAILURES =================================
____________________________ TestClass.test_two ____________________________

self = <test_class.TestClass object at 0xdeadbeef>

    def test_two(self):
        x = "hello"
>       assert hasattr(x, 'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_class.py:8: AssertionError
1 failed, 1 passed in 0.12 seconds

第一条用例执行成功,第二天用例执行失败。你可以很容易地通过断言中变量的中间值来理解失败的原因。

功能测试中请求使用独立的临时目录

pytest提供了内置fixtures及方法参数来请求任意资源,比如一个独立的临时目录:

# test_tmpdir.py文件内容
def test_needsfiles(tmpdir):
    print (tmpdir)
    assert 0

在测试函数参数中使用tmpdir,pytest将在测试函数调用之前查找并调用fixture工厂方法来创建资源。在测试运行之前,pytest创建一个每个测试唯一的临时目录:

$ pytest -q test_tmpdir.py
F                                                                    [100%]
================================= FAILURES =================================
_____________________________ test_needsfiles ______________________________

tmpdir = local('PYTEST_TMPDIR/test_needsfiles0')

    def test_needsfiles(tmpdir):
        print (tmpdir)
>       assert 0
E       assert 0

test_tmpdir.py:3: AssertionError
--------------------------- Captured stdout call ---------------------------
PYTEST_TMPDIR/test_needsfiles0
1 failed in 0.12 seconds

有关tmpdir处理的更多信息,请参见: 临时目录和文件

进一步阅读

查看其他pytest文档资源,来帮助你建立自定义测试用例及独特的工作流:

上一篇 下一篇

猜你喜欢

热点阅读