书籍来源:房荔枝 梁丽丽《pytest框架与自动化测试应用》
一边学习一边整理老师的课程内容及实验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:pytest框架进阶自学系列 | 汇总_热爱编程的通信人的博客-CSDN博客
最多允许失败的测试用例数
当达到最大上限时,退出执行。如未配置,则没有上限。
命令pytest -x遇到第一个失败时,退出执行。
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-2> pytest -x
========================================================================================================= test session starts =========================================================================================================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-2, inifile: pytest.ini
plugins: allure-pytest-2.13.2
collected 32 items
test_assert_1.py F
============================================================================================================== FAILURES ===============================================================================================================
______________________________________________________________________________________________________ test_long_str_comparison _______________________________________________________________________________________________________
def test_long_str_comparison():
str3 = 'abcdef'
str4 = 'adcdef'
> assert str3 == str4
E AssertionError: assert 'abcdef' == 'adcdef'
E - adcdef
E ? ^
E + abcdef
E ? ^
test_assert_1.py:4: AssertionError
========================================================================================================== warnings summary ===========================================================================================================
c:\users\guoliang\appdata\local\programs\python\python37\lib\site-packages\_pytest\terminal.py:289
c:\users\guoliang\appdata\local\programs\python\python37\lib\site-packages\_pytest\terminal.py:289: PytestDeprecationWarning: TerminalReporter.writer attribute is deprecated, use TerminalReporter._tw instead at your own risk.
See https://docs.pytest.org/en/latest/deprecations.html#terminalreporter-writer for more information.
"TerminalReporter.writer attribute is deprecated, use TerminalReporter._tw instead at your own risk.\n"
-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================================================================================================= short test summary info =======================================================================================================
FAILED test_assert_1.py::test_long_str_comparison - AssertionError: assert 'abcdef' == 'adcdef'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
==================================================================================================== 1 failed, 1 warning in 10.42s ====================================================================================================
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-2>
命令pytest --maxfail=2遇到第2个失败时,退出执行。同理,命令pytest --maxfail=3遇到第3个失败时退出执行。
执行结果如下:
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-2> pytest --maxfail=2
========================================================================================================= test session starts =========================================================================================================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-2, inifile: pytest.ini
plugins: allure-pytest-2.13.2
collected 32 items
test_assert_1.py FF
============================================================================================================== FAILURES ===============================================================================================================
______________________________________________________________________________________________________ test_long_str_comparison _______________________________________________________________________________________________________
def test_long_str_comparison():
str3 = 'abcdef'
str4 = 'adcdef'
> assert str3 == str4
E AssertionError: assert 'abcdef' == 'adcdef'
E - adcdef
E ? ^
E + abcdef
E ? ^
test_assert_1.py:4: AssertionError
____________________________________________________________________________________________________________ test_eq_list _____________________________________________________________________________________________________________
def test_eq_list():
> assert [0,1,2] == [0,1,3]
E assert [0, 1, 2] == [0, 1, 3]
E At index 2 diff: 2 != 3
E Use -v to get the full diff
test_assert_1.py:7: AssertionError
========================================================================================================== warnings summary ===========================================================================================================
c:\users\guoliang\appdata\local\programs\python\python37\lib\site-packages\_pytest\terminal.py:289
c:\users\guoliang\appdata\local\programs\python\python37\lib\site-packages\_pytest\terminal.py:289: PytestDeprecationWarning: TerminalReporter.writer attribute is deprecated, use TerminalReporter._tw instead at your own risk.
See https://docs.pytest.org/en/latest/deprecations.html#terminalreporter-writer for more information.
"TerminalReporter.writer attribute is deprecated, use TerminalReporter._tw instead at your own risk.\n"
-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================================================================================================= short test summary info =======================================================================================================
FAILED test_assert_1.py::test_long_str_comparison - AssertionError: assert 'abcdef' == 'adcdef'
FAILED test_assert_1.py::test_eq_list - assert [0, 1, 2] == [0, 1, 3]
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 2 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
==================================================================================================== 2 failed, 1 warning in 10.43s ====================================================================================================
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-2>
失败运行管理的原理
用例运行失败后,通常希望再次执行失败的用例,看是环境问题还是脚本问题,因此测试方法运行失败需被记录。
这部分内容与第12章缓存目录设置配置内容相一致。cacheprovider插件将执行状态写入缓存文件夹,pytest会将本轮测试的执行状态写入.pytest_cache文件夹,这个行为是由自带的cacheprovider插件实现的。
pytest默认将测试执行的状态写入根目录中的.pytest_cache文件夹,也可以通过在pytest.ini中配置cache_dir选项来自定义缓存的目录,它可以是相对路径,也可以是绝对路径,相对路径指的是相对于pytest.ini文件所在的目录。
例如,我们想把第12章执行的状态的缓存和源码放在一起,该如何实现呢?实现步骤如下。
在src/chapter12/pytest.ini中添加如下配置:
[pytest]
cache_dir = .pytest-cache
这样,即使在项目的根目录下执行src/chapter12/中的用例,也只会在pytest_book/src/chapter12/.pytest_cache中生成缓存,而不是pytest_book/.pytest_cache中。
(1)在chapter12中复制chapter-2中的test_assert_2.py并改名为test_assert_12.py,需要将内容进行适当修改。
(2)在src路径下执行pytest src/chapter12。
(3)执行状态未写在根目录下,而是写在测试文件所在的目录中(.pytest_cache)。
执行结果如图所示。
cacheprovider插件实现失败管理的准备:
(1)在chapter12下创建pytest.ini文件,代码如下:
[pytest]
cache_dir = .pytest-cache
(2)在chapter12下创建test_failed.py文件,代码如下:
import pytest
@pytest.mark.parametrize('num', [1,2])
def test_failed(num):
assert num == 1
(3)在chapter12下创建test_pass.py文件,代码如下:
def test_pass():
assert 1
(4)如果大家是从前到后进行实践,则需要删除test_assert_12.py和.pytest_cache。
(5)再在根目录下执行pytest src/chapter12。
(6)chapter12下会自动创建缓存文件夹及文件.pytest_cache。
执行结果如下,可以看到一共收集到3个测试用例,其中有一个失败,另外两个成功,并且两个执行成功的用例分属不同的测试模块。
执行结果如下:
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src> pytest .\chapter-12\
========================================================================================================= test session starts =========================================================================================================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1
cachedir: .pytest-cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-12, inifile: pytest.ini
plugins: allure-pytest-2.13.2
collected 3 items
chapter-12\test_failed.py .F [ 66%]
chapter-12\test_pass.py . [100%]
============================================================================================================== FAILURES ===============================================================================================================
___________________________________________________________________________________________________________ test_failed[2] ____________________________________________________________________________________________________________
num = 2
@pytest.mark.parametrize('num', [1,2])
def test_failed(num):
> assert num == 1
E assert 2 == 1
chapter-12\test_failed.py:5: AssertionError
======================================================================================================= short test summary info =======================================================================================================
FAILED chapter-12\test_failed.py::test_failed[2] - assert 2 == 1
===================================================================================================== 1 failed, 2 passed in 0.30s =====================================================================================================
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src>
同时,pytest也在src/chapter12/目录下生成缓存文件夹(.pytest_cache)。
具体介绍一下cacheprovider插件的功能。
--lf,--last-failed:只执行上一轮失败的用例。
缓存中的lastfailed文件记录了上次失败的用例ID,可以通过--cache-show命令查看它的内容。
--cache-show命令是cacheprovider提供的新功能,它不会导致任何用例的执行。
执行结果如下:
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src> pytest .\chapter-12\ -q --cache-show 'lastfailed'
cachedir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-12\.pytest-cache
---------------------------------------------------------------------------------------------------- cache values for 'lastfailed' ----------------------------------------------------------------------------------------------------
cache\lastfailed contains:
{'test_failed.py::test_failed[2]': True}
no tests ran in 0.00s
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src>
可以看到,它记录了一个用例,此用例为上次失败的测试用例的ID:test_failed.py::test_failed[2]。
下次执行,当使用--lf选项时,pytest在收集阶段只会选择这个失败的用例,而忽略其他的用例。命令pytest --lf --collect-only src/chapter12/中的collect-only选项只收集用例而不执行。
执行结果如下:
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src> pytest --lf --collect-only .\chapter-12\
========================================================================================================= test session starts =========================================================================================================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1
cachedir: .pytest-cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-12, inifile: pytest.ini
plugins: allure-pytest-2.13.2
collected 1 item
<Module test_failed.py>
<Function test_failed[2]>
run-last-failure: rerun previous 1 failure (skipped 1 file)
======================================================================================================== no tests ran in 0.02s ========================================================================================================
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src>
仔细观察一下上面的回显,collecting...<Module test_failed.py>只收集到test_fail.py文件中<Function test_failed[2]>,表示第二个执行用例失败了,需要重新执行(跳过5个文件)。1deselected(没有选择),此处的“没有选择”是<Module test_failed.py>中执行通过的第一个用例。跳过5个文件包括pytest.ini和test_pass.py在内的5个文件。
实际上,--lf复写了用例收集阶段的两个钩子方法:pytest_ignore_collect(path,config)和pytest_collection_modifyitems(session,config,items)。
--ff,--failed-first:先执行上一轮失败的用例,再执行其他的用例。
先通过实践看一看这个命令的效果,再去分析它的实现,命令如下:
执行结果如下:
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src> pytest --collect-only -s --ff .\chapter-12\
========================================================================================================= test session starts =========================================================================================================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1
cachedir: .pytest-cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-12, inifile: pytest.ini
plugins: allure-pytest-2.13.2
collected 3 items
<Module test_failed.py>
<Function test_failed[2]>
<Function test_failed[1]>
<Module test_pass.py>
<Function test_pass>
run-last-failure: rerun previous 1 failure first
======================================================================================================== no tests ran in 0.02s ========================================================================================================
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src>
可以看到一共收集到3个测试用例,和正常所收集到的测试用例的顺序相比,上一轮失败的test_failed.py::test_failed[2]用例在最前面,将优先执行。
实际上,-ff只复写了钩子方法:pytest_collection_modifyitems(session,config,items),它可以过滤或者重新排序所收集到的用例。
--nf,--new-first:先执行新加的或修改的用例,再执行其他的用例。
缓存中的nodeids文件记录了上一轮执行的所有用例,命令如下:
执行结果如下:
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src> pytest .\chapter-12\ --cache-show 'nodeids'
========================================================================================================= test session starts =========================================================================================================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1
cachedir: .pytest-cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-12, inifile: pytest.ini
plugins: allure-pytest-2.13.2
cachedir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-12\.pytest-cache
----------------------------------------------------------------------------------------------------- cache values for 'nodeids' ------------------------------------------------------------------------------------------------------
cache\nodeids contains:
['test_failed.py::test_failed[1]',
'test_failed.py::test_failed[2]',
'test_pass.py::test_pass']
======================================================================================================== no tests ran in 0.01s ========================================================================================================
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src>
从执行结果可以看到上一轮共执行了3个测试用例。
现在可以在test_pass.py中新加一个用例,并修改一下test_failed.py文件中的用例(但是不添加新用例)。
代码如下:
def test_pass():
assert 1
def test_new_pass():
assert 1
再来执行一下收集命令,命令如下:
执行结果如下:
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src> pytest --collect-only -s --nf .\chapter-12\
========================================================================================================= test session starts =========================================================================================================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1
cachedir: .pytest-cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-12, inifile: pytest.ini
plugins: allure-pytest-2.13.2
collected 4 items
<Module test_pass.py>
<Function test_new_pass>
<Function test_pass>
<Module test_failed.py>
<Function test_failed[1]>
<Function test_failed[2]>
======================================================================================================== no tests ran in 0.48s ========================================================================================================
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src>
可以看到,新加的用例排在最前面,其次修改过的测试用例紧接其后,最后才是旧的用例,这个行为在源码中有所体现。
--cache-clear:先清除所有缓存,再执行用例。
如果上一轮没有失败的用例,则需要先清除缓存,再执行test_pass.py模块(它的用例都是能测试成功的)。
执行及结果如下:
======================================================================================================== no tests ran in 0.48s ========================================================================================================
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src> pytest --cache-clear -q -s .\chapter-12\test_pass.py
..
2 passed in 0.01s
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src>
其结果是不是少了点什么呢?对!因为没有失败的用例,所以不会生成lastfailed文件,那么这个时候再使用--lf和--ff会发生什么呢?我们来试试。
注意:如果我们观察得足够仔细,就会发现现在的缓存目录和之前相比不仅少了lastfailed文件,还少了CACHEDIR.TAG、.gitignore和README.md这3个文件。这是一个Bug,现在pytest的版本是5.2.1,预计会在之后的版本得到修复。