Pytest中文文档(1)——安装与入门

安装与入门

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), argparse (py26), ordereddict (py26).

PDF文档: 下载最新版本文档

安装

安装:

pip install -U pytest

检查您安装的版本是否正确:

$ pytest --version
This is pytest version 3.x.y, imported from $PYTHON_PREFIX/lib/python3.5/site-packages/pytest.py

我们的第一个测试

让我们使用一个简单的测试函数创建一个测试文件:

# 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

======= 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.文件。

断言引起某种异常

如果你想断言某些代码引起的异常,可以使用raises助手:

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

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

这次使用"quite"报告模式来运行:

$ pytest -q test_sysexit.py
.
1 passed in 0.12 seconds

在class中将多个测试进行分组

一旦您开始有了更多的测试时,在逻辑上,在类和模块中进行分组测试是有意义的。让我们写一个包含两个测试的类:

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

这两个test被发现是因为这个标准—— Python测试发现机制约定. 不需要子类化任何东西。我们可以简单地通过传递它的文件名来运行这个模块:

$ pytest -q test_class.py
.F
======= 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

第一个test通过了,第二个test失败了。同样,我们可以很容易地看到断言中使用的中间值,有助于我们理解失败的原因。

功能:请求一个唯一的临时目录

对于功能测试,通常需要创建一些文件并将它们传递给应用程序对象. pytest提供 内置的 fixtures/function 参数 允许任意请求的资源,例如一个独特的临时目录:

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

我们在测试函数签名中列出了tmpdir,在执行测试函数调用之前,pytest将查找并调用一个fixture工厂来创建资源。让我们来运行它:

$ pytest -q test_tmpdir.py
F
======= 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

在test运行之前,创建了一个惟一的测试调用临时目录.

输入以下内容可以查看内置的pytest fixtures:明确的、模块化的、可伸缩的:

pytest --fixtures # shows builtin and custom fixtures

下一步去哪

下面是一些建议:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,246评论 19 139
  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    aimaile阅读 26,634评论 6 427
  • GitHub 上有一个 Awesome - XXX 系列的资源整理,资源非常丰富,涉及面非常广。awesome-p...
    若与阅读 18,901评论 4 418
  • Python 四五事 介绍 Python 相关工具,工作流程和测试框架。 发布于 2014.1.19最后更新 20...
    hzyido阅读 65,071评论 0 4
  • 我曾踏月而来 只因你在山中 山风拂发 拂颈 拂裸露的肩膀 而月光衣我以华裳 月光衣我以华裳 林间有新绿似我青春模样...
    露珠拾遗阅读 691评论 0 0