简体   繁体   中英

How to put PyTest test case report into a variable

I'm using a hook fixture to generate custom report when the test case failed:

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    extra = getattr(rep, 'extra', [])
    if rep.when == 'call' and rep.failed:

Is it possible to get console PyTest report with stdout/stderr into a string variable in term of further using? (in this fixture) Eg report = outcome...

It was done by getting required info directly from rep object, where rep = outcome.get_result() :

def __create_description(self, report):
    stdout = getattr(report, "capstdout")
    longreprtext = getattr(report, "longreprtext")
    delimiter = "\n=============================\n"
    description = delimiter + "Captured stdout:\n" \
                  + stdout + delimiter + "Traceback:\n" + longreprtext
    return description

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