zoukankan      html  css  js  c++  java
  • pytest(六)--fixture之yield实现teardown

    前言

      上一篇讲到fixture通过scope参数控制setup级别,既然有setup作为用例之前的操作,用例执行完之后那肯定也有teardown操作。

      这里用到fixture的teardown操作并不是独立的函数,用yield关键字呼唤teardown操作。

    scope="module"

    1.fixture参数scope="module",module作用是整个.py文件都会生效,用例调用时,参数写上函数名称就行。

    # coding:utf-8
    #test_fix1.py
    import pytest
    @pytest.fixture(scope="module")
    def open():
        print("打开浏览器,并且打开百度")
    def test_1(open):
        print("test_1:搜索fix1")
    def test_2(open):
        print("test_2:搜索fix2")
    def test_3(open):
        print("test_3:搜索fix3")
    if __name__=="__main__":
        pytest.main(["-s","test_fix1.py"])
    

     

    运行结果:

     从结果看出,虽然test_1,test_2,test_3三个地方都调用了open函数,但是它只会在第一个用例前执行一次。

    2.如果test_1不调用,test_2(调用open),test_3不调用,运行顺序会是怎样的?

    # coding:utf-8
    #test_fix1.py
    import pytest
    @pytest.fixture(scope="module")
    def open():
        print("打开浏览器,并且打开百度")
    def test_1():#不调用open
        print("test_1:搜索fix1")
    def test_2(open):
        print("test_2:搜索fix2")
    def test_3(open):
        print("test_3:搜索fix3")
    if __name__=="__main__":
        pytest.main(["-s","test_fix1.py"])  

    运行结果:

     从结果看出,module级别的fixture在当前.py模块里,只会在用例test_2 第一次调用前执行一次。

    yield执行teardown

    1.前面讲的是在用例前加前置条件,相当于setup,既然有setup那就有teardown,fixture里面的teardown用yield来唤醒teardown的执行。

    # coding:utf-8
    #test_fix1.py
    import pytest
    @pytest.fixture(scope="module")
    def open():
        print("打开浏览器,并且打开百度")
        yield
        print("执行teardown")
        print("最后关闭浏览器")
    def test_1(open):
        print("test_1:搜索fix1")
    def test_2():
        print("test_2:搜索fix2")
    def test_3():
        print("test_3:搜索fix3")
    if __name__=="__main__":
        pytest.main(["-s","test_fix1.py"])  

    运行结果:

     yield遇到异常

    1.如果其中一个用例出现异常,不影响yield后面的teardown执行,运行结果互不影响,并且全部用例执行完之后,yield呼唤teardown操作

    # coding:utf-8
    #test_fix1.py
    import pytest
    @pytest.fixture(scope="module")
    def open():
        print("打开浏览器,并且打开百度")
        yield
        print("执行teardown")
        print("最后关闭浏览器")
    def test_1(open):
        print("test_1:搜索fix1")
        raise NameError
    def test_2():
        print("test_2:搜索fix2")
    def test_3():
        print("test_3:搜索fix3")
    if __name__=="__main__":
        pytest.main(["-s","test_fix1.py"])  

    运行结果:

     2.如果在setup就异常了,那么是不会去执行yield后面的teardown内容了

    # coding:utf-8
    #test_fix1.py
    import pytest
    @pytest.fixture(scope="module")
    def open():
        print("打开浏览器,并且打开百度")
        raise NameError
        yield
        print("执行teardown")
        print("最后关闭浏览器")
    def test_1(open):
        print("test_1:搜索fix1")
    def test_2():
        print("test_2:搜索fix2")
    def test_3():
        print("test_3:搜索fix3")
    if __name__=="__main__":
        pytest.main(["-s","test_fix1.py"])  

    运行结果:

     addfinalizer终结函数

    1.除了yield可以实现teardown,在request-context对象中注册addfinalizer方法也可以实现终结函数。

    2.yield和addfinalizer方法都是在测试完成后呼叫相应的代码。但是addfinalizer不同的是:

    • 他可以注册多个终结函数。

    • 这些终结方法总是会被执行,无论在之前的setup code有没有抛出错误。这个方法对于正确关闭所有的fixture创建的资源非常便利,即使其一在创建或获取时失败

     

    越努力,越幸运!!! good good study,day day up!!!
  • 相关阅读:
    行列式运算法则
    神经元的数学模型
    Neural Network and Artificial Neural Network
    常见的23种设计模式
    Java 基本数据类型 及 == 与 equals 方法的区别
    MySQL经典编程问题
    MyEclipse配置tomcat报错
    MySQL 日期类型函数及使用
    MySQL dump文件导入
    MySQL 的两个特殊属性 unsigned与 zerofill
  • 原文地址:https://www.cnblogs.com/canglongdao/p/13397250.html
Copyright © 2011-2022 走看看