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函数执行测试

###

 

 

 

####

相关文章:

  • 2021-05-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-10
  • 2021-12-13
猜你喜欢
  • 2021-10-09
  • 2021-07-10
  • 2021-11-22
  • 2021-10-06
  • 2021-04-14
  • 2021-06-19
  • 2021-05-20
相关资源
相似解决方案