Generate multiple reports at the end of test suite run - each report goes into a dedicated file in the current directory. The report files are prefixed with the test suite name. This function emulates --duration and -rA pytest arguments. This function is to be called from `conftes
(tr, id)
| 553 | |
| 554 | |
| 555 | def pytest_terminal_summary_main(tr, id): |
| 556 | """ |
| 557 | Generate multiple reports at the end of test suite run - each report goes into a dedicated file in the current |
| 558 | directory. The report files are prefixed with the test suite name. |
| 559 | |
| 560 | This function emulates --duration and -rA pytest arguments. |
| 561 | |
| 562 | This function is to be called from `conftest.py` via `pytest_terminal_summary` wrapper that has to be defined |
| 563 | there. |
| 564 | |
| 565 | Args: |
| 566 | - tr: `terminalreporter` passed from `conftest.py` |
| 567 | - id: unique id like `tests` or `examples` that will be incorporated into the final reports filenames - this is |
| 568 | needed as some jobs have multiple runs of pytest, so we can't have them overwrite each other. |
| 569 | |
| 570 | NB: this functions taps into a private _pytest API and while unlikely, it could break should |
| 571 | pytest do internal changes - also it calls default internal methods of terminalreporter which |
| 572 | can be hijacked by various `pytest-` plugins and interfere. |
| 573 | |
| 574 | """ |
| 575 | from _pytest.config import create_terminal_writer |
| 576 | |
| 577 | if not len(id): |
| 578 | id = "tests" |
| 579 | |
| 580 | config = tr.config |
| 581 | orig_writer = config.get_terminal_writer() |
| 582 | orig_tbstyle = config.option.tbstyle |
| 583 | orig_reportchars = tr.reportchars |
| 584 | |
| 585 | dir = "reports" |
| 586 | Path(dir).mkdir(parents=True, exist_ok=True) |
| 587 | report_files = { |
| 588 | k: f"{dir}/{id}_{k}.txt" |
| 589 | for k in [ |
| 590 | "durations", |
| 591 | "errors", |
| 592 | "failures_long", |
| 593 | "failures_short", |
| 594 | "failures_line", |
| 595 | "passes", |
| 596 | "stats", |
| 597 | "summary_short", |
| 598 | "warnings", |
| 599 | ] |
| 600 | } |
| 601 | |
| 602 | # custom durations report |
| 603 | # note: there is no need to call pytest --durations=XX to get this separate report |
| 604 | # adapted from https://github.com/pytest-dev/pytest/blob/897f151e/src/_pytest/runner.py#L66 |
| 605 | dlist = [] |
| 606 | for replist in tr.stats.values(): |
| 607 | for rep in replist: |
| 608 | if hasattr(rep, "duration"): |
| 609 | dlist.append(rep) |
| 610 | if dlist: |
| 611 | dlist.sort(key=lambda x: x.duration, reverse=True) |
| 612 | with open(report_files["durations"], "w") as f: |
no test coverage detected