码迷,mamicode.com
首页 > 其他好文 > 详细

Pytest学习(一)

时间:2021-07-13 17:47:16      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:func   运算   imp   amp   iter   tar   assertion   用例   文件   

最近看了许多Pytest的文档,发现有些已经是很早以前的版本了。沿着学习的道路,我记录下我学习Pytest的过程

安装

pip install pytest

安装很简单,如果第一次安装用上述pip安装,如果已经安装了使用

pip install -U pytest

-U参数具有更新作用

我的使用环境

platform win32 -- Python 3.8.10, pytest-6.2.4, py-1.10.0, pluggy-0.13.1

创建第一个测试

用四行代码创建一个简单的测试函数:

# 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-6.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:6: AssertionError
========================= short test summary info ==========================
FAILED test_sample.py::test_answer - assert 4 == 5
============================ 1 failed in 0.12s =============================

这里是在cmd中执行的,也可以在pycharm的Terminal执行(如果你安装了虚拟环境)

这个 [100%] 指运行所有测试用例的总体进度。完成后,pytest会显示一个失败报告,因为 func(3) 不返 5 。

pytest可以搜索指定目录下包括子目录的test_*.py和*_test.py脚本。注意:这里是“.py”文件

断言

pytest 允许您使用标准的python assert 用于验证Python测试中的期望和值

# content of test_assert1.py
def f():
    return 3


def test_function():
    assert f() == 4

断言函数返回某个值。如果此断言失败,您将看到函数调用的返回值:

$ pytest test_assert1.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 1 item

test_assert1.py F                                                    [100%]

================================= FAILURES =================================
______________________________ test_function _______________________________

    def test_function():
>       assert f() == 4
E       assert 3 == 4
E        +  where 3 = f()

test_assert1.py:6: AssertionError
========================= short test summary info ==========================
FAILED test_assert1.py::test_function - assert 3 == 4
============================ 1 failed in 0.12s =============================

pytest 支持显示最常见的子表达式的值,包括调用、属性、比较以及二进制和一元运算符。

def test_ne():
    a = 7
    assert a % 2 == 0, "value was odd, should be even"

pytest运行结果

==================================================================================== FAILURES =====================================================================================
_____________________________________________________________________________________ test_ne _____________________________________________________________________________________

    def test_ne():
        a = 7
>       assert a % 2 == 0, "value was odd, should be even"
E       AssertionError: value was odd, should be even
E       assert (7 % 2) == 0

2_test.py:10: AssertionError

如图你只是知道7%2==0失败,但7%2的值你不知道。

改为:

def test_ne():
    a = 7
    value = a % 2
    assert value == 0, "value was odd, should be even"

这样会得出:

_____________________________________________________________________________________ test_ne _____________________________________________________________________________________

    def test_ne():
        a = 7
        value = a % 2
>       assert value == 0, "value was odd, should be even"
E       AssertionError: value was odd, should be even
E       assert 1 == 0

2_test.py:11: AssertionError

这样就会知道value的值,会帮助我们发现问题

关于预期异常的断言

import pytest


def test_zero_division():
    with pytest.raises(ZeroDivisionError):
        1 / 0

如果您需要访问实际的异常信息,可以使用:

def test_recursion_depth():
    with pytest.raises(RuntimeError) as excinfo:

        def f():
            f()

        f()
    assert "maximum recursion" in str(excinfo.value)

 

Pytest学习(一)

标签:func   运算   imp   amp   iter   tar   assertion   用例   文件   

原文地址:https://www.cnblogs.com/gexbooks/p/15006820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!