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)
| 952 | |
| 953 | |
| 954 | def pytest_terminal_summary_main(tr, id): |
| 955 | """ |
| 956 | Generate multiple reports at the end of test suite run - each report goes into a dedicated file in the current |
| 957 | directory. The report files are prefixed with the test suite name. |
| 958 | |
| 959 | This function emulates --duration and -rA pytest arguments. |
| 960 | |
| 961 | This function is to be called from `conftest.py` via `pytest_terminal_summary` wrapper that has to be defined |
| 962 | there. |
| 963 | |
| 964 | Args: |
| 965 | - tr: `terminalreporter` passed from `conftest.py` |
| 966 | - id: unique id like `tests` or `examples` that will be incorporated into the final reports filenames - this is |
| 967 | needed as some jobs have multiple runs of pytest, so we can't have them overwrite each other. |
| 968 | |
| 969 | NB: this functions taps into a private _pytest API and while unlikely, it could break should pytest do internal |
| 970 | changes - also it calls default internal methods of terminalreporter which can be hijacked by various `pytest-` |
| 971 | plugins and interfere. |
| 972 | |
| 973 | """ |
| 974 | from _pytest.config import create_terminal_writer |
| 975 | |
| 976 | if not len(id): |
| 977 | id = "tests" |
| 978 | |
| 979 | config = tr.config |
| 980 | orig_writer = config.get_terminal_writer() |
| 981 | orig_tbstyle = config.option.tbstyle |
| 982 | orig_reportchars = tr.reportchars |
| 983 | |
| 984 | dir = f"reports/{id}" |
| 985 | Path(dir).mkdir(parents=True, exist_ok=True) |
| 986 | report_files = { |
| 987 | k: f"{dir}/{k}.txt" |
| 988 | for k in [ |
| 989 | "durations", |
| 990 | "errors", |
| 991 | "failures_long", |
| 992 | "failures_short", |
| 993 | "failures_line", |
| 994 | "passes", |
| 995 | "stats", |
| 996 | "summary_short", |
| 997 | "warnings", |
| 998 | ] |
| 999 | } |
| 1000 | |
| 1001 | # custom durations report |
| 1002 | # note: there is no need to call pytest --durations=XX to get this separate report |
| 1003 | # adapted from https://github.com/pytest-dev/pytest/blob/897f151e/src/_pytest/runner.py#L66 |
| 1004 | dlist = [] |
| 1005 | for replist in tr.stats.values(): |
| 1006 | for rep in replist: |
| 1007 | if hasattr(rep, "duration"): |
| 1008 | dlist.append(rep) |
| 1009 | if dlist: |
| 1010 | dlist.sort(key=lambda x: x.duration, reverse=True) |
| 1011 | with open(report_files["durations"], "w") as f: |
nothing calls this directly
no test coverage detected