Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thorough reporting option #2

Merged
merged 2 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ pytest [...]
# `test-results/report-1735941218.338192.xml` will be created
```

### Thorough JUnit report files

`pytest-nm-releng` can append some flags that will make the generated JUnit report files more thorough/comprehensive:

- All output will be included in the reported (including stdout/stderr like logging)
- All typical results/output will be included for passing tests (normally this is only captured for failing/etc. tests)

> ![NOTE]
> This does _not_ append any flags to actually generate reports. This must be done manually or with the [Dynamically-named JUnit report files](#dynamically-named-junit-report-files) feature.

To enable this feature, set the `NMRE_JUNIT_FULL` env var to `1`:

```shell
# example: prefixing a command
NMRE_JUNIT_FULL=1 pytest [...]
```

### Code coverage

`pytest-nm-releng` can automatically add some code coverage flags as well (requires [pytest-cov]).
Expand Down
8 changes: 8 additions & 0 deletions src/pytest_nm_releng/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def generate_junit_flags() -> list[str]:
return [f"--junit-xml={junitxml_file.as_posix()}"]


def generate_full_junit_flags() -> list[str]:
if os.environ.get("NMRE_JUNIT_FULL", "") == "1":
return ["-o", "junit_logging=all", "-o", "junit_log_passing_tests=True"]

return []


def generate_coverage_flags() -> list[str]:
if not (cc_package_name := os.getenv("NMRE_COV_NAME")):
return []
Expand All @@ -49,5 +56,6 @@ def generate_coverage_flags() -> list[str]:
def pytest_load_initial_conftests(early_config, args: list[str], parser):
new_args: list[str] = []
new_args.extend(generate_junit_flags())
new_args.extend(generate_full_junit_flags())
new_args.extend(generate_coverage_flags())
args[:] = [*args, *new_args]
35 changes: 34 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

import pytest

from pytest_nm_releng.plugin import generate_coverage_flags, generate_junit_flags
from pytest_nm_releng.plugin import (
generate_coverage_flags,
generate_full_junit_flags,
generate_junit_flags,
)
from tests.utils import setenv

EnvVarValue = Union[str, None]
Expand Down Expand Up @@ -51,6 +55,35 @@ def test_generate_coverage_flags_set(
assert result == expected_flags


@pytest.mark.parametrize(
"value",
[
pytest.param("vllm", id="value:vllm"),
pytest.param("", id="empty"),
pytest.param("1", id="1"),
pytest.param("0", id="0"),
pytest.param("2", id="2"),
pytest.param("-1", id="-1"),
pytest.param(None, id="unset"),
],
)
def test_generate_full_junit_flags(monkeypatch: pytest.MonkeyPatch, value: EnvVarValue):
setenv(monkeypatch, "NMRE_JUNIT_FULL", value)

if value == "1":
expected_flags = [
"-o",
"junit_logging=all",
"-o",
"junit_log_passing_tests=True",
]
else:
expected_flags = []

result = generate_full_junit_flags()
assert result == expected_flags


@pytest.mark.parametrize(
("env_junit_base", "env_junit_prefix"),
[
Expand Down
22 changes: 20 additions & 2 deletions tests/test_nm_releng.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ def test_plugin_adds_junit_args(
assert actual.startswith("results")


def test_plugin_adds_full_junit_args(
pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
):
"""Verify the plugin adds the expected junit args"""

monkeypatch.setenv("NMRE_JUNIT_FULL", "1")

cf = pytester.parseconfigure()
actual = cf.getoption("-o", None)
assert actual is not None
assert actual == ["junit_logging=all", "junit_log_passing_tests=True"]


@pytest.mark.skipif(not _pytest_cov_installed, reason="pytest-cov is required")
def test_plugin_adds_coverage_args(
pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
Expand All @@ -72,10 +85,15 @@ def test_plugin_adds_all_args(
"""Verify the plugin adds the expected coverage args"""

monkeypatch.setenv("NMRE_JUNIT_BASE", "results")
monkeypatch.setenv("NMRE_JUNIT_FULL", "1")
monkeypatch.setenv("NMRE_COV_NAME", "vllm")

cf = pytester.parseconfigure()

assert cf.getoption("--junit-xml", None).startswith("results")
assert "vllm" in cf.getoption("--cov", None)
assert cf.getoption("--junit-xml", "").startswith("results")
assert "vllm" in cf.getoption("--cov", [])
assert cf.getoption("-o", None) == [
"junit_logging=all",
"junit_log_passing_tests=True",
]
assert cf.getoption("--cov-append") is True
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ envlist = py39,py310,py311,py312

[testenv]
deps =
pytest >= 8
pytest ~= 8.3
pytest-cov ~= 6.0
commands =
pytest {posargs:tests}

Expand Down