question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to fetch the "test_" method status in teardown(after each test method)

See original GitHub issue

Hi, I am trying to fetch the test method result post execution of each test method. For this I am using hook to do that but I am not able to fetch the results as per pytest test results i.e.,(xpass, xfail, fail, etc) But from the below code i am able to fetch the test result from report object but for the case where i am making test xfail i am able to fetch output as “skipped” but pytest is showing it as xfail.

Could you please help me out in fetching pytest test method output after each test execution. i.e., as xfail, xpass, pass, fail, error, etc

Environment details: platform win32(Windows 10) – Python 3.7.3, pytest-5.3.1,

conftest.py

import pytest
import os.path

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    # execute all other hooks to obtain the report object
    outcome = yield
    print(f"{outcome}")
    rep = outcome.get_result()
    print(f"\n{rep}")
    if rep.when == "call":
        print(f'{rep.outcome}')

test_file.py

import pytest
def test_fail1(tmpdir):
    assert False

def test_fail2():
    assert True

def test_fail3():
    pytest.xfail("Intenional")

def test_fail4():
    1/0

Console Output:

pytest test_file.py -s -v
.......
collected 4 items                                                                

testresults.py::test_fail1 <pluggy.callers._Result object at 0x000001A0E8A24860>

<TestReport 'testresults.py::test_fail1' when='setup' outcome='passed'>
<pluggy.callers._Result object at 0x000001A0E8F7E588>

<TestReport 'testresults.py::test_fail1' when='call' outcome='failed'>
failed
FAILED<pluggy.callers._Result object at 0x000001A0E8F7E438>

<TestReport 'testresults.py::test_fail1' when='teardown' outcome='passed'>

testresults.py::test_fail2 <pluggy.callers._Result object at 0x000001A0E8F7E438>

<TestReport 'testresults.py::test_fail2' when='setup' outcome='passed'>
<pluggy.callers._Result object at 0x000001A0E8F7E438>

<TestReport 'testresults.py::test_fail2' when='call' outcome='passed'>
passed
PASSED<pluggy.callers._Result object at 0x000001A0E8F7E438>

<TestReport 'testresults.py::test_fail2' when='teardown' outcome='passed'>

testresults.py::test_fail3 <pluggy.callers._Result object at 0x000001A0E8F7E588>

<TestReport 'testresults.py::test_fail3' when='setup' outcome='passed'>
<pluggy.callers._Result object at 0x000001A0E8F856D8>

<TestReport 'testresults.py::test_fail3' when='call' outcome='skipped'>
**skipped**
**XFAIL**<pluggy.callers._Result object at 0x000001A0E8F85BA8>

<TestReport 'testresults.py::test_fail3' when='teardown' outcome='passed'>

testresults.py::test_fail4 <pluggy.callers._Result object at 0x000001A0E8F85A58>

<TestReport 'testresults.py::test_fail4' when='setup' outcome='passed'>
<pluggy.callers._Result object at 0x000001A0E8F859B0>

<TestReport 'testresults.py::test_fail4' when='call' outcome='failed'>
**failed**
**FAILED**<pluggy.callers._Result object at 0x000001A0E8F7E668>

<TestReport 'testresults.py::test_fail4' when='teardown' outcome='passed'>


=================================== FAILURES ====================================
__________________________________ test_fail1 ___________________________________

tmpdir = local('C:\\Users\\Ravi\\AppData\\Local\\Temp\\pytest-of-Ravi\\pytest-60\\
test_fail10')

    def test_fail1(tmpdir):
>       assert False
E       assert False

testresults.py:3: AssertionError
__________________________________ test_fail4 ___________________________________

    def test_fail4():
>       1/0
E       ZeroDivisionError: division by zero

testresults.py:12: ZeroDivisionError
==================== 2 failed, 1 passed, 1 xfailed in 0.31s =====================

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
aklajnertcommented, Feb 25, 2020

The xfail status can be determined by a small workaround actually:

xfail = rep.outcome == 'skipped' and hasattr(rep, 'wasxfail')

Maybe that will be useful.

0reactions
Zac-HDcommented, Nov 29, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fetch the status of test-case in teardown, after each test ...
Executing few test-cases as part of test-suite. Goal is to skip test-case, if there is any failure in previous test-cases or any dependency....
Read more >
How to get the name of a test method that was run in a TestNG ...
Step 1 − Create a TestNG class NewTestngClass and write the @AfterMethod method. ; Step 2 − Write the following code inside @AfterMethod....
Read more >
Set Up and Tear Down State in Your Tests - Apple Developer
XCTest runs the teardown methods once after each test method completes, with tearDown() first, then tearDownWithError() , then tearDown() async throws .
Read more >
Writing Avocado Tests — Avocado 61.0 documentation
SKIP - the test's pre-requisites were not satisfied and the test's body was not executed (nor its setUp() and tearDown ). CANCEL -...
Read more >
Unit testing fundamentals - Visual Studio (Windows)
You can quickly generate test projects and test methods from your code, ... method and teardown methods that are run after each test...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found