pytest的assert用法

assert小例子

想在抛出异常之后输出一些提示信息,执行之后就方便查看是什么原因了
# 异常信息
def f():
    return 3
def test_function():
    a = f()
    assert a % 2 == 0, "判断 a 为偶数,当前 a 的值为:%s" % a

###

这算是最简单的了,

def test_1():
    assert 1 == 1, "哦,出错了"

 

###

注意pytest的运行方式,函数命名方式要test_开头,至于文件名,不是一定要test_开头,

 # file_name: test_abc.py
 import pytest # 引入pytest包
def test_a(): # test开头的测试函数 print("------->test_a") assert 1 # 断言成功
def test_b(): print("------->test_b") assert 0 # 断言失败

if __name__ == '__main__': pytest.main("-s test_abc.py") # 调用pytest的main函数执行测试

###

 

 

 

####

posted @ 2022-01-30 16:12  技术改变命运Andy  阅读(192)  评论(1编辑  收藏  举报