Pytest参数选项自由执行测试用例详解(二)

  

      运行pytest可以指定目录和文件,如果不指定,pytest会搜索当前目录及其子目录中以test_开头或以_test结尾得测试函数。我们把pytest搜索测试文件和测试用例的过程称为测试搜索(test discovery)。只要遵循pytest的命名规则,pytest就能自动搜索所有待执行的测试用例。所有的包必须要有init.py文件(在使用各种编辑器时会自动生成)

1、测试文件命名规则,test_xxx.py或xxx_test.py

2、方法、测试函数命名规则,test_xxx

3、测试类命名规则,Testxxx,并且不能带有 init 方法

Pytest参数选项在脚本中和命令行用法详解(一)

-k选项  -K EXPRESSION

使用表达式指定某个关键字的测试用例,如果某测试名是唯一的或多个测试名的前缀或后缀相同,可快速匹配,匹配范围是全局相同目录或下层目录所有(包名、文件名、类名、函数名为变量),文件名、类名、函数名,必须是test_开头或_test结尾的。

pytest.main(['-k','关键字'])
关键字包含于或等于文件名、类名、函数名,多个关键字之间用and、or、not连接

测试代码如下:

chengzi\test_04.pyimport pytestclass TestClass(object):def test_one(self):assert 2 == 1def test_two(self):assert 2 == 2def test_two2(self):assert 2 == 2

执行匹配包名,运行了3条用例

pytest.main(['-v','-k','chengzi'])

if __name__ == '__main__':pytest.main(['-v','-k','chengzi'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/chengzi/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'Packages': {'pluggy': '0.13.1', 'py': '1.9.0', 'pytest': '6.1.1'}, 'Platform': 'Windows-10-10.0.18362-SP0', 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Python': '3.5.2', 'Plugins': {'allure-pytest': '2.8.18', 'metadata': '1.8.0', 'rerunfailures': '9.1.1', 'html': '1.22.0'}}
rootdir: C:\Users\wangli\PycharmProjects\untitled\chengzi
plugins: allure-pytest-2.8.18, html-1.22.0, metadata-1.8.0, rerunfailures-9.1.1
collecting ... collected 3 itemstest_04.py::TestClass::test_one FAILED                                   [ 33%]
test_04.py::TestClass::test_two PASSED                                   [ 66%]
test_04.py::TestClass::test_two2 PASSED                                  [100%]================================== FAILURES ===================================
_____________________________ TestClass.test_one ______________________________self = <chengzi.test_04.TestClass object at 0x000001F27E537B70>def test_one(self):
>       assert 2 == 1
E       assert 2 == 1
E         +2
E         -1test_04.py:5: AssertionError
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_one - assert 2 == 1
========================= 1 failed, 2 passed in 0.63s =========================Process finished with exit code 0

执行匹配文件名,运行了3条用例

pytest.main(['-v','-k','test_04.py'])
if __name__ == '__main__':pytest.main(['-v','-k','test_04.py'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/chengzi/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'Platform': 'Windows-10-10.0.18362-SP0', 'Python': '3.5.2', 'Packages': {'py': '1.9.0', 'pluggy': '0.13.1', 'pytest': '6.1.1'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Plugins': {'rerunfailures': '9.1.1', 'html': '1.22.0', 'allure-pytest': '2.8.18', 'metadata': '1.8.0'}}
rootdir: C:\Users\wangli\PycharmProjects\untitled\chengzi
plugins: allure-pytest-2.8.18, html-1.22.0, metadata-1.8.0, rerunfailures-9.1.1
collecting ... collected 3 itemstest_04.py::TestClass::test_one FAILED                                   [ 33%]
test_04.py::TestClass::test_two PASSED                                   [ 66%]
test_04.py::TestClass::test_two2 PASSED                                  [100%]================================== FAILURES ===================================
_____________________________ TestClass.test_one ______________________________self = <chengzi.test_04.TestClass object at 0x000002031D345588>def test_one(self):
>       assert 2 == 1
E       assert 2 == 1
E         +2
E         -1test_04.py:6: AssertionError
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_one - assert 2 == 1
========================= 1 failed, 2 passed in 0.48s =========================Process finished with exit code 0

执行匹配类名,运行了3条用例

pytest.main(['-v','-k','Class'])
if __name__ == '__main__':pytest.main(['-v','-k','Class'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/chengzi/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'Plugins': {'html': '1.22.0', 'rerunfailures': '9.1.1', 'metadata': '1.8.0', 'allure-pytest': '2.8.18'}, 'Packages': {'pluggy': '0.13.1', 'py': '1.9.0', 'pytest': '6.1.1'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Python': '3.5.2', 'Platform': 'Windows-10-10.0.18362-SP0'}
rootdir: C:\Users\wangli\PycharmProjects\untitled\chengzi
plugins: allure-pytest-2.8.18, html-1.22.0, metadata-1.8.0, rerunfailures-9.1.1
collecting ... collected 3 itemstest_04.py::TestClass::test_one FAILED                                   [ 33%]
test_04.py::TestClass::test_two PASSED                                   [ 66%]
test_04.py::TestClass::test_two2 PASSED                                  [100%]================================== FAILURES ===================================
_____________________________ TestClass.test_one ______________________________self = <chengzi.test_04.TestClass object at 0x0000023ED5C68B00>def test_one(self):
>       assert 2 == 1
E       assert 2 == 1
E         +2
E         -1test_04.py:6: AssertionError
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_one - assert 2 == 1
========================= 1 failed, 2 passed in 0.21s =========================Process finished with exit code 0

执行匹配函数名,函数名中含test且含two的,运行了2条用例

pytest.main(['-v','-k','test and two'])
if __name__ == '__main__':pytest.main(['-v','-k','test and two'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/chengzi/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'Platform': 'Windows-10-10.0.18362-SP0', 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Plugins': {'rerunfailures': '9.1.1', 'html': '1.22.0', 'allure-pytest': '2.8.18', 'metadata': '1.8.0'}, 'Packages': {'pluggy': '0.13.1', 'py': '1.9.0', 'pytest': '6.1.1'}, 'Python': '3.5.2'}
rootdir: C:\Users\wangli\PycharmProjects\untitled\chengzi
plugins: allure-pytest-2.8.18, html-1.22.0, metadata-1.8.0, rerunfailures-9.1.1
collecting ... collected 3 items / 1 deselected / 2 selectedtest_04.py::TestClass::test_two PASSED                                   [ 50%]
test_04.py::TestClass::test_two2 PASSED                                  [100%]======================= 2 passed, 1 deselected in 0.11s =======================Process finished with exit code 0

-m选项  -m MARKEXPR

标记(marker)用于标记测试并分组,以便快速选中并运行,匹配运行所有标记了装饰器@pytest.mark.marker的类或方法的测试用例,标记名可自行定义,如果标记名为run_all,可使用@pytest.mark.run_all装饰类或函数来做标记,匹配范围是全局相同目录或下层目录所有(类装饰器的标记名、函数装饰器的标记名),文件名、类名、函数名,必须是test_开头或_test结尾的。

pytest.main(['-v','-m','关键字'])
关键字等于类装饰器或函装饰器@pytest.mark.标记名,里的标记名,多个关键字之间用and、or、not连接

测试代码目录:

测试代码:

chengzi\test_01.py
import pytestclass Test_Class(object):def test_one(self):assert 2 == 1@pytest.mark.run_twodef testtwo(self):assert 2 == 2def test_two2(self):assert 2 == 2if __name__ == '__main__':pytest.main(['-v','-m','run_one or run_all'])juzi\test_02.py
@pytest.mark.run_all
class TestClass(object):def test_one(self):assert 2 == 1def test_two(self):assert 2 == 2def test_two2(self):assert 2 == 2if __name__ == '__main__':pytest.main(['-v','-m','run_one or run_all'])import pytesttest_03.py
class TestClass(object):@pytest.mark.run_onedef test_one(self):assert 2 == 1def test_two(self):assert 2 == 2def test_two2(self):assert 2 == 2if __name__ == '__main__':pytest.main(['-v','-m','run_one or run_all'])test_04.py    
class TestClass(object):def test_onqq(self):assert 2 == 1def test_two(self):assert 2 == 2@pytest.mark.two2def test_two2(self):assert 2 == 2if __name__ == '__main__':pytest.main(['-v','-m','run_one or run_all'])  

执行匹配类或函数装饰器里,标记名为run_one或run_all的测试用例,可以看到匹配运行了4条用例

pytest.main(['-v','-m','run_one or run_all'])
if __name__ == '__main__':pytest.main(['-v','-m','run_one or run_all'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1 -- C:\Program Files\Python35\python.exe
cachedir: .pytest_cache
metadata: {'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_101', 'Platform': 'Windows-10-10.0.18362-SP0', 'Python': '3.5.2', 'Plugins': {'allure-pytest': '2.8.18', 'html': '1.22.0', 'metadata': '1.8.0', 'rerunfailures': '9.1.1'}, 'Packages': {'pytest': '6.1.1', 'pluggy': '0.13.1', 'py': '1.9.0'}}
rootdir: C:\Users\wangli\PycharmProjects\untitled
plugins: allure-pytest-2.8.18, html-1.22.0, metadata-1.8.0, rerunfailures-9.1.1
collecting ... collected 12 items / 8 deselected / 4 selectedtest_03.py::TestClass::test_one FAILED                                   [ 25%]
juzi/test_02.py::TestClass::test_one FAILED                              [ 50%]
juzi/test_02.py::TestClass::test_two PASSED                              [ 75%]
juzi/test_02.py::TestClass::test_two2 PASSED                             [100%]================================== FAILURES ===================================
_____________________________ TestClass.test_one ______________________________self = <test_03.TestClass object at 0x00000232EDB1BF60>@pytest.mark.run_onedef test_one(self):
>       assert 2 == 1
E       assert 2 == 1
E         +2
E         -1test_03.py:7: AssertionError
_____________________________ TestClass.test_one ______________________________self = <juzi.test_02.TestClass object at 0x00000232EDB0B080>def test_one(self):
>       assert 2 == 1
E       assert 2 == 1
E         +2
E         -1juzi\test_02.py:6: AssertionError
============================== warnings summary ===============================
test_03.py:5C:\Users\wangli\PycharmProjects\untitled\test_03.py:5: PytestUnknownMarkWarning: Unknown pytest.mark.run_one - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html@pytest.mark.run_onetest_04.py:12C:\Users\wangli\PycharmProjects\untitled\test_04.py:12: PytestUnknownMarkWarning: Unknown pytest.mark.two2 - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html@pytest.mark.two2chengzi\test_01.py:8C:\Users\wangli\PycharmProjects\untitled\chengzi\test_01.py:8: PytestUnknownMarkWarning: Unknown pytest.mark.run_two - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html@pytest.mark.run_twojuzi\test_02.py:3C:\Users\wangli\PycharmProjects\untitled\juzi\test_02.py:3: PytestUnknownMarkWarning: Unknown pytest.mark.run_all - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html@pytest.mark.run_all-- Docs: https://docs.pytest.org/en/stable/warnings.html
=========================== short test summary info ===========================
FAILED test_03.py::TestClass::test_one - assert 2 == 1
FAILED juzi/test_02.py::TestClass::test_one - assert 2 == 1
============ 2 failed, 2 passed, 8 deselected, 4 warnings in 0.24s ============Process finished with exit code 0

-V(--verbose)选项

输出更加详细的信息,比如用例所在的包、py文件、类名及用例名称等测试的名字和结果都会显示出来,最明显区别就是每个文件中的测试用例都会占一行(先前是每个文件占一行)

pytest.main(['-v'])

-s选项

允许终端在测试运行时,输出用例中的调式信息,包括任何符合标准的输出流信息,比如print的打印信息等。

pytest.main(['-s'])

测试代码:

test-04.py
import pytestclass TestClass(object):def test_one(self):print('this is test_one')assert 2 == 1def test_two(self):print('this is test_two')assert 2 == 2@pytest.mark.two2def test_two2(self):print('this is test_two2')assert 2 == 2if __name__ == '__main__':pytest.main(['-s'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\wangli\PycharmProjects\untitled
plugins: allure-pytest-2.8.18, html-1.22.0, metadata-1.8.0, rerunfailures-9.1.1
111
collected 12 itemstest_03.py F..
test_04.py this is test_one
Fthis is test_two
.this is test_two2
.
chengzi\test_01.py F..
juzi\test_02.py F..

-x选项

debug调试时,我们希望遇到断言失败时,立即全局停止,这时就可以用到-x

pytest.main(['-x','test_04.py'])

测试代码如下:

#设置用例1和用例3断言成功,断言2失败
#执行后,用例1执行了,用例2执行断言失败,脚本停止,用例3没有继续执行
import pytestclass TestClass(object):def test_one(self):print('this is test_one')assert 2 == 2def test_two(self):print('this is test_two')assert 2 == 1def test_two2(self):print('this is test_two2')assert 2 == 2if __name__ == '__main__':pytest.main(['-x','test_04.py'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\wangli\PycharmProjects\untitled
plugins: allure-pytest-2.8.18, html-1.22.0, metadata-1.8.0, rerunfailures-9.1.1
collected 3 itemstest_04.py .F================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________self = <test_04.TestClass object at 0x000001465DA2D780>def test_two(self):print('this is test_two')
>       assert 2 == 1
E       assert 2 == 1test_04.py:11: AssertionError
---------------------------- Captured stdout call -----------------------------
this is test_two
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_two - assert 2 == 1
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!
========================= 1 failed, 1 passed in 0.88s =========================Process finished with exit code 0

--maxfail=num

与-x选项类似,测试用例达到指定失败次数,就会全局停止,不在执行,假设允许pytest失败2次后停止,则可以使用--maxfail=2,明确指定可失败次数。

pytest.main(['--maxfail=2','test_04.py'])

测试代码如下:

#设置用例1和用例2断言失败,用例3断言成功
#使用pytest.main(['--maxfail=2','test_04.py'])
#执行了用例1和用例2,达到2次失败,全局停止,未执行用例3
test_04.py
import pytestclass TestClass(object):def test_one(self):print('this is test_one')assert 2 == 1def test_two(self):print('this is test_two')assert 2 == 1@pytest.mark.two2def test_two2(self):print('this is test_two2')assert 2 == 2if __name__ == '__main__':pytest.main(['--maxfail=2','test_04.py'])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/untitled/test_04.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-6.1.1, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\wangli\PycharmProjects\untitled
plugins: allure-pytest-2.8.18, html-1.22.0, metadata-1.8.0, rerunfailures-9.1.1
collected 3 itemstest_04.py FF================================== FAILURES ===================================
_____________________________ TestClass.test_one ______________________________self = <test_04.TestClass object at 0x000001ACC521D5F8>def test_one(self):print('this is test_one')
>       assert 2 == 1
E       assert 2 == 1test_04.py:7: AssertionError
---------------------------- Captured stdout call -----------------------------
this is test_one
_____________________________ TestClass.test_two ______________________________self = <test_04.TestClass object at 0x000001ACC521A668>def test_two(self):print('this is test_two')
>       assert 2 == 1
E       assert 2 == 1test_04.py:11: AssertionError
---------------------------- Captured stdout call -----------------------------
this is test_two
============================== warnings summary ===============================
test_04.py:13C:\Users\wangli\PycharmProjects\untitled\test_04.py:13: PytestUnknownMarkWarning: Unknown pytest.mark.two2 - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/mark.html@pytest.mark.two2-- Docs: https://docs.pytest.org/en/stable/warnings.html
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_one - assert 2 == 1
FAILED test_04.py::TestClass::test_two - assert 2 == 1
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 2 failures !!!!!!!!!!!!!!!!!!!!!!!!!!
======================== 2 failed, 1 warning in 0.17s =========================Process finished with exit code 0

--lf(--last-failed)选项

执行上次断言失败的用例A,当用例A一直是断言失败,运行脚本一直只执行用例A;当用例A断言成功后,没有失败的用例了,会执行全部用例,如果又有断言失败的用例,下次会执行运行失败的用例(运行脚本会执行上次断言失败的用例,没有失败用例会执行所有用例)

pytest.main(['--lf','test_04.py'])
#设置test_two用例断言失败,运行j脚本后只跑了test_two用例
import pytest
class TestClass(object):def test_one(self):print('this is test_one')assert 2 == 2def test_two(self):print('this is test_two')assert 2 == 1def test_two2(self):print('this is test_two2')assert 2 == 2if __name__ == '__main__':pytest.main(['--lf','test_04.py'])collected 3 items / 2 deselected / 1 selected
run-last-failure: rerun previous 1 failuretest_04.py F                                                             [100%]================================== FAILURES ===================================
_____________________________ TestClass.test_two ______________________________self = <test_04.TestClass object at 0x0000028C5FB3A198>def test_two(self):print('this is test_two')
>       assert 2 == 1
E       assert 2 == 1test_04.py:11: AssertionError
---------------------------- Captured stdout call -----------------------------
this is test_two
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_two - assert 2 == 1
======================= 1 failed, 2 deselected in 0.15s =======================Process finished with exit code 0#把用例test_two改成断言正确的,再次运行脚本,仍执行了test_two用例,发现test_two
# 用例断言成功了
import pytest
class TestClass(object):def test_one(self):print('this is test_one')assert 2 == 2def test_two(self):print('this is test_two')assert 2 == 2def test_two2(self):print('this is test_two2')assert 2 == 2if __name__ == '__main__':pytest.main(['--lf','test_04.py'])collected 3 items / 2 deselected / 1 selected
run-last-failure: rerun previous 1 failuretest_04.py .                                                             [100%]======================= 1 passed, 2 deselected in 0.09s =======================Process finished with exit code 0#再次运行脚本,执行了全部用例
import pytest
class TestClass(object):def test_one(self):print('this is test_one')assert 2 == 2def test_two(self):print('this is test_two')assert 2 == 2def test_two2(self):print('this is test_two2')assert 2 == 2if __name__ == '__main__':pytest.main(['--lf','test_04.py'])collected 3 items
run-last-failure: 13 known failures not in selected teststest_04.py ...                                                           [100%]============================== 3 passed in 0.05s ==============================Process finished with exit code 0

--ff(--failed-first)选项

--ff选项,运行脚本,会优先执行上次断言失败的用例,再依次执行其他用例,直至运行脚本,上次断言失败的用例这次变为断言成功,下次再运行脚本,才会按正常用例顺序依次执行,与--lf选项作用基本相同,不同之处--ff会运行剩下 的测试用例。

pytest.main(['-s','--ff','test_04.py'])
# 设置test_two2断言失败,首次运行脚本,未优先执行test_two2,
# 结果test_two2断言失败
import pytest
class TestClass(object):def test_one(self):print('this is test_one')assert 2 == 2def test_two(self):print('this is test_two')assert 2 == 2def test_two2(self):print('this is test_two2')assert 2 == 1if __name__ == '__main__':pytest.main(['-s','--ff','test_04.py'])collected 3 items
run-last-failure: 13 known failures not in selected teststest_04.py this is test_one
.this is test_two
.this is test_two2
F================================== FAILURES ===================================
_____________________________ TestClass.test_two2 _____________________________self = <test_04.TestClass object at 0x000001A124621CF8>def test_two2(self):print('this is test_two2')
>       assert 2 == 1
E       assert 2 == 1test_04.py:15: AssertionError
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_two2 - assert 2 == 1
========================= 1 failed, 2 passed in 0.17s =========================Process finished with exit code 0# 脚本不变,再次运行一次,发现优先执行了上次断言失败的用例test_two2
# 再按顺序依次执行,结果test_two2断言失败
collected 3 items
run-last-failure: rerun previous 1 failure firsttest_04.py this is test_two2
Fthis is test_one
.this is test_two
.================================== FAILURES ===================================
_____________________________ TestClass.test_two2 _____________________________self = <test_04.TestClass object at 0x000002C5D0F03668>def test_two2(self):print('this is test_two2')
>       assert 2 == 1
E       assert 2 == 1test_04.py:15: AssertionError
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_two2 - assert 2 == 1
========================= 1 failed, 2 passed in 0.10s =========================Process finished with exit code 0# test_two2用例改成断言成功,再次执行脚本,发现还是优先执行了上次断言失败的用例test_two2
# 用例,再按顺序依次执行,结果test_two2断言成功def test_two2(self):print('this is test_two2')assert 2 == 2collected 3 items
run-last-failure: rerun previous 1 failure firsttest_04.py this is test_two2
.this is test_one
.this is test_two
.============================== 3 passed in 0.07s ==============================Process finished with exit code 0# 脚本不变,再运行依次脚本,发现未优先执行test_two2用例,因为上次结果
# 没有断言失败的,所有不会优先执行,正常依次执行collected 3 items
run-last-failure: 13 known failures not in selected teststest_04.py this is test_one
.this is test_two
.this is test_two2
.============================== 3 passed in 0.03s ==============================Process finished with exit code 0

--tb=style选项

捕捉到失败时输出信息的显示方式,某个测试用例失败后,pytest会列举出失败信息,包括失败在哪一行、是什么失败、怎么失败的,此过程称“信息回溯”它对找到问题很有帮助,但有时也会对多余的信息感到厌烦,这时--tb=style选项就有用武之地了,style类型有short、line、no。short模式仅输出assert的一行以及系统判定内容(不显示上下文);line模式只使用一行输出显示所有的错误信息;no模式则直接屏蔽全部回溯信息

pytest.main(['--tb=no','test_04.py']),屏蔽全部回溯信息
collected 3 itemstest_04.py ..F                                                           [100%]=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_two2 - assert 2 == 1
========================= 1 failed, 2 passed in 0.09s =========================
pytest.main(['--tb=line','test_04.py']),告诉我们错误的位置
collected 3 itemstest_04.py ..F                                                           [100%]================================== FAILURES ===================================
C:\Users\wangli\PycharmProjects\untitled\test_04.py:15: assert 2 == 1
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_two2 - assert 2 == 1
========================= 1 failed, 2 passed in 0.19s =========================Process finished with exit code 0
pytest.main(['--tb=short','test_04.py']),显示回溯信息比前面两种都详细
collected 3 itemstest_04.py ..F                                                           [100%]================================== FAILURES ===================================
_____________________________ TestClass.test_two2 _____________________________
test_04.py:15: in test_two2assert 2 == 1
E   assert 2 == 1
---------------------------- Captured stdout call -----------------------------
this is test_two2
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_two2 - assert 2 == 1
========================= 1 failed, 2 passed in 0.42s =========================Process finished with exit code 0
pytest.main(['--tb=long','test_04.py'])输出最详细的回溯信息
collected 3 itemstest_04.py ..F                                                           [100%]================================== FAILURES ===================================
_____________________________ TestClass.test_two2 _____________________________self = <test_04.TestClass object at 0x00000229D5D7E8D0>def test_two2(self):print('this is test_two2')
>       assert 2 == 1
E       assert 2 == 1test_04.py:15: AssertionError
---------------------------- Captured stdout call -----------------------------
this is test_two2
=========================== short test summary info ===========================
FAILED test_04.py::TestClass::test_two2 - assert 2 == 1
========================= 1 failed, 2 passed in 0.18s =========================Process finished with exit code 0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/568199.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ISTQB高级国际认证试题及答案(一)

题目 #1 &#xff08;3 分&#xff09;选择2个答案您是旅游信息手机应用项目的测试经理。近期该项目切换到敏捷流程和测试驱动开发&#xff08;TDD&#xff09;。每个开发周期持续15天&#xff0c;在第7天之后开始每日构建。第10天以后&#xff0c;不会再有新的功能加入。开发团…

ISTQB高级-测试经理国际认证试题及答案(二)

1、TM-1.2.1 (K4) 为了计划测试活动和工作产品以实现测试目标&#xff0c;必须对一个系统的测试需求进行分析。问题&#xff1a;您是旅游信息手机应用项目的测试经理。近期该项目切换到敏捷流程和测试驱动开发&#xff08;TDD&#xff09;。每个开发周期持续15天&#xff0c;在…

我的游测之路 | 揭秘游戏测试神秘面纱(一)

经常有人问我&#xff1a;游戏测试到底是干什么呢&#xff1f;是游戏代练&#xff1f;每天玩游戏&#xff1f;装备随便造&#xff0c;怪物随便秒&#xff0c;线上GM指令随便用&#xff1f;可以每天玩玩游戏&#xff0c;不用忙工作&#xff0c;太爽了&#xff1f;当然并非完全如…

opencv22-直方图均衡化

#include<opencv2\highgui\highgui.hpp> #include<opencv2\opencv.hpp> #include<iostream> #include<math.h> using namespace std; using namespace cv; char *output_title "output Image"; Mat src, dst; //霍夫圆检测前先进行中值滤波…

测试管理 | 4种优先级排序方法一定要掌握

卡诺模型&#xff08;KANO模型&#xff09;、大富翁&#xff08;Monopoly Money&#xff09;、莫斯科方法&#xff08;MoSCoW&#xff09;、100点方法&#xff0c;4种方法用于测试管理或项目管理过程中的优先级排序、工作量估算等&#xff0c;如&#xff1a;项目版本优先级、产…

overleaf 支持中文

基本操作 左上角menu中&#xff0c;切换compiler 到xelatex。 然后在\documentclass声明下面加一个 \usepackage{ctex}。 使用\usepackage{xecjk}可能也可以&#xff0c;但会有警告。 警告分析 Font “FandolSong-Regular” does not contain requested Script “CJK”。 网上…

怎样在半个月内迅速提升技能,搞定面试进 大厂?

最近一两个星期&#xff0c;来找我咨询大厂面试与名企跳槽问题的朋友突然多了起来。我才意识到只剩十来天就要进入九月互联网招聘旺季&#xff0c;看来大家都在为跳槽涨薪进大厂做准备了。在小厂做了一两年“测试工程师”&#xff0c;不过是拿到软件点点点&#xff0c;毫无技术…

opencv24-直方图比较

此方法比较准确 还行 此方法不怎么行 此方法比较准确 #include<opencv2\highgui\highgui.hpp> #include<opencv2\opencv.hpp> #include<iostream> #include<math.h> using namespace std; using namespace cv; string convertToString(double d); in…

抓包工具Stream之接口调试和加密解码(二)

移动端iphone抓包调试神器—Stream安装和使用&#xff08;一&#xff09;之前我们介绍了Stream工具的安装和使用&#xff0c;今天我们再来介绍下抓包请求筛选、接口详情、接口导出、接口调试、接口测试、编码解码加密等1、抓包完成&#xff0c;停止抓包&#xff0c;进入最新抓包…

白盒测试 | 用例设计方法之条件覆盖

条件覆盖&#xff0c;程序各判定中的每个条件获得各种可能的取值至少满足一次即可&#xff0c;不考虑路径的覆盖。上图中有两个判定表达式&#xff0c;每个判定表达式中有两个条件&#xff0c;为了做到条件覆盖&#xff0c;第一个判断的所有条件的可能取值情况是A>1或A≤1&a…

白盒测试 | 用例设计方法之判定覆盖

判定覆盖也被成为分支覆盖(Branch Coverage)是设计足够多的测试用例&#xff0c;使得程序中的每一个判断至少获得一次“真”和一次“假”&#xff0c;即使得程序流程图中的每一个真假分支至少被执行一次。使设计的测试用例保证程序中每个判断的每个取值分支&#xff08;ture or…

白盒测试 | 用例设计方法之语句覆盖

语句覆盖&#xff0c;顾名思义就是针对代码语句的嘛。它的含义是我们设计出来的测试用例要保证程序中的每一个语句至少被执行一次。通常语句覆盖被认为是“最弱的覆盖”&#xff0c;原因是它仅仅考虑对代码中的执行语句进行覆盖而没有考虑各种条件和分支&#xff0c;因此在实际…

一套完整的Selenium自动化测试框架设计实战,这次38K, 妥了

金九银十&#xff0c;大家都铆足干劲想抓住机会涨薪进大厂。结合这几年的行业趋势&#xff0c;想要挑战大厂面试&#xff0c;首先必须吃透Selenium自动化测试框架设计。这里说一下原因&#xff1a;首先&#xff0c;一线互联网大厂无一例外&#xff0c;全都要求测试人掌握Seleni…

4天搞定Docker和k8s核心架构,稳拿35k不是问题?

随着云原生时代的到来&#xff0c;各大厂纷纷上云&#xff0c;甚至有人说&#xff0c;未来的软件就是生在云上&#xff0c;长在云上的。在这种云原生时代大势下&#xff0c;衍生出来的 Kubernetes 工程师、云原生工程师的薪资也水涨船高&#xff0c;大厂不惜花重金聘请优秀的云…

1元解锁 | Python万能代码模板 |10大必学实用技巧

内容摘要你要悄悄学 Python&#xff0c;然后惊艳所有人。一听到 Python 或编程语言&#xff0c;你可能条件反射会觉得“很难”。但今天的 Python 课程是个例外&#xff0c;不需要你懂计算机原理&#xff0c;也不需要理解复杂的编程模式&#xff0c;只需替换成你想要的网页链接、…

C++ 递归函数返回值理解

首先看代码&#xff1a; #include <iostream> using namespace std; int main() {int digui(int i);int i;cin>>i;i digui(i);cout << "main函数最后结果&#xff1a;"<<i<<endl; } int digui(int i) {int j 0;if (i 0)return 1;j…

二叉树---树的深度递归理解

int Dep(Bianary *root) {if (root NULL){return 0;}int depth 0;int leftD Dep(root->lchild);int rightD Dep(root->rchild);depth leftD > rightD ? leftD 1 : rightD 1;return depth; } 递归理解&#xff1a; depth的值与是否初始化无关&#xff0c;因为…

升级锦囊 | 测试开发核心技术46讲

课程背景我是思寒&#xff0c;在测试圈里摸爬滚打了十年有余。我从外包功能测试做起&#xff0c;后进入阿里、百度等公司做了测试开发工程师、测试架构师。我发现几乎每个测试工程师都和我一样&#xff0c;职业理想都是从事测试开发的工作。尤其是近几年来&#xff0c;互联网的…