From 73e4dae465c623d3b1d81cf27e499809b3b12ff7 Mon Sep 17 00:00:00 2001 From: Herrtian <70463940+Herrtian@users.noreply.github.com> Date: Wed, 20 May 2026 12:47:06 +0200 Subject: [PATCH] Support pathlike attachment files --- pytest_nunit/plugin.py | 8 ++++++- tests/integration/test_properties.py | 36 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/pytest_nunit/plugin.py b/pytest_nunit/plugin.py index f47b2b9..522cd78 100644 --- a/pytest_nunit/plugin.py +++ b/pytest_nunit/plugin.py @@ -26,6 +26,12 @@ ModuleReport = namedtuple("ModuleReport", "stats cases start stop duration") ParentlessNode = "PARENTLESS_NODE" + +def fsdecode(path): + if hasattr(os, "fsdecode"): + return os.fsdecode(path) + return path if isinstance(path, str) else str(path) + if sys.version_info < (3,): def min_with_default(seq, default): @@ -210,7 +216,7 @@ def add_attachment(self, file, description): r = self.nunit_xml.cases[self.id] if r["attachments"] is None: r["attachments"] = {} - r["attachments"][file] = description + r["attachments"][fsdecode(file)] = description def finalize(self): """Capture finalize stage (required).""" diff --git a/tests/integration/test_properties.py b/tests/integration/test_properties.py index 73e517f..da9c5e7 100644 --- a/tests/integration/test_properties.py +++ b/tests/integration/test_properties.py @@ -2,6 +2,7 @@ Test adding properties to tests """ import os +from pathlib import Path import xmlschema @@ -105,6 +106,41 @@ def test_basic(add_nunit_attachment): ) +def test_attachment_pathlike(testdir, tmpdir): + """ + Test an attachment path that implements os.PathLike + """ + testdir.makepyfile( + """ + from pathlib import Path + + + def test_basic(add_nunit_attachment): + add_nunit_attachment(Path("file.pth"), "desc") + assert 1 == 1 + """ + ) + outfile = tmpdir.join("out.xml") + outfile_pth = str(outfile) + + result = testdir.runpytest("-v", "--nunit-xml=" + outfile_pth) + result.stdout.fnmatch_lines(["*test_basic PASSED*"]) + assert result.ret == 0 + os.path.exists(outfile_pth) + xs = xmlschema.XMLSchema( + os.path.join( + os.path.abspath(os.path.dirname(__file__)), + "../../ext/nunit-src/TestResult.xsd", + ), + validation="lax", + ) + out = xs.to_dict(outfile_pth) + assert ( + out["test-suite"]["test-case"]["attachments"]["attachment"][0]["filePath"] + == str(Path("file.pth")) + ) + + def test_attachment_attach_on_any(testdir, tmpdir): """ Test that nunit_attach_on=any sets attachment properties