Pytest编写测试函数

一个规范的测试方法一定带有断言,在使用pytest时,可以直接使用Python自带的assert关键字

Pytest允许在assert关键字添加任意表达式,表达式的值通过bool转换后等于False则表示断言失败,测试用例执行失败;如果等于True则表示断言成功,测试用例执行成功。

重写assertpytest

可以重写assert关键字,它可以截断对原生assert的调用,替换为pytest定义的assert,从而展示更详细的信息和细节。

from collections import namedtuple
Task = namedtuple('Task', ['summary','owner','done','id'])
# __new__.__defaults__创建默认的Task对象
Task.__new__.__defaults__ = (None, None, False, None)

import pytest


def test_task_equality():
    t1 = Task('sit there', 'brain')
    t2 = Task('do something', 'okken')
    print(t1)
    print(t2)
    assert t1 == t2


def test_dict_equal():
    t1_dict = Task('make sandwich', 'okken')._asdict()
    t2_dict = Task('make sandwich', 'okkem')._asdict()
    print(t1_dict)
    print(t2_dict)
    assert t1_dict == t2_dict

执行结果如下:

E:\Programs\Python\Python_Pytest\TestScripts>pytest test_three.py
======================= test session starts ==========================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 2 items

test_three.py FF                                                                                                                                                     [100%]

===================== FAILURES ==================================
_____________________ test_task_equality _______________________________

    def test_task_equality():
        t1 = Task('sit there', 'brain')
        t2 = Task('do something', 'okken')
        print(t1)
        print(t2)
>       assert t1 == t2
E       AssertionError: assert Task(summary=...alse, id=None) == Task(summary='...alse, id=None)
E         At index 0 diff: 'sit there' != 'do something'
E         Use -v to get the full diff

test_three.py:14: AssertionError
-------------------- Captured stdout call ----------------------------------------
Task(summary='sit there', owner='brain', done=False, id=None)
Task(summary='do something', owner='okken', done=False, id=None)
________________ test_dict_equal ______________________________

    def test_dict_equal():
        t1_dict = Task('make sandwich', 'okken')._asdict()
        t2_dict = Task('make sandwich', 'okkem')._asdict()
        print(t1_dict)
        print(t2_dict)
>       assert t1_dict == t2_dict
E       AssertionError: assert OrderedDict([...('id', None)]) == OrderedDict([(...('id', None)])
E         Omitting 3 identical items, use -vv to show
E         Differing items:
E         {'owner': 'okken'} != {'owner': 'okkem'}
E         Use -v to get the full diff

test_three.py:22: AssertionError
------------------------------- Captured stdout call --------------------------------------------
OrderedDict([('summary', 'make sandwich'), ('owner', 'okken'), ('done', False), ('id', None)])
OrderedDict([('summary', 'make sandwich'), ('owner', 'okkem'), ('done', False), ('id', None)])
================ 2 failed in 0.20 seconds=============================

加上参数-v,执行结果如下:

E:\Programs\Python\Python_Pytest\TestScripts>pytest test_three.py -v
================= test session starts ===============================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 2 items

test_three.py::test_task_equality FAILED                                                                                                                             [ 50%]
test_three.py::test_dict_equal FAILED                                                                                                                                [100%]

==================== FAILURES =========================
____________________ test_task_equality ________________________

    def test_task_equality():
        t1 = Task('sit there', 'brain')
        t2 = Task('do something', 'okken')
        print(t1)
        print(t2)
>       assert t1 == t2
E       AssertionError: assert Task(summary=...alse, id=None) == Task(summary='...alse, id=None)
E         At index 0 diff: 'sit there' != 'do something'
E         Full diff:
E         - Task(summary='sit there', owner='brain', done=False, id=None)
E         ?                ^^^  ^^^          ^^^^
E         + Task(summary='do something', owner='okken', done=False, id=None)
E         ?               +++ ^^^  ^^^          ^^^^

test_three.py:14: AssertionError
-------------------- Captured stdout call -----------------------------------
Task(summary='sit there', owner='brain', done=False, id=None)
Task(summary='do something', owner='okken', done=False, id=None)
____________________ test_dict_equal ________________________________

    def test_dict_equal():
        t1_dict = Task('make sandwich', 'okken')._asdict()
        t2_dict = Task('make sandwich', 'okkem')._asdict()
        print(t1_dict)
        print(t2_dict)
>       assert t1_dict == t2_dict
E       AssertionError: assert OrderedDict([...('id', None)]) == OrderedDict([(...('id', None)])
E         Omitting 3 identical items, use -vv to show
E         Differing items:
E         {'owner': 'okken'} != {'owner': 'okkem'}
E         Full diff:
E         OrderedDict([('summary', 'make sandwich'),
E         -              ('owner', 'okken'),
E         ?                             ^...
E
E         ...Full output truncated (5 lines hidden), use '-vv' to show

test_three.py:22: AssertionError
--------------------------- Captured stdout call --------------------------------
OrderedDict([('summary', 'make sandwich'), ('owner', 'okken'), ('done', False), ('id', None)])
OrderedDict([('summary', 'make sandwich'), ('owner', 'okkem'), ('done', False), ('id', None)])
================= 2 failed in 0.13 seconds ============================

预期异常

"""
在Task项目的API中,有几个地方可能抛出异常
def add(task):  # type:(Task) ->int
def get(task_id):  # type:(int) ->Task
def list_tasks(owner=None):  # type:(str|None) ->list of task
def count():  # type:(None) ->int
def update(task_id, task):  # type:(int, Task) ->None
def delete(task_id):  # type:(int) ->None
def delete_all():  # type:() ->None
def unique_id():  # type:() ->int
def start_tasks_db(db_path, db_type):  # type:(str, str) ->None
def stop_tasks_db():  # type:() ->None
"""

import pytest
import tasks

def test_add_raises():
    with pytest.raises(TypeError):
        tasks.add(task='no a Task object')

"""
测试用例中有with pytest.raise(TypeError)生命,意味着无论with结构中的内容是什么
都至少会发生TypeError异常,如果测试通过,说明确实发生了我们预期TypeError,如果抛出的是其他类型的异常
则与我们预期的异常不一致,测试用例执行失败
"""

def test_start_tasks_db_raises():
    with pytest.raises(ValueError) as excinfo:
        tasks.start_tasks_db('some/great/path', 'mysql')
    exception_msg = excinfo.value.args[0]
    assert exception_msg == "db_type must be a 'tiny' or 'mongo' "
    

测试函数的标记

from collections import namedtuple
import pytest
Task = namedtuple('Task', ['summary','owner','done','id'])
# __new__.__defaults__创建默认的Task对象
Task.__new__.__defaults__ = (None, None, False, None)


@pytest.mark.smoke
def test_task_equality():
    t1 = Task('sit there', 'brain')
    t2 = Task('do something', 'okken')
    print(t1)
    print(t2)
    assert t1 == t2


@pytest.mark.dict
@pytest.mark.smoke
def test_dict_equal():
    t1_dict = Task('make sandwich', 'okken')._asdict()
    t2_dict = Task('make sandwich', 'okkem')._asdict()
    print(t1_dict)
    print(t2_dict)
    assert t1_dict == t2_dict

执行结果如下:

E:\Programs\Python\Python_Pytest\TestScripts>pytest -v -m 'smoke' test_five.py
================ test session starts =====================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 2 items

test_five.py::test_task_equality FAILED                                                                                                                              [ 50%]
test_five.py::test_dict_equal FAILED                                                                                                                                 [100%]

=================== FAILURES ==============================
______________ test_task_equality _________________________
    @pytest.mark.smoke
    def test_task_equality():
        t1 = Task('sit there', 'brain')
        t2 = Task('do something', 'okken')
        print(t1)
        print(t2)
>       assert t1 == t2
E       AssertionError: assert Task(summary=...alse, id=None) == Task(summary='...alse, id=None)
E         At index 0 diff: 'sit there' != 'do something'
E         Full diff:
E         - Task(summary='sit there', owner='brain', done=False, id=None)
E         ?                ^^^  ^^^          ^^^^
E         + Task(summary='do something', owner='okken', done=False, id=None)
E         ?               +++ ^^^  ^^^          ^^^^

test_five.py:14: AssertionError
----------------------- Captured stdout call ----------------------------------
Task(summary='sit there', owner='brain', done=False, id=None)
Task(summary='do something', owner='okken', done=False, id=None)
___________________ test_dict_equal ___________________
    @pytest.mark.dict
    @pytest.mark.smoke
    def test_dict_equal():
        t1_dict = Task('make sandwich', 'okken')._asdict()
        t2_dict = Task('make sandwich', 'okkem')._asdict()
        print(t1_dict)
        print(t2_dict)
>       assert t1_dict == t2_dict
E       AssertionError: assert OrderedDict([...('id', None)]) == OrderedDict([(...('id', None)])
E         Omitting 3 identical items, use -vv to show
E         Differing items:
E         {'owner': 'okken'} != {'owner': 'okkem'}
E         Full diff:
E         OrderedDict([('summary', 'make sandwich'),
E         -              ('owner', 'okken'),
E         ?                             ^...
E
E         ...Full output truncated (5 lines hidden), use '-vv' to show

test_five.py:24: AssertionError
------------------------ Captured stdout call -------------------------------
OrderedDict([('summary', 'make sandwich'), ('owner', 'okken'), ('done', False), ('id', None)])
OrderedDict([('summary', 'make sandwich'), ('owner', 'okkem'), ('done', False), ('id', None)])
================ warnings summary =========================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoi
d this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.dict - is this a typo?  You can register custom marks to avoid
 this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
============== 2 failed, 2 warnings in 0.22 seconds =======================

在命令行执行使用了 -m marker_name参数,pytest在执行时会自动寻找被标记为marker_name的测试方法去执行,同时-m还支持and、or、not关键字,如下方式:

E:\Programs\Python\Python_Pytest\TestScripts>pytest -v -m "smoke and not dict" test_five.py
=========== test session starts ==============================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 2 items / 1 deselected / 1 selected

test_five.py::test_task_equality FAILED                                                                                                                              [100%]

=============== FAILURES ==================
_______________ test_task_equality ____________________________

    @pytest.mark.smoke
    def test_task_equality():
        t1 = Task('sit there', 'brain')
        t2 = Task('do something', 'okken')
        print(t1)
        print(t2)
>       assert t1 == t2
E       AssertionError: assert Task(summary=...alse, id=None) == Task(summary='...alse, id=None)
E         At index 0 diff: 'sit there' != 'do something'
E         Full diff:
E         - Task(summary='sit there', owner='brain', done=False, id=None)
E         ?                ^^^  ^^^          ^^^^
E         + Task(summary='do something', owner='okken', done=False, id=None)
E         ?               +++ ^^^  ^^^          ^^^^

test_five.py:14: AssertionError
--------------- Captured stdout call ------------------------------
Task(summary='sit there', owner='brain', done=False, id=None)
Task(summary='do something', owner='okken', done=False, id=None)
================ warnings summary ====================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoi
d this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.dict - is this a typo?  You can register custom marks to avoid
 this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
============= 1 failed, 1 deselected, 2 warnings in 0.14 seconds ===========

跳过测试

from collections import namedtuple
import pytest
Task = namedtuple('Task', ['summary','owner','done','id'])
# __new__.__defaults__创建默认的Task对象
Task.__new__.__defaults__ = (None, None, False, None)


@pytest.mark.smoke
def test_task_equality():
    t1 = Task('sit there', 'brain')
    t2 = Task('do something', 'okken')
    print(t1)
    print(t2)
    assert t1 == t2


@pytest.mark.dict
@pytest.mark.smoke
@pytest.mark.skip(reason="跳过原因你不知道吗?")
def test_dict_equal():
    t1_dict = Task('make sandwich', 'okken')._asdict()
    t2_dict = Task('make sandwich', 'okkem')._asdict()
    print(t1_dict)
    print(t2_dict)
    assert t1_dict == t2_dict

执行结果:

E:\Programs\Python\Python_Pytest\TestScripts>pytest -v test_five.py
=============== test session starts ===================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 2 items

test_five.py::test_task_equality FAILED                                                                                                                              [ 50%]
test_five.py::test_dict_equal SKIPPED                                                                                                                                [100%]

============== FAILURES ==================================
__________________ test_task_equality _______________________________

    @pytest.mark.smoke
    def test_task_equality():
        t1 = Task('sit there', 'brain')
        t2 = Task('do something', 'okken')
        print(t1)
        print(t2)
>       assert t1 == t2
E       AssertionError: assert Task(summary=...alse, id=None) == Task(summary='...alse, id=None)
E         At index 0 diff: 'sit there' != 'do something'
E         Full diff:
E         - Task(summary='sit there', owner='brain', done=False, id=None)
E         ?                ^^^  ^^^          ^^^^
E         + Task(summary='do something', owner='okken', done=False, id=None)
E         ?               +++ ^^^  ^^^          ^^^^

test_five.py:14: AssertionError
------------------ Captured stdout call --------------------------------
Task(summary='sit there', owner='brain', done=False, id=None)
Task(summary='do something', owner='okken', done=False, id=None)
=========== warnings summary ==============================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoi
d this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.dict - is this a typo?  You can register custom marks to avoid
 this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=========== 1 failed, 1 skipped, 2 warnings in 0.21 seconds ================

从测试结果中我们看到,测试用例test_five.py::test_dict_equal SKIPPED 被跳过了,然而跳过原因并没有如期的展示出来,可以使用参数-rs来展示跳过原因,如下方式:

E:\Programs\Python\Python_Pytest\TestScripts>pytest -rs test_five.py
===================== test session starts ===========================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 2 items

test_five.py Fs                                                                                                                                                      [100%]

============ FAILURES ====================================
__________________________ test_task_equality _______________________
    @pytest.mark.smoke
    def test_task_equality():
        t1 = Task('sit there', 'brain')
        t2 = Task('do something', 'okken')
        print(t1)
        print(t2)
>       assert t1 == t2
E       AssertionError: assert Task(summary=...alse, id=None) == Task(summary='...alse, id=None)
E         At index 0 diff: 'sit there' != 'do something'
E         Use -v to get the full diff

test_five.py:14: AssertionError
-------------------- Captured stdout call ----------------------------------
Task(summary='sit there', owner='brain', done=False, id=None)
Task(summary='do something', owner='okken', done=False, id=None)
========== warnings summary ====================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoi
d this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.dict - is this a typo?  You can register custom marks to avoid
 this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
============ short test summary info ====================
SKIPPED [1] test_five.py:17: 跳过原因你不知道吗?
========== 1 failed, 1 skipped, 2 warnings in 0.14 seconds =================

还可以给跳过添加理由,例如当selenium版本是1.0.4的时候跳过

from collections import namedtuple
import pytest
import selenium
Task = namedtuple('Task', ['summary','owner','done','id'])
# __new__.__defaults__创建默认的Task对象
Task.__new__.__defaults__ = (None, None, False, None)


@pytest.mark.smoke
def test_task_equality():
    t1 = Task('sit there', 'brain')
    t2 = Task('do something', 'okken')
    print(t1)
    print(t2)
    assert t1 == t2


@pytest.mark.dict
@pytest.mark.smoke
@pytest.mark.skipif(selenium.__version__ == '1.0.4', reason="跳过原因你不知道吗?")
def test_dict_equal():
    t1_dict = Task('make sandwich', 'okken')._asdict()
    t2_dict = Task('make sandwich', 'okkem')._asdict()
    print(t1_dict)
    print(t2_dict)
    assert t1_dict == t2_dict

运行测试子集

运行单个目录,只需要将目录作为pytest的参数即可

pytest -v test/func  --tb=no
1
运行单个测试函数,只需要在文件名后添加::符号和函数名

pytest -v test/func/test_five.py::test_dict_equal
1
运行单个测试类,与运行单个测试函数类似,在文件名后添加::符号和类名

pytest -v test/func/test_five.py::Test_Update
1
运行单个测试类中的测试方法,在文件名后添加::符号和类名然后再跟::符号和函数名

pytest -v test/func/test_five.py::Test_Update::test_dict_equal
01-22 22:14
查看更多