pytest学习1:Installation and Getting Started

Python :python 2.7、3.4、3.5、3.6、3.7、jython、pypy-2.3

平台 :unix/posix和windows

PYPI包名称pytest

PDF文档download latest

pytest 是一个使构建简单和可伸缩的测试变得容易的框架。测试具有表达性和可读性,不需要样板代码。几分钟后就可以开始对应用程序或库进行小的单元测试或复杂的功能测试。

安装 pytest

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

创建第一个测试

创建一个简单的测试函数:

# content of 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-4.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
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 .

运行多个测试

pytest 将在当前目录及其子目录中,运行所有这种形式的文件:test _*.py 或者 * _test.py。

断言引发了某个异常

使用 raises 断言某些代码引发异常:

# content of test_sysexit.py
import pytest
def f():
    raise SystemExit(1)

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

以“ quiet ”报告模式执行测试功能:

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

将一个类中的多个测试分组

一旦开发了多个测试任务,可能希望将它们分组到一个类中。pytest很容易创建包含多个测试的类:

# content of 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 会发现所有带 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 提供了 Builtin fixtures/function arguments 请求任意资源,如唯一的临时目录:

# content of test_tmpdir.py
def test_needsfiles(tmpdir):
    print(tmpdir)
    assert 0

在测试函数签名中列出 tmpdir 名字, pytest 将在执行测试函数调用之前查找并调用一个fixture工厂以创建资源。在测试运行之前, pytest 创建一个unique-per-test-invocation(唯一的每个测试调用的)临时目录:

$ 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

找出其他的内置 pytest fixtures 使用以下命令:

pytest --fixtures   # shows builtin and custom fixtures

注意,这个命令省略了前导的fixtures _ 除非 -v 选项已添加。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值