Skip to content

Commit 6569e74

Browse files
authored
Collect pytest logs on CI (#32)
This features collects pytest log files into log directory of the current virtualenv, so we can collect them on CI.
1 parent 174d255 commit 6569e74

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

.github/workflows/tox.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,8 @@ jobs:
101101
- name: Archive logs
102102
uses: actions/upload-artifact@v4
103103
with:
104-
name: logs.zip
104+
name: logs-${{ matrix.name }}.zip
105105
path: .tox/**/log/
106-
# https://github.com/actions/upload-artifact/issues/123
107-
continue-on-error: true
108106

109107
- name: Report failure if git reports dirty status
110108
run: |

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ You can disable regex check by defining `PYTEST_CHECK_TEST_ID_REGEX=0`.
4747

4848
You can disable the length check by defining `PYTEST_MAX_TEST_ID_LENGTH=0`.
4949

50+
## Prepare pytest log files for collection on CI
51+
52+
As pytest log files are created on temp directory and some CI systems refuse
53+
to collect files from outside the current project, we do copy these files inside
54+
`$VIRTUAL_ENV/log`, same directory used by tox itself. To collect the logs on
55+
Github Actions, you only need a step like:
56+
57+
```yaml
58+
- name: Archive logs
59+
uses: actions/upload-artifact@v4
60+
with:
61+
name: logs-${{ matrix.name }}.zip
62+
path: .tox/**/log/
63+
```
64+
5065
## Release process
5166
5267
Releases are triggered from [GitHub Releases](https://github.com/pytest-dev/pytest-plus/releases)

src/pytest_plus/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
import re
6+
import shutil
67

78
import pytest
89
from _pytest.terminal import TerminalReporter
@@ -45,6 +46,16 @@ def pytest_sessionfinish(session: pytest.Session) -> None:
4546
)
4647
session.exitstatus = 1
4748

49+
# If running under CI (Github Actions included) and inside a venv, do
50+
# copy all pytest own logs inside $VIRTUAL_ENV/log, for collection.
51+
venv = os.environ.get("VIRTUAL_ENV", "")
52+
if os.environ.get("CI", "0") != "0" and venv:
53+
shutil.copytree(
54+
src=session.config._tmp_path_factory.getbasetemp(), # type: ignore[attr-defined] # noqa: SLF001
55+
dst=venv + "/log",
56+
dirs_exist_ok=True,
57+
)
58+
4859

4960
@pytest.hookimpl(tryfirst=True) # type: ignore[misc,unused-ignore]
5061
def pytest_collection_modifyitems(items: list[pytest.Item]) -> None:

0 commit comments

Comments
 (0)