Pytest框架学习5-fixture使用yield实现teardown

     1.  fixture里面的teardown用yield来唤醒teardown的执行

     2.如果某个用例执行失败,module级别的fixture中,yield后的teardown操作仍然继续执行

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

#!usr/bin/python
# -*- coding: utf-8 -*-
import pytest


@pytest.fixture(scope="module")
def login_web():
    print "\nsetup操作"
    # raise AttributeError
    yield
    print "teardown操作"


def test_login_3(login_web):
    print "33333333333"
    raise NameError


def test_login_4(login_web):
    print "44444444"



if __name__ == '__main__':
    pytest.main(["-s", "test_login2.py"])
执行结果:
test_login2.py 
setup操作
33333333333
F44444444
.teardown操作                       仍然执行了


================================== FAILURES ===================================
________________________________ test_login_3 _________________________________

login_web = None

    def test_login_3(login_web):
        print "33333333333"
>       raise NameError
E       NameError

test_login2.py:16: NameError
===================== 1 failed, 1 passed in 0.10 seconds ======================

使用addfinalizer函数进行teardown操作

#!usr/bin/python
# -*- coding: utf-8 -*-
import pytest


@pytest.fixture(scope="module")
def login_web(request):
    print "\n setup操作"
    # raise AttributeError

    # yield
    # print "teardown操作"
    def print_teardown():
        print "用例执行完毕"

    request.addfinalizer(print_teardown)



def test_login_3(login_web):
    print "33333333333"
    # raise NameError


def test_login_4(login_web):
    print "44444444"


if __name__ == '__main__':
    pytest.main(["-s", "test_login2.py"])

setup不报错时可以执行:结果如下:
test_login2.py 
 setup操作
.33333333333
.44444444
用例执行完毕

setup中执行出错后,addfinalizer不执行,跟教程结论不一致,后续确定下:
test_login2.py E
 setup操作

test setup failed
request = >

    @pytest.fixture(scope="module")
    def login_web(request):
        print "\n setup操作"
>       raise AttributeError
E       AttributeError

test_login2.py:9: AttributeError
E
test setup failed
request = >

    @pytest.fixture(scope="module")
    def login_web(request):
        print "\n setup操作"
>       raise AttributeError
E       AttributeError

test_login2.py:9: AttributeError
                                                        [100%]

=================================== ERRORS ====================================
_______________________ ERROR at setup of test_login_3 ________________________

request = >

    @pytest.fixture(scope="module")
    def login_web(request):
        print "\n setup操作"
>       raise AttributeError
E       AttributeError

test_login2.py:9: AttributeError
---------------------------- Captured stdout setup ----------------------------

 setup操作
_______________________ ERROR at setup of test_login_4 ________________________

request = >

    @pytest.fixture(scope="module")
    def login_web(request):
        print "\n setup操作"
>       raise AttributeError
E       AttributeError

test_login2.py:9: AttributeError
=========================== 2 error in 0.06 seconds ===========================

 

你可能感兴趣的:(Pytest)