加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

pytest的setup和teardown

发布时间:2020-12-14 14:48:09 所属栏目:百科 来源:网络整理
导读:学过unittest的setup和teardown,前置和后置执行功能。pytest也有此功能并且功能更强大,今天就来学习一下吧。 用例运行级别: 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(setup_function/teardown_function)只对函数用例生效

学过unittest的setup和teardown,前置和后置执行功能。pytest也有此功能并且功能更强大,今天就来学习一下吧。

用例运行级别:

  • 模块级(setup_module/teardown_module)开始于模块始末,全局的

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

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

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

  • 类里面的(setup/teardown)运行在调用方法的前后

demo1:

import pytest
# 函数式
def setup_function():
    print("setup_function")

 teardown_function():
    teardown_function setup_module():
    'setup_module' teardown_module():
    teardown_module test_one():
    正在执行----test_one)
    
 test_two():
    正在执行----test_two test_three():
    正在执行----test_three)
  
if __name__ == __main__:
    pytest.main([-s",test_api.py"])

结果:

?

?setup_module只开头运行一次,teardown_module只结束运行一次,setup_function每次运行用例都执行一次,teardown_function每次用例结束都执行一次。

demo2:

class TestCase():
     setup_class(self):
        setup_class)

     teardown_class(self):
        teardown_class setup_method(self):
        setup_method teardown_method(self):
        teardown_method setup(self):
        setup teardown(self):
        teardown test_one(self):
        )
        
     test_two(self):
         test_three(self):
        "])

结果:

?

?setup_class只开头执行一次,teardown_class只结束执行一次,setup_method和setup都是在用例执行之前分别执行,setup_method的优先级高于setup,teardown_method和teardown也是同理

运行的优先级:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读