5

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
  • 1
    Any chance you can show some of the code you've tried? Maybe give some examples of what you're wanting to be the end result? – B.Adler Jul 31 '19 at 19:00
  • 3
    Check out [Making test result information available in fixtures](https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures). If you don't want to use `yield`, you can use `request.addfinalizer`; check out the examples in [Fixture finalization / executing teardown code](https://docs.pytest.org/en/latest/fixture.html#fixture-finalization-executing-teardown-code). – hoefling Jul 31 '19 at 21:40
  • Thank you, this "request.node.rep_setup.failed" to fix my help :) – Surekha Danduboina Aug 01 '19 at 12:51

1 Answers1

-3

**

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

**