How to get logs with failed tests from the server (pytest)?



  • I want to pull logs from the server, if the test ended up with FAILED status, tell me please what the signature of a function or fixture should look like, which will be called after each test and have information about its status.

    I run the script like this

    py.test -s -vv -l --alluredir allure / main.py
    


  • The documentation suggests doing this using hooks. Sample code (should be in conftest.py file):

    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
         rep = outcome.get_result ()
    
         # we only look at actual failing test calls, not setup / teardown
         if rep.when == "call" and rep.failed:
             mode = "a" if os.path.exists ("failures") else "w"
             with open ("failures", mode) as f:
                 # let's also access a fixture for the fun of it
                 if "tmpdir" in item.fixturenames:
                     extra = "(% s)"% item.funcargs ["tmpdir"]
                 else:
                     extra = ""
    
                 f.write (rep.nodeid + extra + "\ n")
    

    https://docs.pytest.org/en/latest/example/simple.html#post-process-test-reports-failures



Suggested Topics

  • 2
  • 2
  • 2
  • 3
  • 2