zoukankan      html  css  js  c++  java
  • pytest(用例的setup和teardown)

    用例运行级别

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

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

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

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

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

    1、模块级别和函数级别

    setup_module是所有用例开始前只执行一次,teardown_module是所有用例结束后只执行一次

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

    代码示例:

    import pytest
    def setup_module():
        print("setup_module:整个.py模块只执行一次")
        print("比如:所有用例开始前只打开一次浏览器")
    def teardown_module():
        print("teardown_module:整个.py模块只执行一次")
        print("比如:所有用例结束只最后关闭浏览器")
    #  函数式,setup_function/teardown_function 每个用例开始和结束调用一次
    def setup_function():
        print("setup_function:每个用例开始前都会执行")
    def teardown_function():
        print("teardown_function:每个用例结束后都会执行")
    def test_01():
        print("正在执行--test_01")
        a = "sure"
        assert 'e' in a
    def test_02():
        print("正在执行--test_02")
        a = "hello"
        assert 'e' in a
        #  assert hasattr(a, 'check')
    def test_03():
        print("正在执行--test_03")
        a = "hello"
        b = "hello world"
        assert a in b
    if __name__ == "_main_":
        pytest.main(["-s", "test_fixt.py"])
    #  -s参数是为了显示用例的打印信息。 -q参数只显示结果,不显示过程
    运行结果:

      test_fixt.py::test_01 setup_module:整个.py模块只执行一次
      比如:所有用例开始前只打开一次浏览器
      setup_function:每个用例开始前都会执行
      PASSED [ 33%]正在执行--test_01
      teardown_function:每个用例结束后都会执行

      test_fixt.py::test_02 setup_function:每个用例开始前都会执行
      PASSED [ 66%]正在执行--test_02
      teardown_function:每个用例结束后都会执行

      test_fixt.py::test_03 setup_function:每个用例开始前都会执行
      PASSED [100%]正在执行--test_03
      teardown_function:每个用例结束后都会执行
      teardown_module:整个.py模块只执行一次
      比如:所有用例结束只最后关闭浏览器

    2、函数和类混合

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

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

    .setup/teardown和unittest里面的setup/teardown是一样的功能,setup_class和teardown_class等价于unittest里面的setupClass和teardownClass

    示例代码:

    import pytest
    
    def setup_module():
        print("setup_module:整个.py模块只执行一次")
        print("比如:所有用例开始前只打开一次浏览器")
    def teardown_module():
        print("teardown_module:整个.py模块只执行一次")
        print("比如:所有用例结束只最后关闭浏览器")
    #  函数式,setup_function/teardown_function 每个用例开始和结束调用一次
    def setup_function():
        print("setup_function:每个用例开始前都会执行")
    def teardown_function():
        print("teardown_function:每个用例结束后都会执行")
    def test_01():
        print("正在执行--test_01")
        a = "sure"
        assert 'e' in a
    def test_02():
        print("正在执行--test_02")
        a = "hello"
        assert 'e' in a
    
    
    class Test_fixt():
        def setup(self):  # 与unittest中的一致
            print("每个用例开始前都会执行")
        def teardown(self):
            print("每个用例结束都会执行")
        def setup_class(self):  # 相当于unittest中的setupClass和teardownClass
            print("类中所有用例执行前执行")
        def teardown(self):
            print("类中所有用例执行结束后执行")
        def test_03(self):
            print("开始执行第三条用例")
        def test_04(self):
            print("开始执行第四条用例")
    if __name__ == "_main_":
        pytest.main(["-s", "test_fixt.py"])
    #  -s参数是为了显示用例的打印信息。 -q参数只显示结果,不显示过程
    运行结果:

        

        test_fixt.py::test_01 setup_module:整个.py模块只执行一次
        比如:所有用例开始前只打开一次浏览器
        setup_function:每个用例开始前都会执行
        PASSED [ 25%]正在执行--test_01
        teardown_function:每个用例结束后都会执行

        test_fixt.py::test_02 setup_function:每个用例开始前都会执行
        PASSED [ 50%]正在执行--test_02
        teardown_function:每个用例结束后都会执行

        test_fixt.py::Test_fixt::test_03
        test_fixt.py::Test_fixt::test_04

        类中所有用例执行前执行
        每个用例开始前都会执行
        PASSED [ 75%]开始执行第三条用例
        类中所有用例执行结束后执行
        每个用例开始前都会执行
        PASSED [100%]开始执行第四条用例
        类中所有用例执行结束后执行
        teardown_module:整个.py模块只执行一次
        比如:所有用例结束只最后关闭浏览器

    1》运行的优先级:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class
    备注:这里setup_method和teardown_method的功能和setup/teardown功能是一样的,一般二者用其中一个即可

    2》setup_module/teardown_module的优先级是最大的,

    函数里面用到的setup_function/teardown_function与类里面的setup_class/teardown_class互不干涉

  • 相关阅读:
    【解决】Windows7任务栏输入法图标无法显示
    【解决】任务栏中找不到语言栏
    SQL Server中根据起始星期计算星期数和周次数的函数
    SharePoint 2010 中如何配置同步配置文件
    如何禁止Windows系统自动安装驱动
    [要点总结]SharePoint 2010:如何配置表单认证(FBA)
    【解决】如何升级网站集内容数据库
    [解决]Office 2010不能同步文件到SharePoint
    羽毛球比赛规则简略版
    [转]SharePoint 2013中如何恢复“用不同用户登录”链接
  • 原文地址:https://www.cnblogs.com/jialeliu/p/14132337.html
Copyright © 2011-2022 走看看