mercredi 7 juin 2017

pytest how to change result of the test

I am migrating a bunch of nose2 tests to pytest, those kind that yield and pytest does not cover them out of the box; so I had to craft something for these types, therefore I collect every custom assert of type "myassert val1 == val2, error message", I collect them in an dictionary and through the below hook I add them into the final report

@pytest.hookimpl(hookwrapper=True, tryfirst=True) def pytest_runtest_makereport(item, call): # do whatever you want before the next hook executes outcome = yield

# outcome.excinfo may be None or a (cls, val, tb) tuple
res = outcome.get_result()  # will raise if outcome was exception

if res.when == 'call':
    if not internal_errors == 0:
       #add all collected errors 
       for error in test_results[caller_function_name][False]:
          res.sections.append(("Captured stder call", "ERROR: (%s): %s" % (caller_function_name, error)))
       res.sections.append(("Captured stdout call", "Test case final result FAILED"))

final report contains them, it lists them when running from command line or in pycharm output window, but they do not show up in the junitxml report. also final number of failures is still based on how many test_* failed not how many errors I collected, how can I change that in the report? what report object property to update? also, when running in pycharm, I can see in the root of the report tree the errors just like I see them in the output report when running from command line but they are not associated with each test_* I have run, they are associated with the root report object. prolly that's why they do not show in the junitxml report. I would like an explanation on what hooks and what object to update to change correctly the final report, update the number of tests failed, and link number of errors correctly to each test. thanks a bunch

Aucun commentaire:

Enregistrer un commentaire