Time will tell.
1、使用assert
语句进行断言
Pytest 允许使用标准的 Python assert 语法,用来校验expectation and value
是否一致。
代码:
def func():
return 3
def test_func():
assert func() == 4
结果:
(wda_python) bash-3.2$ pytest -q test_assert.py
F [100%]
================================================================ FAILURES ================================================================
_______________________________________________________________ test_func ________________________________________________________________
def test_func():
> assert func() == 4
E assert 3 == 4
E + where 3 = func()
test_assert.py:5: AssertionError
1 failed in 0.07 seconds
(wda_python) bash-3.2$
支持在assert
后面添加描述信息:
def func():
return 3
def test_func():
assert func() == 4, 'Value was odd, should be even'
结果:
(wda_python) bash-3.2$ pytest -q test_assert.py
F [100%]
================================================================ FAILURES ================================================================
_________________________________________