2024-01-28 21:12:42 +08:00
|
|
|
# mypy: allow-untyped-defs
|
2019-08-03 22:11:36 +08:00
|
|
|
from datetime import datetime
|
2018-10-25 15:01:29 +08:00
|
|
|
import os
|
2020-10-03 23:08:14 +08:00
|
|
|
from pathlib import Path
|
2019-08-03 22:11:36 +08:00
|
|
|
import platform
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import cast
|
|
|
|
from typing import List
|
2020-12-16 12:16:05 +08:00
|
|
|
from typing import Optional
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import Tuple
|
2020-10-03 03:09:56 +08:00
|
|
|
from typing import TYPE_CHECKING
|
2020-12-16 12:16:05 +08:00
|
|
|
from typing import Union
|
2009-12-31 18:25:07 +08:00
|
|
|
from xml.dom import minidom
|
2018-10-25 15:01:29 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
import xmlschema
|
2015-09-26 14:11:23 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
from _pytest.config import Config
|
2020-04-09 21:36:59 +08:00
|
|
|
from _pytest.junitxml import bin_xml_escape
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.junitxml import LogXML
|
2020-12-16 12:16:05 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
|
|
|
from _pytest.pytester import Pytester
|
|
|
|
from _pytest.pytester import RunResult
|
2018-06-27 03:17:36 +08:00
|
|
|
from _pytest.reports import BaseReport
|
2020-05-01 19:40:17 +08:00
|
|
|
from _pytest.reports import TestReport
|
2021-07-18 18:51:09 +08:00
|
|
|
from _pytest.stash import Stash
|
2018-10-25 15:01:29 +08:00
|
|
|
import pytest
|
2018-06-27 03:17:36 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@pytest.fixture(scope="session")
|
2020-12-16 12:16:05 +08:00
|
|
|
def schema() -> xmlschema.XMLSchema:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Return an xmlschema.XMLSchema object for the junit-10.xsd file."""
|
2019-07-13 02:14:04 +08:00
|
|
|
fn = Path(__file__).parent / "example_scripts/junit-10.xsd"
|
2023-06-20 19:55:40 +08:00
|
|
|
with fn.open(encoding="utf-8") as f:
|
2019-07-13 02:14:04 +08:00
|
|
|
return xmlschema.XMLSchema(f)
|
|
|
|
|
|
|
|
|
2020-12-19 04:36:20 +08:00
|
|
|
class RunAndParse:
|
|
|
|
def __init__(self, pytester: Pytester, schema: xmlschema.XMLSchema) -> None:
|
|
|
|
self.pytester = pytester
|
|
|
|
self.schema = schema
|
2019-07-13 02:14:04 +08:00
|
|
|
|
2020-12-19 04:36:20 +08:00
|
|
|
def __call__(
|
|
|
|
self, *args: Union[str, "os.PathLike[str]"], family: Optional[str] = "xunit1"
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> Tuple[RunResult, "DomNode"]:
|
2019-07-13 02:14:04 +08:00
|
|
|
if family:
|
2024-02-02 22:12:50 +08:00
|
|
|
args = ("-o", "junit_family=" + family, *args)
|
2020-12-19 04:36:20 +08:00
|
|
|
xml_path = self.pytester.path.joinpath("junit.xml")
|
|
|
|
result = self.pytester.runpytest("--junitxml=%s" % xml_path, *args)
|
2019-07-13 02:14:04 +08:00
|
|
|
if family == "xunit2":
|
2023-06-20 19:55:40 +08:00
|
|
|
with xml_path.open(encoding="utf-8") as f:
|
2020-12-19 04:36:20 +08:00
|
|
|
self.schema.validate(f)
|
2019-07-13 02:14:04 +08:00
|
|
|
xmldoc = minidom.parse(str(xml_path))
|
|
|
|
return result, DomNode(xmldoc)
|
|
|
|
|
2020-12-19 04:36:20 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def run_and_parse(pytester: Pytester, schema: xmlschema.XMLSchema) -> RunAndParse:
|
|
|
|
"""Fixture that returns a function that can be used to execute pytest and
|
|
|
|
return the parsed ``DomNode`` of the root xml node.
|
|
|
|
|
|
|
|
The ``family`` parameter is used to configure the ``junit_family`` of the written report.
|
|
|
|
"xunit2" is also automatically validated against the schema.
|
|
|
|
"""
|
|
|
|
return RunAndParse(pytester, schema)
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
def assert_attr(node, **kwargs):
|
2010-11-06 16:58:04 +08:00
|
|
|
__tracebackhide__ = True
|
2015-12-17 02:07:36 +08:00
|
|
|
|
2015-10-10 15:31:52 +08:00
|
|
|
def nodeval(node, name):
|
|
|
|
anode = node.getAttributeNode(name)
|
|
|
|
if anode is not None:
|
|
|
|
return anode.value
|
|
|
|
|
2015-10-10 06:40:50 +08:00
|
|
|
expected = {name: str(value) for name, value in kwargs.items()}
|
2015-10-10 15:31:52 +08:00
|
|
|
on_node = {name: nodeval(node, name) for name in expected}
|
2015-10-10 06:40:50 +08:00
|
|
|
assert on_node == expected
|
|
|
|
|
|
|
|
|
|
|
|
class DomNode:
|
|
|
|
def __init__(self, dom):
|
|
|
|
self.__node = dom
|
|
|
|
|
2015-10-10 20:39:25 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return self.__node.toxml()
|
|
|
|
|
2015-10-10 06:40:50 +08:00
|
|
|
def find_first_by_tag(self, tag):
|
|
|
|
return self.find_nth_by_tag(tag, 0)
|
|
|
|
|
|
|
|
def _by_tag(self, tag):
|
|
|
|
return self.__node.getElementsByTagName(tag)
|
|
|
|
|
2019-07-04 07:34:12 +08:00
|
|
|
@property
|
|
|
|
def children(self):
|
|
|
|
return [type(self)(x) for x in self.__node.childNodes]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def get_unique_child(self):
|
|
|
|
children = self.children
|
|
|
|
assert len(children) == 1
|
|
|
|
return children[0]
|
|
|
|
|
2015-10-10 06:40:50 +08:00
|
|
|
def find_nth_by_tag(self, tag, n):
|
|
|
|
items = self._by_tag(tag)
|
|
|
|
try:
|
|
|
|
nth = items[n]
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
return type(self)(nth)
|
|
|
|
|
|
|
|
def find_by_tag(self, tag):
|
|
|
|
t = type(self)
|
2015-10-11 01:47:21 +08:00
|
|
|
return [t(x) for x in self.__node.getElementsByTagName(tag)]
|
2015-10-10 06:40:50 +08:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
node = self.__node.getAttributeNode(key)
|
|
|
|
if node is not None:
|
|
|
|
return node.value
|
|
|
|
|
|
|
|
def assert_attr(self, **kwargs):
|
2015-10-11 01:35:58 +08:00
|
|
|
__tracebackhide__ = True
|
2015-10-10 06:40:50 +08:00
|
|
|
return assert_attr(self.__node, **kwargs)
|
|
|
|
|
|
|
|
def toxml(self):
|
|
|
|
return self.__node.toxml()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def text(self):
|
|
|
|
return self.__node.childNodes[0].wholeText
|
|
|
|
|
|
|
|
@property
|
|
|
|
def tag(self):
|
|
|
|
return self.__node.tagName
|
|
|
|
|
|
|
|
@property
|
2019-07-04 07:34:12 +08:00
|
|
|
def next_sibling(self):
|
2015-10-10 06:40:50 +08:00
|
|
|
return type(self)(self.__node.nextSibling)
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
parametrize_families = pytest.mark.parametrize("xunit_family", ["xunit1", "xunit2"])
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestPython:
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_summing_simple(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2009-12-31 18:25:07 +08:00
|
|
|
"""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2009-12-31 18:25:07 +08:00
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
def test_fail():
|
|
|
|
assert 0
|
|
|
|
def test_skip():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.skip("")
|
|
|
|
@pytest.mark.xfail
|
2010-05-20 20:35:13 +08:00
|
|
|
def test_xfail():
|
|
|
|
assert 0
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.xfail
|
2010-05-20 20:35:13 +08:00
|
|
|
def test_xpass():
|
|
|
|
assert 1
|
2009-12-31 18:25:07 +08:00
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2018-12-02 15:45:31 +08:00
|
|
|
node.assert_attr(name="pytest", errors=0, failures=1, skipped=2, tests=5)
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_summing_simple_with_errors(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2016-08-08 20:29:20 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fixture():
|
|
|
|
raise Exception()
|
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
def test_fail():
|
|
|
|
assert 0
|
|
|
|
def test_error(fixture):
|
|
|
|
pass
|
|
|
|
@pytest.mark.xfail
|
2016-08-18 05:32:27 +08:00
|
|
|
def test_xfail():
|
|
|
|
assert False
|
|
|
|
@pytest.mark.xfail(strict=True)
|
2016-08-08 20:29:20 +08:00
|
|
|
def test_xpass():
|
2016-08-18 05:32:27 +08:00
|
|
|
assert True
|
2016-08-08 20:29:20 +08:00
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2016-08-08 20:29:20 +08:00
|
|
|
assert result.ret
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2018-12-02 15:45:31 +08:00
|
|
|
node.assert_attr(name="pytest", errors=1, failures=2, skipped=1, tests=5)
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_hostname_in_xml(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-08-03 22:11:36 +08:00
|
|
|
"""
|
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2019-08-03 22:11:36 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
node.assert_attr(hostname=platform.node())
|
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_timestamp_in_xml(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-08-03 22:11:36 +08:00
|
|
|
"""
|
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
start_time = datetime.now()
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2019-08-03 22:11:36 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
timestamp = datetime.strptime(node["timestamp"], "%Y-%m-%dT%H:%M:%S.%f")
|
|
|
|
assert start_time <= timestamp < datetime.now()
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_timing_function(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, mock_timing
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2011-05-28 20:03:10 +08:00
|
|
|
"""
|
2020-05-23 03:10:51 +08:00
|
|
|
from _pytest import timing
|
2015-05-01 19:55:52 +08:00
|
|
|
def setup_module():
|
2020-05-23 03:10:51 +08:00
|
|
|
timing.sleep(1)
|
2015-05-01 19:55:52 +08:00
|
|
|
def teardown_module():
|
2020-05-23 03:10:51 +08:00
|
|
|
timing.sleep(2)
|
2011-05-28 20:03:10 +08:00
|
|
|
def test_sleep():
|
2020-05-23 03:10:51 +08:00
|
|
|
timing.sleep(4)
|
2011-05-28 20:03:10 +08:00
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse()
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
val = tnode["time"]
|
2020-05-23 03:10:51 +08:00
|
|
|
assert float(val) == 7.0
|
2011-05-28 20:03:10 +08:00
|
|
|
|
2018-12-20 03:11:20 +08:00
|
|
|
@pytest.mark.parametrize("duration_report", ["call", "total"])
|
2019-07-13 02:14:04 +08:00
|
|
|
def test_junit_duration_report(
|
2020-12-16 12:16:05 +08:00
|
|
|
self,
|
|
|
|
pytester: Pytester,
|
|
|
|
monkeypatch: MonkeyPatch,
|
2020-12-19 04:36:20 +08:00
|
|
|
duration_report: str,
|
|
|
|
run_and_parse: RunAndParse,
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
2018-12-20 03:11:20 +08:00
|
|
|
# mock LogXML.node_reporter so it always sets a known duration to each test report object
|
|
|
|
original_node_reporter = LogXML.node_reporter
|
|
|
|
|
|
|
|
def node_reporter_wrapper(s, report):
|
|
|
|
report.duration = 1.0
|
|
|
|
reporter = original_node_reporter(s, report)
|
|
|
|
return reporter
|
|
|
|
|
|
|
|
monkeypatch.setattr(LogXML, "node_reporter", node_reporter_wrapper)
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2018-12-12 00:29:31 +08:00
|
|
|
"""
|
2018-12-20 03:11:20 +08:00
|
|
|
def test_foo():
|
|
|
|
pass
|
2018-12-12 00:29:31 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-10-03 04:16:22 +08:00
|
|
|
result, dom = run_and_parse("-o", f"junit_duration_report={duration_report}")
|
2018-12-12 00:29:31 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
2018-12-20 03:11:20 +08:00
|
|
|
val = float(tnode["time"])
|
|
|
|
if duration_report == "total":
|
|
|
|
assert val == 3.0
|
|
|
|
else:
|
|
|
|
assert duration_report == "call"
|
|
|
|
assert val == 1.0
|
2018-12-12 00:29:31 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_setup_error(
|
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
|
|
|
) -> None:
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2009-12-31 18:25:07 +08:00
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg(request):
|
2020-06-24 00:03:46 +08:00
|
|
|
raise ValueError("Error reason")
|
2009-12-31 18:25:07 +08:00
|
|
|
def test_function(arg):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2016-08-08 20:29:20 +08:00
|
|
|
node.assert_attr(errors=1, tests=1)
|
2015-10-10 06:40:50 +08:00
|
|
|
tnode = node.find_first_by_tag("testcase")
|
2018-12-02 15:45:31 +08:00
|
|
|
tnode.assert_attr(classname="test_setup_error", name="test_function")
|
2015-10-10 06:40:50 +08:00
|
|
|
fnode = tnode.find_first_by_tag("error")
|
2020-06-24 00:03:46 +08:00
|
|
|
fnode.assert_attr(message='failed on setup with "ValueError: Error reason"')
|
2009-12-31 18:25:07 +08:00
|
|
|
assert "ValueError" in fnode.toxml()
|
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_teardown_error(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2016-11-02 20:45:40 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg():
|
|
|
|
yield
|
2020-06-24 00:03:46 +08:00
|
|
|
raise ValueError('Error reason')
|
2016-11-02 20:45:40 +08:00
|
|
|
def test_function(arg):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2016-11-02 20:45:40 +08:00
|
|
|
assert result.ret
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
2018-12-02 15:45:31 +08:00
|
|
|
tnode.assert_attr(classname="test_teardown_error", name="test_function")
|
2016-11-02 20:45:40 +08:00
|
|
|
fnode = tnode.find_first_by_tag("error")
|
2020-06-24 00:03:46 +08:00
|
|
|
fnode.assert_attr(message='failed on teardown with "ValueError: Error reason"')
|
2016-11-02 20:45:40 +08:00
|
|
|
assert "ValueError" in fnode.toxml()
|
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_call_failure_teardown_error(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2017-02-03 17:30:28 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg():
|
|
|
|
yield
|
|
|
|
raise Exception("Teardown Exception")
|
|
|
|
def test_function(arg):
|
|
|
|
raise Exception("Call Exception")
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2017-02-03 17:30:28 +08:00
|
|
|
assert result.ret
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
node.assert_attr(errors=1, failures=1, tests=1)
|
|
|
|
first, second = dom.find_by_tag("testcase")
|
2020-07-23 20:31:14 +08:00
|
|
|
assert first
|
|
|
|
assert second
|
|
|
|
assert first != second
|
2017-02-03 17:30:28 +08:00
|
|
|
fnode = first.find_first_by_tag("failure")
|
|
|
|
fnode.assert_attr(message="Exception: Call Exception")
|
|
|
|
snode = second.find_first_by_tag("error")
|
2020-06-24 00:03:46 +08:00
|
|
|
snode.assert_attr(
|
|
|
|
message='failed on teardown with "Exception: Teardown Exception"'
|
|
|
|
)
|
2017-02-03 17:30:28 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_skip_contains_name_reason(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2011-03-20 00:59:07 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
def test_skip():
|
|
|
|
pytest.skip("hello23")
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2011-03-20 00:59:07 +08:00
|
|
|
assert result.ret == 0
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2018-12-02 15:45:31 +08:00
|
|
|
node.assert_attr(skipped=1)
|
2015-10-10 06:40:50 +08:00
|
|
|
tnode = node.find_first_by_tag("testcase")
|
2018-12-02 15:45:31 +08:00
|
|
|
tnode.assert_attr(classname="test_skip_contains_name_reason", name="test_skip")
|
2015-10-10 06:40:50 +08:00
|
|
|
snode = tnode.find_first_by_tag("skipped")
|
|
|
|
snode.assert_attr(type="pytest.skip", message="hello23")
|
2011-03-20 00:59:07 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_mark_skip_contains_name_reason(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2016-04-11 01:57:45 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.skip(reason="hello24")
|
|
|
|
def test_skip():
|
|
|
|
assert True
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2016-04-11 01:57:45 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2018-12-02 15:45:31 +08:00
|
|
|
node.assert_attr(skipped=1)
|
2016-04-11 01:57:45 +08:00
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(
|
2018-12-02 15:45:31 +08:00
|
|
|
classname="test_mark_skip_contains_name_reason", name="test_skip"
|
2016-04-11 01:57:45 +08:00
|
|
|
)
|
|
|
|
snode = tnode.find_first_by_tag("skipped")
|
|
|
|
snode.assert_attr(type="pytest.skip", message="hello24")
|
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
|
|
|
def test_mark_skipif_contains_name_reason(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2016-04-11 01:57:45 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
GLOBAL_CONDITION = True
|
|
|
|
@pytest.mark.skipif(GLOBAL_CONDITION, reason="hello25")
|
|
|
|
def test_skip():
|
|
|
|
assert True
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2016-04-11 01:57:45 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2018-12-02 15:45:31 +08:00
|
|
|
node.assert_attr(skipped=1)
|
2016-04-11 01:57:45 +08:00
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(
|
2018-12-02 15:45:31 +08:00
|
|
|
classname="test_mark_skipif_contains_name_reason", name="test_skip"
|
2016-04-11 01:57:45 +08:00
|
|
|
)
|
|
|
|
snode = tnode.find_first_by_tag("skipped")
|
|
|
|
snode.assert_attr(type="pytest.skip", message="hello25")
|
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
|
|
|
def test_mark_skip_doesnt_capture_output(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2016-11-27 01:47:26 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.skip(reason="foo")
|
|
|
|
def test_skip():
|
|
|
|
print("bar!")
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2016-11-27 01:47:26 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
node_xml = dom.find_first_by_tag("testsuite").toxml()
|
|
|
|
assert "bar!" not in node_xml
|
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_classname_instance(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2010-01-12 08:35:50 +08:00
|
|
|
"""
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2010-01-12 08:35:50 +08:00
|
|
|
def test_method(self):
|
|
|
|
assert 0
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
node.assert_attr(failures=1)
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(
|
2018-12-02 15:45:31 +08:00
|
|
|
classname="test_classname_instance.TestClass", name="test_method"
|
2015-10-10 06:40:50 +08:00
|
|
|
)
|
2010-01-12 08:35:50 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_classname_nested_dir(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
p = pytester.mkdir("sub").joinpath("test_hello.py")
|
2023-06-20 19:55:40 +08:00
|
|
|
p.write_text("def test_func(): 0/0", encoding="utf-8")
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2010-11-06 16:58:04 +08:00
|
|
|
assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
node.assert_attr(failures=1)
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
2018-12-02 15:45:31 +08:00
|
|
|
tnode.assert_attr(classname="sub.test_hello", name="test_func")
|
2010-11-06 16:58:04 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_internal_error(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makeconftest("def pytest_runtest_protocol(): 0 / 0")
|
|
|
|
pytester.makepyfile("def test_function(): pass")
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2016-08-08 20:29:20 +08:00
|
|
|
node.assert_attr(errors=1, tests=1)
|
2015-10-10 06:40:50 +08:00
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(classname="pytest", name="internal")
|
|
|
|
fnode = tnode.find_first_by_tag("error")
|
|
|
|
fnode.assert_attr(message="internal error")
|
2009-12-31 18:25:07 +08:00
|
|
|
assert "Division" in fnode.toxml()
|
|
|
|
|
2020-01-16 15:14:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"junit_logging", ["no", "log", "system-out", "system-err", "out-err", "all"]
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
|
|
|
def test_failure_function(
|
2020-12-19 04:36:20 +08:00
|
|
|
self,
|
|
|
|
pytester: Pytester,
|
|
|
|
junit_logging,
|
|
|
|
run_and_parse: RunAndParse,
|
|
|
|
xunit_family,
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2011-07-13 05:09:03 +08:00
|
|
|
"""
|
2018-01-26 16:28:23 +08:00
|
|
|
import logging
|
2011-07-13 05:09:03 +08:00
|
|
|
import sys
|
2018-01-26 16:28:23 +08:00
|
|
|
|
2011-07-13 05:09:03 +08:00
|
|
|
def test_fail():
|
2018-11-22 16:15:14 +08:00
|
|
|
print("hello-stdout")
|
2011-07-13 05:09:03 +08:00
|
|
|
sys.stderr.write("hello-stderr\\n")
|
2018-01-26 16:28:23 +08:00
|
|
|
logging.info('info msg')
|
|
|
|
logging.warning('warning msg')
|
2011-07-13 05:09:03 +08:00
|
|
|
raise ValueError(42)
|
|
|
|
"""
|
|
|
|
)
|
2012-06-23 17:32:32 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(
|
|
|
|
"-o", "junit_logging=%s" % junit_logging, family=xunit_family
|
|
|
|
)
|
2020-01-16 15:14:46 +08:00
|
|
|
assert result.ret, "Expected ret > 0"
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
node.assert_attr(failures=1, tests=1)
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
2018-12-02 15:45:31 +08:00
|
|
|
tnode.assert_attr(classname="test_failure_function", name="test_fail")
|
2015-10-10 06:40:50 +08:00
|
|
|
fnode = tnode.find_first_by_tag("failure")
|
|
|
|
fnode.assert_attr(message="ValueError: 42")
|
2020-01-16 15:14:46 +08:00
|
|
|
assert "ValueError" in fnode.toxml(), "ValueError not included"
|
|
|
|
|
|
|
|
if junit_logging in ["log", "all"]:
|
|
|
|
logdata = tnode.find_first_by_tag("system-out")
|
|
|
|
log_xml = logdata.toxml()
|
|
|
|
assert logdata.tag == "system-out", "Expected tag: system-out"
|
|
|
|
assert "info msg" not in log_xml, "Unexpected INFO message"
|
|
|
|
assert "warning msg" in log_xml, "Missing WARN message"
|
|
|
|
if junit_logging in ["system-out", "out-err", "all"]:
|
|
|
|
systemout = tnode.find_first_by_tag("system-out")
|
|
|
|
systemout_xml = systemout.toxml()
|
|
|
|
assert systemout.tag == "system-out", "Expected tag: system-out"
|
|
|
|
assert "info msg" not in systemout_xml, "INFO message found in system-out"
|
|
|
|
assert (
|
|
|
|
"hello-stdout" in systemout_xml
|
|
|
|
), "Missing 'hello-stdout' in system-out"
|
|
|
|
if junit_logging in ["system-err", "out-err", "all"]:
|
|
|
|
systemerr = tnode.find_first_by_tag("system-err")
|
|
|
|
systemerr_xml = systemerr.toxml()
|
|
|
|
assert systemerr.tag == "system-err", "Expected tag: system-err"
|
|
|
|
assert "info msg" not in systemerr_xml, "INFO message found in system-err"
|
|
|
|
assert (
|
|
|
|
"hello-stderr" in systemerr_xml
|
|
|
|
), "Missing 'hello-stderr' in system-err"
|
|
|
|
assert (
|
|
|
|
"warning msg" not in systemerr_xml
|
|
|
|
), "WARN message found in system-err"
|
|
|
|
if junit_logging == "no":
|
|
|
|
assert not tnode.find_by_tag("log"), "Found unexpected content: log"
|
|
|
|
assert not tnode.find_by_tag(
|
|
|
|
"system-out"
|
|
|
|
), "Found unexpected content: system-out"
|
|
|
|
assert not tnode.find_by_tag(
|
|
|
|
"system-err"
|
|
|
|
), "Found unexpected content: system-err"
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_failure_verbose_message(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2015-01-20 06:45:26 +08:00
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
def test_fail():
|
|
|
|
assert 0, "An error"
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
fnode = tnode.find_first_by_tag("failure")
|
2020-07-23 20:31:14 +08:00
|
|
|
fnode.assert_attr(message="AssertionError: An error\nassert 0")
|
2015-01-20 06:45:26 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_failure_escape(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2010-06-09 21:27:45 +08:00
|
|
|
"""
|
2012-08-17 22:08:08 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('arg1', "<&'", ids="<&'")
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_func(arg1):
|
2012-08-29 04:37:43 +08:00
|
|
|
print(arg1)
|
2010-06-09 21:27:45 +08:00
|
|
|
assert 0
|
|
|
|
"""
|
|
|
|
)
|
2020-01-16 15:14:46 +08:00
|
|
|
result, dom = run_and_parse(
|
|
|
|
"-o", "junit_logging=system-out", family=xunit_family
|
|
|
|
)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
node.assert_attr(failures=3, tests=3)
|
2012-08-17 22:08:08 +08:00
|
|
|
|
|
|
|
for index, char in enumerate("<&'"):
|
2015-10-10 06:40:50 +08:00
|
|
|
tnode = node.find_nth_by_tag("testcase", index)
|
|
|
|
tnode.assert_attr(
|
2018-12-02 15:45:31 +08:00
|
|
|
classname="test_failure_escape", name="test_func[%s]" % char
|
2015-10-10 06:40:50 +08:00
|
|
|
)
|
|
|
|
sysout = tnode.find_first_by_tag("system-out")
|
|
|
|
text = sysout.text
|
2020-01-16 15:14:46 +08:00
|
|
|
assert "%s\n" % char in text
|
2012-08-17 22:08:08 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_junit_prefixing(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2010-06-09 22:18:47 +08:00
|
|
|
"""
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_func():
|
2010-06-09 22:18:47 +08:00
|
|
|
assert 0
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestHello(object):
|
2010-06-09 22:18:47 +08:00
|
|
|
def test_hello(self):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse("--junitprefix=xyz", family=xunit_family)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
node.assert_attr(failures=1, tests=2)
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
2018-12-02 15:45:31 +08:00
|
|
|
tnode.assert_attr(classname="xyz.test_junit_prefixing", name="test_func")
|
2015-10-10 06:40:50 +08:00
|
|
|
tnode = node.find_nth_by_tag("testcase", 1)
|
|
|
|
tnode.assert_attr(
|
2018-12-02 15:45:31 +08:00
|
|
|
classname="xyz.test_junit_prefixing.TestHello", name="test_hello"
|
2015-10-10 06:40:50 +08:00
|
|
|
)
|
2010-06-09 22:18:47 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_xfailure_function(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2010-05-20 20:35:13 +08:00
|
|
|
"""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-05-20 20:35:13 +08:00
|
|
|
def test_xfail():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.xfail("42")
|
2010-05-20 20:35:13 +08:00
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2018-12-02 15:45:31 +08:00
|
|
|
node.assert_attr(skipped=1, tests=1)
|
2015-10-10 06:40:50 +08:00
|
|
|
tnode = node.find_first_by_tag("testcase")
|
2018-12-02 15:45:31 +08:00
|
|
|
tnode.assert_attr(classname="test_xfailure_function", name="test_xfail")
|
2015-10-10 06:40:50 +08:00
|
|
|
fnode = tnode.find_first_by_tag("skipped")
|
2019-04-11 06:07:57 +08:00
|
|
|
fnode.assert_attr(type="pytest.xfail", message="42")
|
2010-05-20 20:35:13 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_xfailure_marker(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-04-11 06:07:57 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.xfail(reason="42")
|
|
|
|
def test_xfail():
|
|
|
|
assert False
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2019-04-11 06:07:57 +08:00
|
|
|
assert not result.ret
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
node.assert_attr(skipped=1, tests=1)
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(classname="test_xfailure_marker", name="test_xfail")
|
|
|
|
fnode = tnode.find_first_by_tag("skipped")
|
|
|
|
fnode.assert_attr(type="pytest.xfail", message="42")
|
|
|
|
|
2020-01-16 15:14:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"junit_logging", ["no", "log", "system-out", "system-err", "out-err", "all"]
|
|
|
|
)
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_xfail_captures_output_once(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, junit_logging: str, run_and_parse: RunAndParse
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-26 05:21:48 +08:00
|
|
|
"""
|
2018-05-24 10:34:45 +08:00
|
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.xfail()
|
|
|
|
def test_fail():
|
|
|
|
sys.stdout.write('XFAIL This is stdout')
|
|
|
|
sys.stderr.write('XFAIL This is stderr')
|
|
|
|
assert 0
|
2018-05-26 05:21:48 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-01-16 15:14:46 +08:00
|
|
|
result, dom = run_and_parse("-o", "junit_logging=%s" % junit_logging)
|
2018-05-24 10:34:45 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
2020-01-16 15:14:46 +08:00
|
|
|
if junit_logging in ["system-err", "out-err", "all"]:
|
|
|
|
assert len(tnode.find_by_tag("system-err")) == 1
|
|
|
|
else:
|
|
|
|
assert len(tnode.find_by_tag("system-err")) == 0
|
|
|
|
|
|
|
|
if junit_logging in ["log", "system-out", "out-err", "all"]:
|
|
|
|
assert len(tnode.find_by_tag("system-out")) == 1
|
|
|
|
else:
|
|
|
|
assert len(tnode.find_by_tag("system-out")) == 0
|
2018-05-24 11:15:28 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_xfailure_xpass(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2010-05-20 20:35:13 +08:00
|
|
|
"""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.xfail
|
2010-05-20 20:35:13 +08:00
|
|
|
def test_xpass():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2015-09-30 04:39:18 +08:00
|
|
|
# assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2018-12-02 15:45:31 +08:00
|
|
|
node.assert_attr(skipped=0, tests=1)
|
2015-10-10 06:40:50 +08:00
|
|
|
tnode = node.find_first_by_tag("testcase")
|
2018-12-02 15:45:31 +08:00
|
|
|
tnode.assert_attr(classname="test_xfailure_xpass", name="test_xpass")
|
2016-08-18 05:02:54 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_xfailure_xpass_strict(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2016-08-18 05:02:54 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.xfail(strict=True, reason="This needs to fail!")
|
|
|
|
def test_xpass():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2016-08-18 05:02:54 +08:00
|
|
|
# assert result.ret
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2018-12-02 15:45:31 +08:00
|
|
|
node.assert_attr(skipped=0, tests=1)
|
2016-08-18 05:02:54 +08:00
|
|
|
tnode = node.find_first_by_tag("testcase")
|
2018-12-02 15:45:31 +08:00
|
|
|
tnode.assert_attr(classname="test_xfailure_xpass_strict", name="test_xpass")
|
2016-08-18 05:02:54 +08:00
|
|
|
fnode = tnode.find_first_by_tag("failure")
|
|
|
|
fnode.assert_attr(message="[XPASS(strict)] This needs to fail!")
|
2010-05-20 20:35:13 +08:00
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_collect_error(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile("syntax error")
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2016-08-08 20:29:20 +08:00
|
|
|
node.assert_attr(errors=1, tests=1)
|
2015-10-10 06:40:50 +08:00
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
fnode = tnode.find_first_by_tag("error")
|
|
|
|
fnode.assert_attr(message="collection failure")
|
2010-04-28 21:24:38 +08:00
|
|
|
assert "SyntaxError" in fnode.toxml()
|
2009-12-31 18:25:07 +08:00
|
|
|
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_unicode(self, pytester: Pytester, run_and_parse: RunAndParse) -> None:
|
2010-04-27 21:15:43 +08:00
|
|
|
value = "hx\xc4\x85\xc4\x87\n"
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2019-06-03 06:40:34 +08:00
|
|
|
"""\
|
2012-10-19 16:53:28 +08:00
|
|
|
# coding: latin1
|
2010-04-27 21:15:43 +08:00
|
|
|
def test_hello():
|
2018-11-22 16:15:14 +08:00
|
|
|
print(%r)
|
2010-04-27 21:15:43 +08:00
|
|
|
assert 0
|
2019-06-03 06:40:34 +08:00
|
|
|
"""
|
2010-04-27 21:15:43 +08:00
|
|
|
% value
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse()
|
2010-04-27 21:15:43 +08:00
|
|
|
assert result.ret == 1
|
2015-10-10 06:40:50 +08:00
|
|
|
tnode = dom.find_first_by_tag("testcase")
|
|
|
|
fnode = tnode.find_first_by_tag("failure")
|
2019-05-28 07:31:52 +08:00
|
|
|
assert "hx" in fnode.toxml()
|
2010-04-27 21:15:43 +08:00
|
|
|
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_assertion_binchars(
|
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse
|
|
|
|
) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""This test did fail when the escaping wasn't strict."""
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2013-12-13 17:28:23 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
M1 = '\x01\x02\x03\x04'
|
|
|
|
M2 = '\x01\x02\x03\x05'
|
|
|
|
|
|
|
|
def test_str_compare():
|
|
|
|
assert M1 == M2
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse()
|
2013-12-16 18:51:04 +08:00
|
|
|
print(dom.toxml())
|
2013-12-13 17:28:23 +08:00
|
|
|
|
2020-01-16 15:14:46 +08:00
|
|
|
@pytest.mark.parametrize("junit_logging", ["no", "system-out"])
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_pass_captures_stdout(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, junit_logging: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-04-16 12:45:14 +08:00
|
|
|
"""
|
|
|
|
def test_pass():
|
|
|
|
print('hello-stdout')
|
|
|
|
"""
|
|
|
|
)
|
2020-01-16 15:14:46 +08:00
|
|
|
result, dom = run_and_parse("-o", "junit_logging=%s" % junit_logging)
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
pnode = node.find_first_by_tag("testcase")
|
2020-01-16 15:14:46 +08:00
|
|
|
if junit_logging == "no":
|
|
|
|
assert not node.find_by_tag(
|
|
|
|
"system-out"
|
|
|
|
), "system-out should not be generated"
|
|
|
|
if junit_logging == "system-out":
|
|
|
|
systemout = pnode.find_first_by_tag("system-out")
|
|
|
|
assert (
|
|
|
|
"hello-stdout" in systemout.toxml()
|
|
|
|
), "'hello-stdout' should be in system-out"
|
2013-04-16 12:45:14 +08:00
|
|
|
|
2020-01-16 15:14:46 +08:00
|
|
|
@pytest.mark.parametrize("junit_logging", ["no", "system-err"])
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_pass_captures_stderr(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, junit_logging: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2013-04-16 12:45:14 +08:00
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
def test_pass():
|
|
|
|
sys.stderr.write('hello-stderr')
|
|
|
|
"""
|
|
|
|
)
|
2020-01-16 15:14:46 +08:00
|
|
|
result, dom = run_and_parse("-o", "junit_logging=%s" % junit_logging)
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
pnode = node.find_first_by_tag("testcase")
|
2020-01-16 15:14:46 +08:00
|
|
|
if junit_logging == "no":
|
|
|
|
assert not node.find_by_tag(
|
|
|
|
"system-err"
|
|
|
|
), "system-err should not be generated"
|
|
|
|
if junit_logging == "system-err":
|
|
|
|
systemerr = pnode.find_first_by_tag("system-err")
|
|
|
|
assert (
|
|
|
|
"hello-stderr" in systemerr.toxml()
|
|
|
|
), "'hello-stderr' should be in system-err"
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("junit_logging", ["no", "system-out"])
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_setup_error_captures_stdout(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, junit_logging: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2016-01-21 01:13:01 +08:00
|
|
|
"""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg(request):
|
2016-01-21 01:13:01 +08:00
|
|
|
print('hello-stdout')
|
|
|
|
raise ValueError()
|
|
|
|
def test_function(arg):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-01-16 15:14:46 +08:00
|
|
|
result, dom = run_and_parse("-o", "junit_logging=%s" % junit_logging)
|
2016-01-21 01:13:01 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
pnode = node.find_first_by_tag("testcase")
|
2020-01-16 15:14:46 +08:00
|
|
|
if junit_logging == "no":
|
|
|
|
assert not node.find_by_tag(
|
|
|
|
"system-out"
|
|
|
|
), "system-out should not be generated"
|
|
|
|
if junit_logging == "system-out":
|
|
|
|
systemout = pnode.find_first_by_tag("system-out")
|
|
|
|
assert (
|
|
|
|
"hello-stdout" in systemout.toxml()
|
|
|
|
), "'hello-stdout' should be in system-out"
|
2016-01-21 01:13:01 +08:00
|
|
|
|
2020-01-16 15:14:46 +08:00
|
|
|
@pytest.mark.parametrize("junit_logging", ["no", "system-err"])
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_setup_error_captures_stderr(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, junit_logging: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2016-01-21 01:13:01 +08:00
|
|
|
"""
|
|
|
|
import sys
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg(request):
|
2016-01-21 01:13:01 +08:00
|
|
|
sys.stderr.write('hello-stderr')
|
|
|
|
raise ValueError()
|
|
|
|
def test_function(arg):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-01-16 15:14:46 +08:00
|
|
|
result, dom = run_and_parse("-o", "junit_logging=%s" % junit_logging)
|
2016-01-21 01:13:01 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
pnode = node.find_first_by_tag("testcase")
|
2020-01-16 15:14:46 +08:00
|
|
|
if junit_logging == "no":
|
|
|
|
assert not node.find_by_tag(
|
|
|
|
"system-err"
|
|
|
|
), "system-err should not be generated"
|
|
|
|
if junit_logging == "system-err":
|
|
|
|
systemerr = pnode.find_first_by_tag("system-err")
|
|
|
|
assert (
|
|
|
|
"hello-stderr" in systemerr.toxml()
|
|
|
|
), "'hello-stderr' should be in system-err"
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("junit_logging", ["no", "system-out"])
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_avoid_double_stdout(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, junit_logging: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2017-02-22 21:17:45 +08:00
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg(request):
|
|
|
|
yield
|
|
|
|
sys.stdout.write('hello-stdout teardown')
|
|
|
|
raise ValueError()
|
|
|
|
def test_function(arg):
|
|
|
|
sys.stdout.write('hello-stdout call')
|
|
|
|
"""
|
|
|
|
)
|
2020-01-16 15:14:46 +08:00
|
|
|
result, dom = run_and_parse("-o", "junit_logging=%s" % junit_logging)
|
2017-02-22 21:17:45 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
pnode = node.find_first_by_tag("testcase")
|
2020-01-16 15:14:46 +08:00
|
|
|
if junit_logging == "no":
|
|
|
|
assert not node.find_by_tag(
|
|
|
|
"system-out"
|
|
|
|
), "system-out should not be generated"
|
|
|
|
if junit_logging == "system-out":
|
|
|
|
systemout = pnode.find_first_by_tag("system-out")
|
|
|
|
assert "hello-stdout call" in systemout.toxml()
|
|
|
|
assert "hello-stdout teardown" in systemout.toxml()
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_mangle_test_address() -> None:
|
2016-03-01 23:49:55 +08:00
|
|
|
from _pytest.junitxml import mangle_test_address
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2021-11-13 20:03:44 +08:00
|
|
|
address = "::".join(["a/my.py.thing.py", "Class", "method", "[a-1-::]"])
|
2016-03-01 23:49:55 +08:00
|
|
|
newnames = mangle_test_address(address)
|
|
|
|
assert newnames == ["a.my.py.thing", "Class", "method", "[a-1-::]"]
|
2012-05-22 23:18:04 +08:00
|
|
|
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_dont_configure_on_workers(tmp_path: Path) -> None:
|
2020-10-06 09:13:05 +08:00
|
|
|
gotten: List[object] = []
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class FakeConfig:
|
2020-05-01 19:40:17 +08:00
|
|
|
if TYPE_CHECKING:
|
2020-06-12 20:13:52 +08:00
|
|
|
workerinput = None
|
2020-05-01 19:40:17 +08:00
|
|
|
|
2013-03-25 03:43:25 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.pluginmanager = self
|
|
|
|
self.option = self
|
2021-07-18 19:18:03 +08:00
|
|
|
self.stash = Stash()
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2017-05-13 04:52:50 +08:00
|
|
|
def getini(self, name):
|
|
|
|
return "pytest"
|
2017-05-12 09:45:20 +08:00
|
|
|
|
2013-03-25 03:43:25 +08:00
|
|
|
junitprefix = None
|
2020-12-16 12:16:05 +08:00
|
|
|
# XXX: shouldn't need tmp_path ?
|
|
|
|
xmlpath = str(tmp_path.joinpath("junix.xml"))
|
2013-03-25 03:43:25 +08:00
|
|
|
register = gotten.append
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
fake_config = cast(Config, FakeConfig())
|
2013-03-25 03:43:25 +08:00
|
|
|
from _pytest import junitxml
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2013-03-25 03:43:25 +08:00
|
|
|
junitxml.pytest_configure(fake_config)
|
|
|
|
assert len(gotten) == 1
|
2020-06-12 20:13:52 +08:00
|
|
|
FakeConfig.workerinput = None
|
2013-03-25 03:43:25 +08:00
|
|
|
junitxml.pytest_configure(fake_config)
|
|
|
|
assert len(gotten) == 1
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestNonPython:
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_summing_simple(
|
2020-12-19 04:36:20 +08:00
|
|
|
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makeconftest(
|
2009-12-31 18:25:07 +08:00
|
|
|
"""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
2021-12-03 20:14:09 +08:00
|
|
|
def pytest_collect_file(file_path, parent):
|
|
|
|
if file_path.suffix == ".xyz":
|
|
|
|
return MyItem.from_parent(name=file_path.name, parent=parent)
|
2010-11-13 16:05:11 +08:00
|
|
|
class MyItem(pytest.Item):
|
2009-12-31 18:25:07 +08:00
|
|
|
def runtest(self):
|
|
|
|
raise ValueError(42)
|
|
|
|
def repr_failure(self, excinfo):
|
|
|
|
return "custom item runtest failed"
|
|
|
|
"""
|
|
|
|
)
|
2023-06-20 19:55:40 +08:00
|
|
|
pytester.path.joinpath("myfile.xyz").write_text("hello", encoding="utf-8")
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert result.ret
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2018-12-02 15:45:31 +08:00
|
|
|
node.assert_attr(errors=0, failures=1, skipped=0, tests=1)
|
2015-10-10 06:40:50 +08:00
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(name="myfile.xyz")
|
|
|
|
fnode = tnode.find_first_by_tag("failure")
|
|
|
|
fnode.assert_attr(message="custom item runtest failed")
|
2009-12-31 18:25:07 +08:00
|
|
|
assert "custom item runtest failed" in fnode.toxml()
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2011-04-16 07:09:25 +08:00
|
|
|
|
2020-01-16 15:14:46 +08:00
|
|
|
@pytest.mark.parametrize("junit_logging", ["no", "system-out"])
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_nullbyte(pytester: Pytester, junit_logging: str) -> None:
|
2011-04-16 07:09:25 +08:00
|
|
|
# A null byte can not occur in XML (see section 2.2 of the spec)
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2011-04-16 07:09:25 +08:00
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
def test_print_nullbyte():
|
|
|
|
sys.stdout.write('Here the null -->' + chr(0) + '<--')
|
|
|
|
sys.stdout.write('In repr form -->' + repr(chr(0)) + '<--')
|
|
|
|
assert False
|
|
|
|
"""
|
|
|
|
)
|
2020-12-16 12:16:05 +08:00
|
|
|
xmlf = pytester.path.joinpath("junit.xml")
|
|
|
|
pytester.runpytest("--junitxml=%s" % xmlf, "-o", "junit_logging=%s" % junit_logging)
|
2023-06-20 19:55:40 +08:00
|
|
|
text = xmlf.read_text(encoding="utf-8")
|
2011-04-16 07:09:25 +08:00
|
|
|
assert "\x00" not in text
|
2020-01-16 15:14:46 +08:00
|
|
|
if junit_logging == "system-out":
|
|
|
|
assert "#x00" in text
|
|
|
|
if junit_logging == "no":
|
|
|
|
assert "#x00" not in text
|
2011-04-16 07:09:25 +08:00
|
|
|
|
|
|
|
|
2020-01-16 15:14:46 +08:00
|
|
|
@pytest.mark.parametrize("junit_logging", ["no", "system-out"])
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_nullbyte_replace(pytester: Pytester, junit_logging: str) -> None:
|
2011-04-16 07:09:25 +08:00
|
|
|
# Check if the null byte gets replaced
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2011-04-16 07:09:25 +08:00
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
def test_print_nullbyte():
|
|
|
|
sys.stdout.write('Here the null -->' + chr(0) + '<--')
|
|
|
|
sys.stdout.write('In repr form -->' + repr(chr(0)) + '<--')
|
|
|
|
assert False
|
|
|
|
"""
|
|
|
|
)
|
2020-12-16 12:16:05 +08:00
|
|
|
xmlf = pytester.path.joinpath("junit.xml")
|
|
|
|
pytester.runpytest("--junitxml=%s" % xmlf, "-o", "junit_logging=%s" % junit_logging)
|
2023-06-20 19:55:40 +08:00
|
|
|
text = xmlf.read_text(encoding="utf-8")
|
2020-01-16 15:14:46 +08:00
|
|
|
if junit_logging == "system-out":
|
|
|
|
assert "#x0" in text
|
|
|
|
if junit_logging == "no":
|
|
|
|
assert "#x0" not in text
|
2011-04-16 07:09:25 +08:00
|
|
|
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_invalid_xml_escape() -> None:
|
2011-04-16 07:09:25 +08:00
|
|
|
# Test some more invalid xml chars, the full range should be
|
2019-07-13 02:14:04 +08:00
|
|
|
# tested really but let's just test the edges of the ranges
|
|
|
|
# instead.
|
2011-04-16 07:09:25 +08:00
|
|
|
# XXX This only tests low unicode character points for now as
|
|
|
|
# there are some issues with the testing infrastructure for
|
|
|
|
# the higher ones.
|
|
|
|
# XXX Testing 0xD (\r) is tricky as it overwrites the just written
|
|
|
|
# line in the output, so we skip it too.
|
2015-09-30 04:39:18 +08:00
|
|
|
invalid = (
|
|
|
|
0x00,
|
|
|
|
0x1,
|
|
|
|
0xB,
|
|
|
|
0xC,
|
|
|
|
0xE,
|
|
|
|
0x19,
|
|
|
|
27, # issue #126
|
|
|
|
0xD800,
|
|
|
|
0xDFFF,
|
|
|
|
0xFFFE,
|
|
|
|
0x0FFFF,
|
|
|
|
) # , 0x110000)
|
|
|
|
valid = (0x9, 0xA, 0x20)
|
|
|
|
# 0xD, 0xD7FF, 0xE000, 0xFFFD, 0x10000, 0x10FFFF)
|
2012-06-23 17:32:32 +08:00
|
|
|
|
2011-04-16 07:09:25 +08:00
|
|
|
for i in invalid:
|
2020-07-23 20:31:14 +08:00
|
|
|
got = bin_xml_escape(chr(i))
|
2011-04-16 07:09:25 +08:00
|
|
|
if i <= 0xFF:
|
2012-03-09 20:12:18 +08:00
|
|
|
expected = "#x%02X" % i
|
2011-04-16 07:09:25 +08:00
|
|
|
else:
|
2012-03-09 20:12:18 +08:00
|
|
|
expected = "#x%04X" % i
|
|
|
|
assert got == expected
|
2011-04-16 07:09:25 +08:00
|
|
|
for i in valid:
|
2020-07-23 20:31:14 +08:00
|
|
|
assert chr(i) == bin_xml_escape(chr(i))
|
2011-05-27 13:54:03 +08:00
|
|
|
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_logxml_path_expansion(tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
|
|
|
|
home_tilde = Path(os.path.expanduser("~")).joinpath("test.xml")
|
|
|
|
xml_tilde = LogXML(Path("~", "test.xml"), None)
|
|
|
|
assert xml_tilde.logfile == str(home_tilde)
|
2011-05-27 13:54:03 +08:00
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
monkeypatch.setenv("HOME", str(tmp_path))
|
2012-10-08 19:42:31 +08:00
|
|
|
home_var = os.path.normpath(os.path.expandvars("$HOME/test.xml"))
|
2020-12-16 12:16:05 +08:00
|
|
|
xml_var = LogXML(Path("$HOME", "test.xml"), None)
|
|
|
|
assert xml_var.logfile == str(home_var)
|
2012-08-04 16:33:43 +08:00
|
|
|
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_logxml_changingdir(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2012-08-04 16:33:43 +08:00
|
|
|
"""
|
|
|
|
def test_func():
|
|
|
|
import os
|
|
|
|
os.chdir("a")
|
|
|
|
"""
|
|
|
|
)
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.mkdir("a")
|
|
|
|
result = pytester.runpytest("--junitxml=a/x.xml")
|
2012-08-04 16:33:43 +08:00
|
|
|
assert result.ret == 0
|
2020-12-16 12:16:05 +08:00
|
|
|
assert pytester.path.joinpath("a/x.xml").exists()
|
2012-08-04 16:33:43 +08:00
|
|
|
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_logxml_makedir(pytester: Pytester) -> None:
|
2015-06-17 11:04:25 +08:00
|
|
|
"""--junitxml should automatically create directories for the xml file"""
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2015-06-17 06:36:04 +08:00
|
|
|
"""
|
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-16 12:16:05 +08:00
|
|
|
result = pytester.runpytest("--junitxml=path/to/results.xml")
|
2015-06-17 06:36:04 +08:00
|
|
|
assert result.ret == 0
|
2020-12-16 12:16:05 +08:00
|
|
|
assert pytester.path.joinpath("path/to/results.xml").exists()
|
2015-06-17 06:36:04 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_logxml_check_isdir(pytester: Pytester) -> None:
|
2016-11-28 07:55:49 +08:00
|
|
|
"""Give an error if --junit-xml is a directory (#2089)"""
|
2020-12-16 12:16:05 +08:00
|
|
|
result = pytester.runpytest("--junit-xml=.")
|
2016-11-28 07:55:49 +08:00
|
|
|
result.stderr.fnmatch_lines(["*--junitxml must be a filename*"])
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_escaped_parametrized_names_xml(
|
|
|
|
pytester: Pytester, run_and_parse: RunAndParse
|
|
|
|
) -> None:
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2019-06-05 08:48:06 +08:00
|
|
|
"""\
|
2013-05-10 03:16:57 +08:00
|
|
|
import pytest
|
2019-06-05 08:48:06 +08:00
|
|
|
@pytest.mark.parametrize('char', ["\\x00"])
|
2013-05-10 03:16:57 +08:00
|
|
|
def test_func(char):
|
|
|
|
assert char
|
2019-06-05 08:48:06 +08:00
|
|
|
"""
|
2013-05-10 03:16:57 +08:00
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse()
|
2013-05-10 03:16:57 +08:00
|
|
|
assert result.ret == 0
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testcase")
|
2016-04-02 05:57:42 +08:00
|
|
|
node.assert_attr(name="test_func[\\x00]")
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2013-05-10 03:16:57 +08:00
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_double_colon_split_function_issue469(
|
2020-12-19 04:36:20 +08:00
|
|
|
pytester: Pytester, run_and_parse: RunAndParse
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2016-03-01 23:49:55 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('param', ["double::colon"])
|
|
|
|
def test_func(param):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse()
|
2016-03-01 23:49:55 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
node = dom.find_first_by_tag("testcase")
|
2016-03-03 17:12:56 +08:00
|
|
|
node.assert_attr(classname="test_double_colon_split_function_issue469")
|
|
|
|
node.assert_attr(name="test_func[double::colon]")
|
|
|
|
|
|
|
|
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_double_colon_split_method_issue469(
|
|
|
|
pytester: Pytester, run_and_parse: RunAndParse
|
|
|
|
) -> None:
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2016-03-03 17:12:56 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2016-03-03 17:12:56 +08:00
|
|
|
@pytest.mark.parametrize('param', ["double::colon"])
|
|
|
|
def test_func(self, param):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse()
|
2016-03-03 17:12:56 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
node = dom.find_first_by_tag("testcase")
|
|
|
|
node.assert_attr(classname="test_double_colon_split_method_issue469.TestClass")
|
2016-03-01 23:49:55 +08:00
|
|
|
node.assert_attr(name="test_func[double::colon]")
|
|
|
|
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_unicode_issue368(pytester: Pytester) -> None:
|
|
|
|
path = pytester.path.joinpath("test.xml")
|
2013-10-21 22:54:25 +08:00
|
|
|
log = LogXML(str(path), None)
|
2018-08-23 10:21:00 +08:00
|
|
|
ustr = "ВНИ!"
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2014-03-14 19:49:34 +08:00
|
|
|
class Report(BaseReport):
|
2013-10-22 17:26:29 +08:00
|
|
|
longrepr = ustr
|
2020-10-06 09:13:05 +08:00
|
|
|
sections: List[Tuple[str, str]] = []
|
2013-10-21 22:54:25 +08:00
|
|
|
nodeid = "something"
|
2015-07-01 07:30:20 +08:00
|
|
|
location = "tests/filename.py", 42, "TestClass.method"
|
2020-10-06 09:13:05 +08:00
|
|
|
when = "teardown"
|
2015-09-30 04:39:18 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
test_report = cast(TestReport, Report())
|
2013-10-21 22:54:25 +08:00
|
|
|
|
|
|
|
# hopefully this is not too brittle ...
|
|
|
|
log.pytest_sessionstart()
|
2015-10-11 01:24:21 +08:00
|
|
|
node_reporter = log._opentestcase(test_report)
|
|
|
|
node_reporter.append_failure(test_report)
|
|
|
|
node_reporter.append_collect_error(test_report)
|
|
|
|
node_reporter.append_collect_skipped(test_report)
|
|
|
|
node_reporter.append_error(test_report)
|
|
|
|
test_report.longrepr = "filename", 1, ustr
|
|
|
|
node_reporter.append_skipped(test_report)
|
|
|
|
test_report.longrepr = "filename", 1, "Skipped: 卡嘣嘣"
|
|
|
|
node_reporter.append_skipped(test_report)
|
2020-07-10 14:44:14 +08:00
|
|
|
test_report.wasxfail = ustr # type: ignore[attr-defined]
|
2015-10-11 01:24:21 +08:00
|
|
|
node_reporter.append_skipped(test_report)
|
2013-10-21 22:54:25 +08:00
|
|
|
log.pytest_sessionfinish()
|
|
|
|
|
2015-09-26 14:11:23 +08:00
|
|
|
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_record_property(pytester: Pytester, run_and_parse: RunAndParse) -> None:
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2015-08-20 05:36:42 +08:00
|
|
|
"""
|
2015-10-10 14:19:41 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
2017-08-16 19:23:28 +08:00
|
|
|
def other(record_property):
|
|
|
|
record_property("bar", 1)
|
|
|
|
def test_record(record_property, other):
|
|
|
|
record_property("foo", "<1");
|
2015-08-20 05:36:42 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-01-23 18:35:35 +08:00
|
|
|
result, dom = run_and_parse()
|
2015-10-10 06:40:50 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
psnode = tnode.find_first_by_tag("properties")
|
2015-10-10 20:39:25 +08:00
|
|
|
pnodes = psnode.find_by_tag("property")
|
|
|
|
pnodes[0].assert_attr(name="bar", value="1")
|
|
|
|
pnodes[1].assert_attr(name="foo", value="<1")
|
2020-01-23 17:39:28 +08:00
|
|
|
result.stdout.fnmatch_lines(["*= 1 passed in *"])
|
2015-09-26 14:11:23 +08:00
|
|
|
|
|
|
|
|
2023-09-04 01:33:54 +08:00
|
|
|
def test_record_property_on_test_and_teardown_failure(
|
|
|
|
pytester: Pytester, run_and_parse: RunAndParse
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def other(record_property):
|
|
|
|
record_property("bar", 1)
|
|
|
|
yield
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
def test_record(record_property, other):
|
|
|
|
record_property("foo", "<1")
|
|
|
|
assert 0
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result, dom = run_and_parse()
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnodes = node.find_by_tag("testcase")
|
|
|
|
for tnode in tnodes:
|
|
|
|
psnode = tnode.find_first_by_tag("properties")
|
|
|
|
assert psnode, f"testcase didn't had expected properties:\n{tnode}"
|
|
|
|
pnodes = psnode.find_by_tag("property")
|
|
|
|
pnodes[0].assert_attr(name="bar", value="1")
|
|
|
|
pnodes[1].assert_attr(name="foo", value="<1")
|
|
|
|
result.stdout.fnmatch_lines(["*= 1 failed, 1 error *"])
|
|
|
|
|
|
|
|
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_record_property_same_name(
|
|
|
|
pytester: Pytester, run_and_parse: RunAndParse
|
|
|
|
) -> None:
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2016-02-29 22:39:37 +08:00
|
|
|
"""
|
2017-08-16 19:23:28 +08:00
|
|
|
def test_record_with_same_name(record_property):
|
|
|
|
record_property("foo", "bar")
|
|
|
|
record_property("foo", "baz")
|
2016-02-29 22:39:37 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-01-23 18:35:35 +08:00
|
|
|
result, dom = run_and_parse()
|
2016-02-29 22:39:37 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
psnode = tnode.find_first_by_tag("properties")
|
|
|
|
pnodes = psnode.find_by_tag("property")
|
|
|
|
pnodes[0].assert_attr(name="foo", value="bar")
|
|
|
|
pnodes[1].assert_attr(name="foo", value="baz")
|
|
|
|
|
|
|
|
|
2019-05-04 02:33:43 +08:00
|
|
|
@pytest.mark.parametrize("fixture_name", ["record_property", "record_xml_attribute"])
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_record_fixtures_without_junitxml(
|
|
|
|
pytester: Pytester, fixture_name: str
|
|
|
|
) -> None:
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2019-05-04 02:33:43 +08:00
|
|
|
f"""
|
|
|
|
def test_record({fixture_name}):
|
|
|
|
{fixture_name}("foo", "bar")
|
|
|
|
"""
|
|
|
|
)
|
2020-12-16 12:16:05 +08:00
|
|
|
result = pytester.runpytest()
|
2019-05-04 02:33:43 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2018-09-02 08:58:48 +08:00
|
|
|
@pytest.mark.filterwarnings("default")
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_record_attribute(pytester: Pytester, run_and_parse: RunAndParse) -> None:
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makeini(
|
2019-01-15 02:21:06 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
junit_family = xunit1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2018-01-19 08:06:42 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def other(record_xml_attribute):
|
|
|
|
record_xml_attribute("bar", 1)
|
|
|
|
def test_record(record_xml_attribute, other):
|
|
|
|
record_xml_attribute("foo", "<1");
|
|
|
|
"""
|
|
|
|
)
|
2020-01-23 18:35:35 +08:00
|
|
|
result, dom = run_and_parse()
|
2018-01-19 08:06:42 +08:00
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
tnode = node.find_first_by_tag("testcase")
|
|
|
|
tnode.assert_attr(bar="1")
|
|
|
|
tnode.assert_attr(foo="<1")
|
|
|
|
result.stdout.fnmatch_lines(
|
2018-09-04 07:13:41 +08:00
|
|
|
["*test_record_attribute.py:6:*record_xml_attribute is an experimental feature"]
|
2018-01-19 08:06:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-01-15 02:21:06 +08:00
|
|
|
@pytest.mark.filterwarnings("default")
|
2019-05-04 02:33:43 +08:00
|
|
|
@pytest.mark.parametrize("fixture_name", ["record_xml_attribute", "record_property"])
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_record_fixtures_xunit2(
|
2020-12-19 04:36:20 +08:00
|
|
|
pytester: Pytester, fixture_name: str, run_and_parse: RunAndParse
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Ensure record_xml_attribute and record_property drop values when outside of legacy family."""
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makeini(
|
2019-01-15 02:21:06 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
junit_family = xunit2
|
|
|
|
"""
|
|
|
|
)
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2019-01-15 02:21:06 +08:00
|
|
|
f"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
2019-05-04 02:33:43 +08:00
|
|
|
def other({fixture_name}):
|
|
|
|
{fixture_name}("bar", 1)
|
|
|
|
def test_record({fixture_name}, other):
|
|
|
|
{fixture_name}("foo", "<1");
|
|
|
|
"""
|
2019-01-15 02:21:06 +08:00
|
|
|
)
|
|
|
|
|
2020-01-23 18:35:35 +08:00
|
|
|
result, dom = run_and_parse(family=None)
|
2019-05-04 02:33:43 +08:00
|
|
|
expected_lines = []
|
|
|
|
if fixture_name == "record_xml_attribute":
|
|
|
|
expected_lines.append(
|
|
|
|
"*test_record_fixtures_xunit2.py:6:*record_xml_attribute is an experimental feature"
|
|
|
|
)
|
|
|
|
expected_lines = [
|
|
|
|
f"*test_record_fixtures_xunit2.py:6:*{fixture_name} is incompatible "
|
|
|
|
"with junit_family 'xunit2' (use 'legacy' or 'xunit1')"
|
|
|
|
]
|
|
|
|
result.stdout.fnmatch_lines(expected_lines)
|
2019-01-15 02:21:06 +08:00
|
|
|
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_random_report_log_xdist(
|
2020-12-19 04:36:20 +08:00
|
|
|
pytester: Pytester, monkeypatch: MonkeyPatch, run_and_parse: RunAndParse
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""`xdist` calls pytest_runtest_logreport as they are executed by the workers,
|
2015-09-26 14:11:23 +08:00
|
|
|
with nodes from several nodes overlapping, so junitxml must cope with that
|
2020-07-18 17:35:13 +08:00
|
|
|
to produce correct reports (#1064)."""
|
2015-09-26 14:11:23 +08:00
|
|
|
pytest.importorskip("xdist")
|
2018-12-09 18:53:41 +08:00
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2015-09-26 14:11:23 +08:00
|
|
|
"""
|
|
|
|
import pytest, time
|
|
|
|
@pytest.mark.parametrize('i', list(range(30)))
|
|
|
|
def test_x(i):
|
|
|
|
assert i != 22
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
_, dom = run_and_parse("-n2")
|
2015-10-10 06:40:50 +08:00
|
|
|
suite_node = dom.find_first_by_tag("testsuite")
|
2015-09-26 14:11:23 +08:00
|
|
|
failed = []
|
2015-10-10 06:40:50 +08:00
|
|
|
for case_node in suite_node.find_by_tag("testcase"):
|
|
|
|
if case_node.find_first_by_tag("failure"):
|
|
|
|
failed.append(case_node["name"])
|
2015-09-26 14:11:23 +08:00
|
|
|
|
|
|
|
assert failed == ["test_x[22]"]
|
2015-11-25 04:24:15 +08:00
|
|
|
|
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_root_testsuites_tag(
|
|
|
|
pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
|
|
|
) -> None:
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2019-07-04 07:34:12 +08:00
|
|
|
"""
|
|
|
|
def test_x():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
_, dom = run_and_parse(family=xunit_family)
|
2019-07-04 07:34:12 +08:00
|
|
|
root = dom.get_unique_child
|
|
|
|
assert root.tag == "testsuites"
|
|
|
|
suite_node = root.get_unique_child
|
|
|
|
assert suite_node.tag == "testsuite"
|
|
|
|
|
|
|
|
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_runs_twice(pytester: Pytester, run_and_parse: RunAndParse) -> None:
|
2020-12-16 12:16:05 +08:00
|
|
|
f = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-11-25 04:24:15 +08:00
|
|
|
def test_pass():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-11-25 04:24:15 +08:00
|
|
|
)
|
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(f, f)
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*INTERNALERROR*")
|
2021-05-26 16:19:45 +08:00
|
|
|
first, second = (x["classname"] for x in dom.find_by_tag("testcase"))
|
2015-12-18 05:27:01 +08:00
|
|
|
assert first == second
|
2015-12-06 20:21:52 +08:00
|
|
|
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_runs_twice_xdist(
|
2020-12-19 04:36:20 +08:00
|
|
|
pytester: Pytester, monkeypatch: MonkeyPatch, run_and_parse: RunAndParse
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
2015-12-06 20:21:52 +08:00
|
|
|
pytest.importorskip("xdist")
|
2020-12-16 12:16:05 +08:00
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD")
|
|
|
|
f = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-12-06 20:21:52 +08:00
|
|
|
def test_pass():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-12-06 20:21:52 +08:00
|
|
|
)
|
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(f, "--dist", "each", "--tx", "2*popen")
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*INTERNALERROR*")
|
2021-05-26 16:19:45 +08:00
|
|
|
first, second = (x["classname"] for x in dom.find_by_tag("testcase"))
|
2015-12-18 05:27:01 +08:00
|
|
|
assert first == second
|
2015-12-17 02:07:36 +08:00
|
|
|
|
|
|
|
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_fancy_items_regression(pytester: Pytester, run_and_parse: RunAndParse) -> None:
|
2015-12-18 05:27:01 +08:00
|
|
|
# issue 1259
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makeconftest(
|
2015-12-17 02:07:36 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
class FunItem(pytest.Item):
|
|
|
|
def runtest(self):
|
|
|
|
pass
|
|
|
|
class NoFunItem(pytest.Item):
|
|
|
|
def runtest(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class FunCollector(pytest.File):
|
|
|
|
def collect(self):
|
|
|
|
return [
|
2020-07-23 08:36:51 +08:00
|
|
|
FunItem.from_parent(name='a', parent=self),
|
|
|
|
NoFunItem.from_parent(name='a', parent=self),
|
|
|
|
NoFunItem.from_parent(name='b', parent=self),
|
2015-12-17 02:07:36 +08:00
|
|
|
]
|
|
|
|
|
2021-12-03 20:14:09 +08:00
|
|
|
def pytest_collect_file(file_path, parent):
|
|
|
|
if file_path.suffix == '.py':
|
|
|
|
return FunCollector.from_parent(path=file_path, parent=parent)
|
2015-12-17 02:07:36 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-12-17 02:07:36 +08:00
|
|
|
def test_pass():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-12-17 02:07:36 +08:00
|
|
|
)
|
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse()
|
2015-12-17 02:07:36 +08:00
|
|
|
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*INTERNALERROR*")
|
2015-12-18 05:27:01 +08:00
|
|
|
|
2018-12-02 15:45:31 +08:00
|
|
|
items = sorted(
|
|
|
|
"%(classname)s %(name)s" % x # noqa: UP031
|
2015-09-26 14:11:23 +08:00
|
|
|
# dom is a DomNode not a mapping, it's not possible to ** it.
|
2018-12-02 15:45:31 +08:00
|
|
|
for x in dom.find_by_tag("testcase")
|
2024-02-01 04:12:33 +08:00
|
|
|
)
|
|
|
|
|
2015-12-18 05:27:01 +08:00
|
|
|
import pprint
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2015-12-18 05:27:01 +08:00
|
|
|
pprint.pprint(items)
|
2018-06-26 21:35:27 +08:00
|
|
|
assert items == [
|
2018-12-02 15:45:31 +08:00
|
|
|
"conftest a",
|
|
|
|
"conftest a",
|
|
|
|
"conftest b",
|
|
|
|
"test_fancy_items_regression a",
|
|
|
|
"test_fancy_items_regression a",
|
|
|
|
"test_fancy_items_regression b",
|
|
|
|
"test_fancy_items_regression test_pass",
|
2018-06-26 21:35:27 +08:00
|
|
|
]
|
2016-02-29 22:14:23 +08:00
|
|
|
|
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_global_properties(pytester: Pytester, xunit_family: str) -> None:
|
2020-12-16 12:16:05 +08:00
|
|
|
path = pytester.path.joinpath("test_global_properties.xml")
|
2019-07-13 02:14:04 +08:00
|
|
|
log = LogXML(str(path), None, family=xunit_family)
|
2016-02-29 22:14:23 +08:00
|
|
|
|
|
|
|
class Report(BaseReport):
|
2020-10-06 09:13:05 +08:00
|
|
|
sections: List[Tuple[str, str]] = []
|
2016-02-29 22:14:23 +08:00
|
|
|
nodeid = "test_node_id"
|
|
|
|
|
|
|
|
log.pytest_sessionstart()
|
2020-05-01 19:40:17 +08:00
|
|
|
log.add_global_property("foo", "1")
|
|
|
|
log.add_global_property("bar", "2")
|
2016-02-29 22:14:23 +08:00
|
|
|
log.pytest_sessionfinish()
|
|
|
|
|
|
|
|
dom = minidom.parse(str(path))
|
|
|
|
|
|
|
|
properties = dom.getElementsByTagName("properties")
|
|
|
|
|
|
|
|
assert properties.length == 1, "There must be one <properties> node"
|
|
|
|
|
|
|
|
property_list = dom.getElementsByTagName("property")
|
|
|
|
|
|
|
|
assert property_list.length == 2, "There most be only 2 property nodes"
|
|
|
|
|
|
|
|
expected = {"foo": "1", "bar": "2"}
|
|
|
|
actual = {}
|
|
|
|
|
|
|
|
for p in property_list:
|
|
|
|
k = str(p.getAttribute("name"))
|
|
|
|
v = str(p.getAttribute("value"))
|
|
|
|
actual[k] = v
|
|
|
|
|
|
|
|
assert actual == expected
|
2016-08-26 04:08:51 +08:00
|
|
|
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_url_property(pytester: Pytester) -> None:
|
2016-08-26 04:08:51 +08:00
|
|
|
test_url = "http://www.github.com/pytest-dev"
|
2020-12-16 12:16:05 +08:00
|
|
|
path = pytester.path.joinpath("test_url_property.xml")
|
2016-08-26 04:08:51 +08:00
|
|
|
log = LogXML(str(path), None)
|
|
|
|
|
|
|
|
class Report(BaseReport):
|
|
|
|
longrepr = "FooBarBaz"
|
2020-10-06 09:13:05 +08:00
|
|
|
sections: List[Tuple[str, str]] = []
|
2016-08-26 04:08:51 +08:00
|
|
|
nodeid = "something"
|
|
|
|
location = "tests/filename.py", 42, "TestClass.method"
|
|
|
|
url = test_url
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
test_report = cast(TestReport, Report())
|
2016-08-26 04:08:51 +08:00
|
|
|
|
|
|
|
log.pytest_sessionstart()
|
|
|
|
node_reporter = log._opentestcase(test_report)
|
|
|
|
node_reporter.append_failure(test_report)
|
|
|
|
log.pytest_sessionfinish()
|
|
|
|
|
|
|
|
test_case = minidom.parse(str(path)).getElementsByTagName("testcase")[0]
|
|
|
|
|
2017-03-08 07:47:43 +08:00
|
|
|
assert (
|
|
|
|
test_case.getAttribute("url") == test_url
|
|
|
|
), "The URL did not get written to the xml"
|
2017-02-23 04:31:30 +08:00
|
|
|
|
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_record_testsuite_property(
|
2020-12-19 04:36:20 +08:00
|
|
|
pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-05-04 03:30:16 +08:00
|
|
|
"""
|
|
|
|
def test_func1(record_testsuite_property):
|
|
|
|
record_testsuite_property("stats", "all good")
|
|
|
|
|
|
|
|
def test_func2(record_testsuite_property):
|
|
|
|
record_testsuite_property("stats", 10)
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2019-05-04 03:30:16 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
|
|
|
properties_node = node.find_first_by_tag("properties")
|
|
|
|
p1_node = properties_node.find_nth_by_tag("property", 0)
|
|
|
|
p2_node = properties_node.find_nth_by_tag("property", 1)
|
|
|
|
p1_node.assert_attr(name="stats", value="all good")
|
|
|
|
p2_node.assert_attr(name="stats", value="10")
|
|
|
|
|
|
|
|
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_record_testsuite_property_junit_disabled(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-05-04 03:30:16 +08:00
|
|
|
"""
|
|
|
|
def test_func1(record_testsuite_property):
|
|
|
|
record_testsuite_property("stats", "all good")
|
|
|
|
"""
|
|
|
|
)
|
2020-12-16 12:16:05 +08:00
|
|
|
result = pytester.runpytest()
|
2019-05-04 03:30:16 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("junit", [True, False])
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_record_testsuite_property_type_checking(
|
|
|
|
pytester: Pytester, junit: bool
|
|
|
|
) -> None:
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2019-05-04 03:30:16 +08:00
|
|
|
"""
|
|
|
|
def test_func1(record_testsuite_property):
|
|
|
|
record_testsuite_property(1, 2)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
args = ("--junitxml=tests.xml",) if junit else ()
|
2020-12-16 12:16:05 +08:00
|
|
|
result = pytester.runpytest(*args)
|
2019-05-04 03:30:16 +08:00
|
|
|
assert result.ret == 1
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*TypeError: name parameter needs to be a string, but int given"]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-05-13 04:52:50 +08:00
|
|
|
@pytest.mark.parametrize("suite_name", ["my_suite", ""])
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
2020-12-16 12:16:05 +08:00
|
|
|
def test_set_suite_name(
|
2020-12-19 04:36:20 +08:00
|
|
|
pytester: Pytester, suite_name: str, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
2017-05-13 04:52:50 +08:00
|
|
|
if suite_name:
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makeini(
|
2017-05-13 04:52:50 +08:00
|
|
|
f"""
|
|
|
|
[pytest]
|
2019-07-13 02:14:04 +08:00
|
|
|
junit_suite_name={suite_name}
|
|
|
|
junit_family={xunit_family}
|
2017-05-13 04:52:50 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2017-05-13 04:52:50 +08:00
|
|
|
expected = suite_name
|
|
|
|
else:
|
|
|
|
expected = "pytest"
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2017-02-23 04:31:30 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2017-02-23 04:31:30 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
node = dom.find_first_by_tag("testsuite")
|
2017-05-13 04:52:50 +08:00
|
|
|
node.assert_attr(name=expected)
|
2018-10-16 20:46:38 +08:00
|
|
|
|
|
|
|
|
2020-12-19 04:36:20 +08:00
|
|
|
def test_escaped_skipreason_issue3533(
|
|
|
|
pytester: Pytester, run_and_parse: RunAndParse
|
|
|
|
) -> None:
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2018-10-16 20:46:38 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.skip(reason='1 <> 2')
|
|
|
|
def test_skip():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
_, dom = run_and_parse()
|
2018-10-16 20:46:38 +08:00
|
|
|
node = dom.find_first_by_tag("testcase")
|
|
|
|
snode = node.find_first_by_tag("skipped")
|
|
|
|
assert "1 <> 2" in snode.text
|
|
|
|
snode.assert_attr(message="1 <> 2")
|
2019-04-05 11:26:48 +08:00
|
|
|
|
|
|
|
|
2024-01-19 09:08:26 +08:00
|
|
|
def test_bin_escaped_skipreason(pytester: Pytester, run_and_parse: RunAndParse) -> None:
|
|
|
|
"""Escape special characters from mark.skip reason (#11842)."""
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.skip("\33[31;1mred\33[0m")
|
|
|
|
def test_skip():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
_, dom = run_and_parse()
|
|
|
|
node = dom.find_first_by_tag("testcase")
|
|
|
|
snode = node.find_first_by_tag("skipped")
|
|
|
|
assert "#x1B[31;1mred#x1B[0m" in snode.text
|
|
|
|
snode.assert_attr(message="#x1B[31;1mred#x1B[0m")
|
|
|
|
|
|
|
|
|
2022-08-12 17:11:03 +08:00
|
|
|
def test_escaped_setup_teardown_error(
|
|
|
|
pytester: Pytester, run_and_parse: RunAndParse
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def my_setup():
|
|
|
|
raise Exception("error: \033[31mred\033[m")
|
|
|
|
|
|
|
|
def test_esc(my_setup):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
_, dom = run_and_parse()
|
|
|
|
node = dom.find_first_by_tag("testcase")
|
|
|
|
snode = node.find_first_by_tag("error")
|
|
|
|
assert "#x1B[31mred#x1B[m" in snode["message"]
|
|
|
|
assert "#x1B[31mred#x1B[m" in snode.text
|
|
|
|
|
|
|
|
|
2019-07-13 02:14:04 +08:00
|
|
|
@parametrize_families
|
|
|
|
def test_logging_passing_tests_disabled_does_not_log_test_output(
|
2020-12-19 04:36:20 +08:00
|
|
|
pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makeini(
|
2019-04-05 11:26:48 +08:00
|
|
|
f"""
|
|
|
|
[pytest]
|
|
|
|
junit_log_passing_tests=False
|
|
|
|
junit_logging=system-out
|
2019-07-13 02:14:04 +08:00
|
|
|
junit_family={xunit_family}
|
|
|
|
"""
|
2019-04-05 11:26:48 +08:00
|
|
|
)
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2019-04-05 11:26:48 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
def test_func():
|
|
|
|
sys.stdout.write('This is stdout')
|
|
|
|
sys.stderr.write('This is stderr')
|
|
|
|
logging.warning('hello')
|
|
|
|
"""
|
|
|
|
)
|
2019-07-13 02:14:04 +08:00
|
|
|
result, dom = run_and_parse(family=xunit_family)
|
2019-04-05 11:26:48 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
node = dom.find_first_by_tag("testcase")
|
|
|
|
assert len(node.find_by_tag("system-err")) == 0
|
|
|
|
assert len(node.find_by_tag("system-out")) == 0
|
2019-11-24 22:20:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
@parametrize_families
|
|
|
|
@pytest.mark.parametrize("junit_logging", ["no", "system-out", "system-err"])
|
|
|
|
def test_logging_passing_tests_disabled_logs_output_for_failing_test_issue5430(
|
2020-12-19 04:36:20 +08:00
|
|
|
pytester: Pytester,
|
|
|
|
junit_logging: str,
|
|
|
|
run_and_parse: RunAndParse,
|
|
|
|
xunit_family: str,
|
2020-12-16 12:16:05 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makeini(
|
2019-11-24 22:20:02 +08:00
|
|
|
f"""
|
|
|
|
[pytest]
|
|
|
|
junit_log_passing_tests=False
|
|
|
|
junit_family={xunit_family}
|
|
|
|
"""
|
|
|
|
)
|
2020-12-16 12:16:05 +08:00
|
|
|
pytester.makepyfile(
|
2019-11-24 22:20:02 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
def test_func():
|
|
|
|
logging.warning('hello')
|
|
|
|
assert 0
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result, dom = run_and_parse(
|
|
|
|
"-o", "junit_logging=%s" % junit_logging, family=xunit_family
|
|
|
|
)
|
|
|
|
assert result.ret == 1
|
|
|
|
node = dom.find_first_by_tag("testcase")
|
|
|
|
if junit_logging == "system-out":
|
|
|
|
assert len(node.find_by_tag("system-err")) == 0
|
|
|
|
assert len(node.find_by_tag("system-out")) == 1
|
|
|
|
elif junit_logging == "system-err":
|
|
|
|
assert len(node.find_by_tag("system-err")) == 1
|
|
|
|
assert len(node.find_by_tag("system-out")) == 0
|
|
|
|
else:
|
|
|
|
assert junit_logging == "no"
|
|
|
|
assert len(node.find_by_tag("system-err")) == 0
|
|
|
|
assert len(node.find_by_tag("system-out")) == 0
|