首页 > 其他 > 详细

《pytest测试实战》-- Brian Okken

时间:2020-08-12 09:17:15      阅读:274      评论:0      收藏:0      [点我收藏+]

一、pytest 入门

这是一个测试用例

ch1/test_one.py

def test_passing():
    assert (1, 2, 3) == (1, 2, 3)

执行

cd /ch1
pytest test_one.py

结果

(venv) C:\Users\admin\Desktop\ch1>pytest test_one.py
======================================= test session starts ====================
platform win32 -- Python 3.6.8, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\admin\Desktop\ch1
collected 1 item                                                                                                                                                                                                                                                                                                       

test_one.py .                                                               [100%]

========================================= 1 passed in 0.01s ====================

这是第二个测试用例

ch1/test_two.py
def test_passing():
    assert (1, 2, 3) == (3, 2, 1)

运行后结果

(venv) C:\Users\admin\Desktop\ch1>pytest test_two.py
================================================ test session starts ================================
platform win32 -- Python 3.6.8, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\admin\Desktop\ch1
collected 1 item                                                                                                                                                                                                                                                                                                       

test_two.py F                                                                                                                                                                                                                                                                                                    [100%]

=========================== FAILURES =================================
________________________________________ test_passing ____________________

    def test_passing():
>       assert (1, 2, 3) == (3, 2, 1)
E       assert (1, 2, 3) == (3, 2, 1)
E         At index 0 diff: 1 != 3
E         Use -v to get the full diff

test_two.py:2: AssertionError
=================================== short test summary info ===================================
FAILED test_two.py::test_passing - assert (1, 2, 3) == (3, 2, 1)
==================================== 1 failed in 0.03s ========================================

1.1 资源获取

  pytest的官方文档地址

https://docs.pytest.org

  pytest通过PyPI(Python官方包管理索引)分发托管:

https://pypi.python.org/pypi/pytest

  建议使用vritualenv来使用

1.2 运行pytest

pytest --help
usage: pytest [options] [file_or_dir] [file_or_dir] [...]

  在没有其他参数的情况下,pytest会递归遍历每个目录及其子目录。

  举一个例子,我们创建一个tasks子目录,并且创建以下测试文件:

ch1/tasks/test_three.py
"""Test the Task data type."""

from collections import namedtuple

Task = namedtuple(Task, [summary, owner, done, id])
Task.__new__.__defaults__ = (None, None, False, None)  # 指定默认值


def test_defaults():
    """Using no parameters should invoke defaults"""
    t1 = Task()
    t2 = Task(None, None, False, None)
    assert t1 == t2


def test_member_access():
    """Check .field functionality of namedtuple."""
    t = Task(buy milk, brian)
    assert t.summary == buy milk
    assert t.owner == brian
    assert (t.done, t.id) == (False, None)

 

  下面演示下_asdict() 函数和 _replace() 函数的功能:

# ch1/tasks/test_four.py
"""Type the Task data type"""
from collections import namedtuple

Task = namedtuple(Task, [summary, owner, done, id])
Task.__new__.__defaults__ = (None, None, False, None)


def test_asdict():
    """_asdict() should return a dictionary"""
    t_task = Task(do something, okken, True, 21)
    t_dict = t_task._asdict()
    expected = {summary: do something,
               owner: okken,
               done: True,
               id: 21}
    assert t_dict == expected


def test_replace():
    """replace() should change passed in fields"""
    t_before = Task(finish book, brian, False)
    t_after = t_before._replace(id=10, done=True)
    t_expected = Task(finish book, brian, True, 10)
    assert t_after == t_expected

运行时

cd ch1
pytest

技术分享图片

 

  如果不指定,pytest会搜索当前目录及子目录中以test_开头或者以_test结尾的测试函数

  我们把 pytest 搜索测试文件和测试用例的过程称为测试搜索(test discovery)。只要遵守命名规则,就能自动搜索。以下是几条主要的命名规则

  1. 测试文件应当命名为 test_<something>.py 或者 <something_test.py>
  2. 测试函数、测试类方法应当命名为teet_<something>
  3. 测试类应当命名为 Test<Something>

 

 

 

 

 

《pytest测试实战》-- Brian Okken

原文:https://www.cnblogs.com/dongye95/p/13488235.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!