设为首页 加入收藏

TOP

1、pytest中文文档--安装和入门(一)
2019-09-14 00:55:44 】 浏览:95
Tags:pytest 中文 文档 安装 入门

安装和入门

  • Python版本:Python 3.5, 3.6, 3.7, PyPy3
  • 平台: Linux或者Windows
  • PyPI包:pytest

pytest是一个测试框架,它能够简化测试系统的构建,和规模化测试的组织。测试系统将会变得更具有表现力和可读性———不再需要通过阅读模版代码来了解系统。只需要几分钟的时间,就可以开始对你的应用做一个简单的单元测试或者复杂的功能测试。

安装pytest

  1. 在你的命令行中执行如下命令:

     pip install -U pytest
  2. 检查pytest的版本信息:

     $ pytest --version
     This is pytest version 5.1.2, imported from $PYTHON_PREFIX/lib/python3.7/site-packages/pytest.py

创建你的第一个测试用例

创建一个简单的测试用例,它只有四行代码:

# test_sample.py

def func(x):
    return x + 1


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

现在你可以执行这个测试用例:

/d/Personal Files/Python/pytest-chinese-doc/src (5.1.2)
λ pytest test_sample.py
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\Personal Files\Python\pytest-chinese-doc\src
collected 1 item

test_sample.py F                                                                                                 [100%]

====================================================== FAILURES =======================================================
_____________________________________________________ test_sample _____________________________________________________

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

test_sample.py:28: AssertionError
================================================== 1 failed in 0.07s ==================================================

pytest返回一个失败的测试报告,因为func(3)不等于5

执行多个测试用例

在当前文件夹和子文件夹中,pytest会执行所有符合test_*.py*_test.py命名规则的文件。一般来说,其遵循标准的测试发现规则

检查代码是否触发一个指定的异常

使用raises可以检查代码是否抛出一个指定的异常:

# test_sysexit.py

import pytest


def f():
    # 解释器请求退出
    raise SystemExit(1)


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

执行这个测试用例时,加上-q选项可以减少不必要的日志输出:

/d/Personal Files/Python/pytest-chinese-doc/src (5.1.2)
λ pytest -q test_sysexit.py
.                                                                                                                                                                [100%] 1 passed in 0.01s

在一个类中组织多个测试用例

当你需要新建多个相似的测试用例时,pytest可以让你很容易的通过创建一个测试类来包含所有的测试用例:

测试类的命名也有确定的规则,通常需要以Test开头或结尾;

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

pytest可以发现测试类中所有以test_开头的实例函数,查找过程遵循Python发现测试的约定,你只需要通过传入具体的文件名来执行这些测试:

/d/Personal Files/Python/pytest-chinese-doc/src (5.1.2)
λ pytest test_class.py
================================================= test session starts =================================================
platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: D:\Personal Files\Python\pytest-chinese-doc\src
collected 2 items

test_class.py .F                                                                                                 [100%]

====================================================== FAILURES ======================================================= _________________________________________________ TestClass.test_two __________________________________________________

self = <test_class.TestClass object at 0x00000200EFA83550>

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

test_class.py:30: AssertionError
============================================= 1 failed, 1 passed in 0.05s ===========================================
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python 入门之 模块 下一篇python — 索引与pymysql模块

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目