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
17 changes: 15 additions & 2 deletions pytest_nunit/nunit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
FailureType, PropertyBagType, PropertyType,
ReasonType, TestCaseElementType, TestFilterType,
TestResultType, TestRunStateType, TestRunType,
TestStatusType, TestSuiteElementType,
TestStatusType, TestSuiteElementType, SettingsType,
SettingType,
TestSuiteTypeType, ValueMatchFilterType)

FRAMEWORK_VERSION = "3.6.2" # Nunit version this was based on
Expand Down Expand Up @@ -116,6 +117,18 @@ def _format_filters(filters_):
)


def _format_settings(settings):
if not settings:
return None

return SettingsType(
setting=[
SettingType(item=None, name=name, value=value)
for name, value in sorted(settings.items())
]
)


def _getlocale():
# See https://github.com/pytest-dev/pytest-nunit/pull/73
with warnings.catch_warnings():
Expand Down Expand Up @@ -212,7 +225,7 @@ def test_suites(self):
property=[PropertyType(name="python_version", value=sys.version)]
),
environment=self.environment,
settings=None,
settings=_format_settings(self.nunitxml.settings),
failure=None,
reason=None,
output=None,
Expand Down
23 changes: 23 additions & 0 deletions pytest_nunit/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ def max_with_default(seq, default):
max_with_default = max


def _stringify_setting(value):
if value is None:
return ""
if isinstance(value, (list, tuple)):
return " ".join(str(item) for item in value)
return str(value)


def _collect_runtime_settings(config):
rootdir = getattr(config, "rootpath", getattr(config, "rootdir", ""))
inifile = getattr(config, "inipath", getattr(config, "inifile", None))
settings = {
"rootdir": rootdir,
"inifile": inifile,
"nunit_suite_name": config.getini("nunit_suite_name"),
"nunit_attach_on": config.getini("nunit_attach_on"),
}
return {name: _stringify_setting(value) for name, value in settings.items()}


def pytest_addoption(parser):
"""Allow export settings on CLI."""
group = parser.getgroup("terminal reporting")
Expand Down Expand Up @@ -111,6 +131,7 @@ def pytest_configure(config):
show_user_domain=config.getini("nunit_show_user_domain"),
attach_on=config.getini("nunit_attach_on"),
filters=filters,
settings=_collect_runtime_settings(config),
)
config.pluginmanager.register(config._nunitxml)

Expand Down Expand Up @@ -267,6 +288,7 @@ def __init__(
show_user_domain=False,
attach_on="any",
filters=None,
settings=None,
):
logfile = os.path.expanduser(os.path.expandvars(logfile))
self.logfile = os.path.normpath(os.path.abspath(logfile))
Expand All @@ -284,6 +306,7 @@ def __init__(
log.debug("Attach on criteria : %s", attach_on)
self.idrefindex = 100 # Create a unique ID counter
self.filters = filters
self.settings = settings or {}

self.node_descriptions = defaultdict(str)
self.module_descriptions = defaultdict(str)
Expand Down
43 changes: 43 additions & 0 deletions tests/integration/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,46 @@ def test_basic():
assert out["test-suite"]["@failed"] == 0
assert out["test-suite"]["@skipped"] == 0
assert out["test-suite"]["test-case"]["@name"] == "test1.test_prefix.py::test_basic"


def test_runtime_settings(testdir, tmpdir):
"""
Test that pytest runtime settings are included in suite metadata.
"""
testdir.makeini(
"""
[pytest]
nunit_suite_name = Configured suite
nunit_attach_on = fail
"""
)
testdir.makepyfile(
"""
def test_basic():
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

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)
settings = {
setting["@name"]: setting["@value"]
for setting in out["test-suite"]["settings"]["setting"]
}

assert settings["nunit_suite_name"] == "Configured suite"
assert settings["nunit_attach_on"] == "fail"
assert settings["rootdir"] == str(testdir.tmpdir)
assert settings["inifile"].endswith(".ini")