Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving pytest logs in a file

I've seen this question: py.test logging messages and test results/assertions into a single file I've also read documentation here: https://docs.pytest.org/en/latest/logging.html

Neither comes close to a satisfactory solution.

  1. I don't need assertion results together with logs, but it's OK if they are both in the logs.
  2. I need all the logs produced during the test, but I don't need them for the tests themselves. I need them for analyzing the test results after the tests failed / succeeded.
  3. I need logs for both succeeding and for failing tests.
  4. I need stdout to only contain the summary (eg. test-name PASSED). It's OK if the summary also contains the stack trace for failing tests, but it's not essential.
  5. Essentially, I need the test to produce 3 different outputs:
    1. HTML / JUnit XML artifact for CI.
    2. CLI minimal output for CI log.
    3. Extended log for testers / automation team to analyze.

I tried pytest-logs plugin. As far as I can tell, it can override some default pytest behavior by displaying all logging while the test runs. This is slightly better than default behavior, but it's still very far from what I need. From the documentation I understand that pytest-catchlog will conflict with pytest, and I don't even want to explore that option.

Question

Is this achievable by configuring pytest or should I write a plugin, or, perhaps, even a plugin won't do it, and I will have to patch pytest?

like image 521
wvxvw Avatar asked Oct 28 '22 20:10

wvxvw


1 Answers

You can use --junit-xml=xml-path switch to generate junit logs. If you want the report in html format, you can use pytest-html plugin. Similarly, you can use pytest-excel plugin to generate report in excel format.

You can use tee to pipe logs to two different processes. example: pytest --junit-xml=report.xml | tee log_for_testers.log It will generate logs in stdout for CI log, report.xml for CI artifact and log_for_testers.log for team analysis.

like image 118
SilentGuy Avatar answered Nov 15 '22 05:11

SilentGuy