pytest允许在测试用例中使用标准的python断言,如下:
# test_case.py中的内容
def foo(x, y):
return x + y
def test_01():
assert foo(3, 4) == 8
本例中的函数期望返回一个固定的值。如果该断言失败了,你会看到该函数的返回值:
$ pytest test_assert1.py
============================= test session starts ==============================
platform darwin ‐‐ Python 3.7.6, pytest‐4.2.1, py‐1.7.0, pluggy‐0.8.1
rootdir: /Users/liuke/test/demo, inifile:
collected 1 item
test_case.py F [100%]
=================================== FAILURES ===================================
________________________________ test_function _________________________________
def test_function():
> assert foo(3, 4) == 8
E assert 7 == 8
E + where 7 = foo()
test_case.py:5: AssertionError
=========================== 1 failed in 0.07 seconds ===========================
pytest支持显示常见的子表达式的值,包括调用,属性,比较以及二元和一元运算符。(参看Demo
of Python failure reports with purest 这允许你使用你习惯的python的在不丢失内省信息的情况下
构造代码。(什么是内省信息?更详细的内部输出信息?) 如果你为断言指定了输出信息,那么不会
输出任何内省信息,而是在traceback中直接输出指定的信息:
assert a % 2 ==0, "value was odd, should be even"
更多断言内省信息请参考Advanced assertion introspection
标签:语句,断言,py,assert,pytest,test,foo From: https://www.cnblogs.com/liu-ke/p/17175609.html