Skip to content

Fix: Use absolute path for the report #735

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

Merged
merged 12 commits into from
Sep 14, 2023
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ Versions follow `Semantic Versioning`_ (``<major>.<minor>.<patch>``).
Version History
---------------

4.0.2 (2023-09-12)
~~~~~~~~~~~~~~~~~~

* Use absolute path to the report file.

* Thanks to `@adrien-berchet <https://github.com/adrien-berchet>`_ for reporting and for the PR.

4.0.1 (2023-09-10)
~~~~~~~~~~~~~~~~~~

Expand Down
6 changes: 4 additions & 2 deletions src/pytest_html/basereport.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

class BaseReport:
def __init__(self, report_path, config, report_data, template, css):
self._report_path = Path(os.path.expandvars(report_path)).expanduser()
self._report_path = (
Path.cwd() / Path(os.path.expandvars(report_path)).expanduser()
)
self._report_path.parent.mkdir(parents=True, exist_ok=True)
self._config = config
self._template = template
Expand Down Expand Up @@ -186,7 +188,7 @@ def pytest_sessionfinish(self, session):
def pytest_terminal_summary(self, terminalreporter):
terminalreporter.write_sep(
"-",
f"Generated html report: file://{self._report_path.resolve().as_posix()}",
f"Generated html report: {self._report_path.as_uri()}",
)

@pytest.hookimpl(trylast=True)
Expand Down
22 changes: 22 additions & 0 deletions testing/test_unit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import importlib.resources
import os
import sys
from pathlib import Path

import pkg_resources
import pytest
Expand Down Expand Up @@ -70,6 +71,27 @@ def pytest_html_results_summary(prefix, summary, postfix, session):
result.assert_outcomes(passed=1)


def test_chdir(pytester):
pytester.makepyfile(
"""
import pytest

@pytest.fixture
def changing_dir(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)

def test_function(changing_dir):
pass
"""
)
report_path = Path("reports") / "report.html"
page = pytester.runpytest("--html", str(report_path))
assert page.ret == 0
assert (
f"Generated html report: {(pytester.path / report_path).as_uri()}"
) in page.outlines[-2]


@pytest.fixture
def css_file_path(pytester):
css_one = """
Expand Down