006、pytest中有多个断言时,第1个断言失败的话后面的代码(包括第2个断言)不会再执行,跳到下一条测试用例。
pytest中有多个断言时,前面的断言失败的话,后面的代码不会再执行,跳到下一条测试用例。
实例代码如下:
import pytest
from loguru import logger
# 被测试函数
def str_to_int(str_param: str):
if str_param.isdigit():
results = int(str_param)
else:
results = '非int字符串,不能转化为int'
return results
# 测试数据
cases_list = [
('', '非int字符串,不能转化为int'),
('1234', 1234),
('abc', '非int字符串,不能转化为intaa'),
('3.14', '非int字符串,不能转化为int')
]
@pytest.mark.parametrize('item', cases_list)
def test_str_to_int(item):
assert str_to_int(item[0]) == item[1] # 如果在这里断言失败了,后面的代码不会执行,跳到下一条测试用例。
logger.info("aaaaaaaaaaaa")
a = 1
assert 1 == a
执行结果如下,只有3个 aaaaaaaaaaa 输出 :
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day09\venv\Scripts\python.exe "C:\SkyWorkSpace\WorkTools\PyCharm\PyCharm_Community_Edition_202003\PyCharm Community Edition 2020.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --target test.py::test_str_to_int Testing started at 16:36 ... Launching pytest with arguments test.py::test_str_to_int in D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day09\test_08 ============================= test session starts ============================= platform win32 -- Python 3.8.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 -- D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day09\venv\Scripts\python.exe cachedir: .pytest_cache metadata: {'Python': '3.8.6', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '5.4.3', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.9.43', 'Faker': '8.12.1', 'html': '2.1.1', 'metadata': '1.11.0'}, 'JAVA_HOME': 'C:\\SkyWorkSpace\\WorkTools\\Java\\jdk1.8\\jdk1.8.0_271'} rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day09\test_08 plugins: allure-pytest-2.9.43, Faker-8.12.1, html-2.1.1, metadata-1.11.0 collecting ... collected 4 items test.py::test_str_to_int[item0] test.py::test_str_to_int[item1] test.py::test_str_to_int[item2] PASSED [ 25%]2021-09-26 16:36:58.783 | INFO | test:test_str_to_int:26 - aaaaaaaaaaaa PASSED [ 50%]2021-09-26 16:36:58.784 | INFO | test:test_str_to_int:26 - aaaaaaaaaaaa FAILED [ 75%] test.py:22 (test_str_to_int[item2]) item = ('abc', '非int字符串,不能转化为intaa') @pytest.mark.parametrize('item', cases_list) def test_str_to_int(item): > assert str_to_int(item[0]) == item[1] # 如果在这里断言失败了,后面的代码不会执行,跳到下一条测试用例。 E AssertionError test.py:25: AssertionError PASSED [100%]2021-09-26 16:36:58.899 | INFO | test:test_str_to_int:26 - aaaaaaaaaaaa Assertion failed test.py::test_str_to_int[item3] ================================== FAILURES =================================== ___________________________ test_str_to_int[item2] ____________________________ item = ('abc', '非int字符串,不能转化为intaa') @pytest.mark.parametrize('item', cases_list) def test_str_to_int(item): > assert str_to_int(item[0]) == item[1] # 如果在这里断言失败了,后面的代码不会执行,跳到下一条测试用例。 E AssertionError test.py:25: AssertionError =========================== short test summary info =========================== FAILED test.py::test_str_to_int[item2] - AssertionError ========================= 1 failed, 3 passed in 0.29s ========================= Process finished with exit code 1 Assertion failed Assertion failed