Skip to content
Open
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
8 changes: 7 additions & 1 deletion pytest_nunit/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)."""
Expand Down
36 changes: 36 additions & 0 deletions tests/integration/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Test adding properties to tests
"""
import os
from pathlib import Path

import xmlschema

Expand Down Expand Up @@ -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
Expand Down