pytest测试框架setup和tearDown的用法

  介绍

这篇文章主要讲解了pytest测试框架setup和tearDown的用法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

<强> pytest的安装与拆卸

1) pytest提供了两套互相独立的安装与拆卸和一对相对自由的安装与拆卸

2)模块级与函数级

模块级(setup_module/teardown_module) #开始于模块始末(不在类中)

函数级(setup_function/teardown_function) #只对函数用例生效(不在类中)

3)方法级与类级

方法级(setup_method/teardown_method) #开始于方法始末(在类中)

类级(setup_class/teardown_class) #只在类中前后运行一次(在类中)

3)类里面的(安装/拆卸)#运行在调用方法的前后

<强>安装与拆卸例子

进口pytest
  #模块中的方法
  def setup_module ():
  打印(
  “setup_module:整个test_module.py模块只执行一次“
  )
  def teardown_module ():
  打印(
  “teardown_module:整个test_module.py模块只执行一次“
  )
  def setup_function ():
  打印(“setup_function:每个用例开始前都会执行“)
  def teardown_function ():
  打印(“teardown_function:每个用例结束后都会执行“)
  #测试模块中的用例1
  def test_one ():
  print(“正在执行测试模块——test_one")
  #测试模块中的用例2
  def test_two ():
  print(“正在执行测试模块——test_two")
  #测试类
  TestCase类():
  def setup_class(自我):
  打印(“setup_class:所有用例执行之前“)
  def teardown_class(自我):
  打印(“teardown_class:所有用例执行之后“)def setup_method (
  自我):
  打印(“setup_method:每个用例开始前执行“)
  def teardown_method(自我):
  打印(“teardown_method:每个用例结束后执行“)
  def设置(自我):
  print(“设置:每个用例开始前都会执行“)
  def拆卸(自我):
  打印(“拆卸:每个用例结束后都会执行“)
  def test_three(自我):
  print(“正在执行测试类——test_three")
  def test_four(自我):
  print(“正在执行测试类——test_four")
  if __name__==癬_main__":
  pytest.main ([“-s",“test_module.py"]) 

看完上述内容,是不是对pytest测试框架setup和tearDown的用法有进一步的了解,如果还想学习更多内容,欢迎关注行业资讯频道。

pytest测试框架setup和tearDown的用法