简体   繁体   中英

How to use test result in “teardown” in fixtures in “pytest” framework

I am trying to use test result or status in tear down of the fixture, but I am not able to find the code without using the keyword "yield".. in "pytest" framework.

import pytest
import requests

@pytest.fixture
def update_result_todb(request):
    print('In update_result_toDB')
    def _fin():
        try:
            if(testPassed):  # looking for this
                print('Result updated to DB successfully')
            else:
                print('test failed')

        except:
           print ('exception raised')

    request.addfinalizer(_fin)
    return _fin

def test_failure(update_result_todb):
    print('test_failure')
    assert 1 == 0

def test_success(update_result_todb):
    print('test_success')
    assert 0 == 0

**

import pytest
import requests
@pytest.fixture
def update_result_todb(request):
    print('In update_result_toDB')
    def _fin():
        try:
            if(request.node.rep_setup.passed):  # this works
                print('Result updated to DB successfully')
            else:
                print('test failed')
        except:
           print ('exception raised')
    request.addfinalizer(_fin)
    return _fin
def test_failure(update_result_todb):
    print('test_failure')
    assert 1 == 0
def test_success(update_result_todb):
    print('test_success')
    assert 0 == 0

**

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM