| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from datetime import datetime |
| 4 |
from datetime import timezone |
| 5 |
import os |
| 6 |
from pathlib import Path |
| 7 |
import platform |
| 8 |
from typing import Any |
| 9 |
from typing import cast |
| 10 |
from typing import TYPE_CHECKING |
| 11 |
from xml.dom import minidom |
| 12 |
|
| 13 |
import xmlschema |
| 14 |
|
| 15 |
from _pytest.config import Config |
| 16 |
from _pytest.junitxml import bin_xml_escape |
| 17 |
from _pytest.junitxml import LogXML |
| 18 |
from _pytest.monkeypatch import MonkeyPatch |
| 19 |
from _pytest.pytester import Pytester |
| 20 |
from _pytest.pytester import RunResult |
| 21 |
from _pytest.reports import BaseReport |
| 22 |
from _pytest.reports import TestReport |
| 23 |
from _pytest.stash import Stash |
| 24 |
import _pytest.timing |
| 25 |
import pytest |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture(scope="session") |
| 29 |
def schema() -> xmlschema.XMLSchema: |
| 30 |
"""Return an xmlschema.XMLSchema object for the junit-10.xsd file.""" |
| 31 |
fn = Path(__file__).parent / "example_scripts/junit-10.xsd" |
| 32 |
with fn.open(encoding="utf-8") as f: |
| 33 |
return xmlschema.XMLSchema(f) |
| 34 |
|
| 35 |
|
| 36 |
class RunAndParse: |
| 37 |
def __init__(self, pytester: Pytester, schema: xmlschema.XMLSchema) -> None: |
| 38 |
self.pytester = pytester |
| 39 |
self.schema = schema |
| 40 |
|
| 41 |
def __call__( |
| 42 |
self, |
| 43 |
*args: str | os.PathLike[str], |
| 44 |
family: str | None = "xunit1", |
| 45 |
suite_name: str = "pytest", |
| 46 |
) -> tuple[RunResult, DomDocument]: |
| 47 |
if family: |
| 48 |
args = ("-o", "junit_family=" + family, *args) |
| 49 |
xml_path = self.pytester.path.joinpath("junit.xml") |
| 50 |
result = self.pytester.runpytest(f"--junitxml={xml_path}", *args) |
| 51 |
if family == "xunit2": |
| 52 |
with xml_path.open(encoding="utf-8") as f: |
| 53 |
self.schema.validate(f) |
| 54 |
xmldoc = minidom.parse(str(xml_path)) |
| 55 |
# Ensure the tests attribute of the ``<testsuite>`` element |
| 56 |
# always matches the number of ``<testcase>`` elements (#3580). |
| 57 |
doc = DomDocument(xmldoc) |
| 58 |
testcase_nodes = doc.find_by_tag("testcase") |
| 59 |
test_suite_node = doc.get_first_by_tag("testsuite") |
| 60 |
test_suite_node.assert_attr(name=suite_name, tests=len(testcase_nodes)) |
| 61 |
return result, doc |
| 62 |
|
| 63 |
|
| 64 |
@pytest.fixture |
| 65 |
def run_and_parse(pytester: Pytester, schema: xmlschema.XMLSchema) -> RunAndParse: |
| 66 |
"""Fixture that returns a function that can be used to execute pytest and |
| 67 |
return the parsed ``DomNode`` of the root xml node. |
| 68 |
|
| 69 |
The ``family`` parameter is used to configure the ``junit_family`` of the written report. |
| 70 |
"xunit2" is also automatically validated against the schema. |
| 71 |
""" |
| 72 |
return RunAndParse(pytester, schema) |
| 73 |
|
| 74 |
|
| 75 |
def assert_attr(node: minidom.Element, **kwargs: object) -> None: |
| 76 |
__tracebackhide__ = True |
| 77 |
|
| 78 |
def nodeval(node: minidom.Element, name: str) -> str | None: |
| 79 |
anode = node.getAttributeNode(name) |
| 80 |
return anode.value if anode is not None else None |
| 81 |
|
| 82 |
expected = {name: str(value) for name, value in kwargs.items()} |
| 83 |
on_node = {name: nodeval(node, name) for name in expected} |
| 84 |
assert on_node == expected |
| 85 |
|
| 86 |
|
| 87 |
class DomDocument: |
| 88 |
_node: minidom.Document | minidom.Element |
| 89 |
|
| 90 |
def __init__(self, dom: minidom.Document) -> None: |
| 91 |
self._node = dom |
| 92 |
|
| 93 |
def find_first_by_tag(self, tag: str) -> DomNode | None: |
| 94 |
return self.find_nth_by_tag(tag, 0) |
| 95 |
|
| 96 |
def get_first_by_tag(self, tag: str) -> DomNode: |
| 97 |
maybe = self.find_first_by_tag(tag) |
| 98 |
if maybe is None: |
| 99 |
raise LookupError(tag) |
| 100 |
else: |
| 101 |
return maybe |
| 102 |
|
| 103 |
def find_nth_by_tag(self, tag: str, n: int) -> DomNode | None: |
| 104 |
items = self._node.getElementsByTagName(tag) |
| 105 |
try: |
| 106 |
nth = items[n] |
| 107 |
except IndexError: |
| 108 |
return None |
| 109 |
else: |
| 110 |
return DomNode(nth) |
| 111 |
|
| 112 |
def find_by_tag(self, tag: str) -> list[DomNode]: |
| 113 |
return [DomNode(x) for x in self._node.getElementsByTagName(tag)] |
| 114 |
|
| 115 |
@property |
| 116 |
def children(self) -> list[DomNode]: |
| 117 |
return [ |
| 118 |
DomNode(x) for x in self._node.childNodes if isinstance(x, minidom.Element) |
| 119 |
] |
| 120 |
|
| 121 |
@property |
| 122 |
def get_unique_child(self) -> DomNode: |
| 123 |
children = self.children |
| 124 |
assert len(children) == 1 |
| 125 |
return children[0] |
| 126 |
|
| 127 |
def toxml(self) -> str: |
| 128 |
return self._node.toxml() |
| 129 |
|
| 130 |
|
| 131 |
class DomNode(DomDocument): |
| 132 |
_node: minidom.Element |
| 133 |
|
| 134 |
def __init__(self, dom: minidom.Element) -> None: |
| 135 |
self._node = dom |
| 136 |
|
| 137 |
def __repr__(self) -> str: |
| 138 |
return self.toxml() |
| 139 |
|
| 140 |
def __getitem__(self, key: str) -> str: |
| 141 |
node = self._node.getAttributeNode(key) |
| 142 |
if node is not None: |
| 143 |
return node.value |
| 144 |
else: |
| 145 |
raise KeyError(key) |
| 146 |
|
| 147 |
def assert_attr(self, **kwargs: object) -> None: |
| 148 |
__tracebackhide__ = True |
| 149 |
return assert_attr(self._node, **kwargs) |
| 150 |
|
| 151 |
@property |
| 152 |
def text(self) -> str: |
| 153 |
text = self._node.childNodes[0] |
| 154 |
assert isinstance(text, minidom.Text) |
| 155 |
return text.wholeText |
| 156 |
|
| 157 |
@property |
| 158 |
def tag(self) -> str: |
| 159 |
return self._node.tagName |
| 160 |
|
| 161 |
|
| 162 |
class TestJunitHelpers: |
| 163 |
"""minimal test to increase coverage for methods that are used in debugging""" |
| 164 |
|
| 165 |
@pytest.fixture |
| 166 |
def document(self) -> DomDocument: |
| 167 |
doc = minidom.parseString(""" |
| 168 |
<root> |
| 169 |
<item name="a"></item> |
| 170 |
<item name="b"></item> |
| 171 |
</root> |
| 172 |
""") |
| 173 |
return DomDocument(doc) |
| 174 |
|
| 175 |
def test_uc_root(self, document: DomDocument) -> None: |
| 176 |
assert document.get_unique_child.tag == "root" |
| 177 |
|
| 178 |
def test_node_assert_attr(self, document: DomDocument) -> None: |
| 179 |
item = document.get_first_by_tag("item") |
| 180 |
|
| 181 |
item.assert_attr(name="a") |
| 182 |
|
| 183 |
with pytest.raises(AssertionError): |
| 184 |
item.assert_attr(missing="foo") |
| 185 |
|
| 186 |
def test_node_getitem(self, document: DomDocument) -> None: |
| 187 |
item = document.get_first_by_tag("item") |
| 188 |
assert item["name"] == "a" |
| 189 |
|
| 190 |
with pytest.raises(KeyError, match="missing"): |
| 191 |
item["missing"] |
| 192 |
|
| 193 |
def test_node_get_first_lookup(self, document: DomDocument) -> None: |
| 194 |
with pytest.raises(LookupError, match="missing"): |
| 195 |
document.get_first_by_tag("missing") |
| 196 |
|
| 197 |
def test_node_repr(self, document: DomDocument) -> None: |
| 198 |
item = document.get_first_by_tag("item") |
| 199 |
|
| 200 |
assert repr(item) == item.toxml() |
| 201 |
assert item.toxml() == '<item name="a"/>' |
| 202 |
|
| 203 |
|
| 204 |
parametrize_families = pytest.mark.parametrize("xunit_family", ["xunit1", "xunit2"]) |
| 205 |
|
| 206 |
|
| 207 |
class TestPython: |
| 208 |
@parametrize_families |
| 209 |
def test_summing_simple( |
| 210 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 211 |
) -> None: |
| 212 |
pytester.makepyfile( |
| 213 |
""" |
| 214 |
import pytest |
| 215 |
def test_pass(): |
| 216 |
pass |
| 217 |
def test_fail(): |
| 218 |
assert 0 |
| 219 |
def test_skip(): |
| 220 |
pytest.skip("") |
| 221 |
@pytest.mark.xfail |
| 222 |
def test_xfail(): |
| 223 |
assert 0 |
| 224 |
@pytest.mark.xfail |
| 225 |
def test_xpass(): |
| 226 |
assert 1 |
| 227 |
""" |
| 228 |
) |
| 229 |
result, dom = run_and_parse(family=xunit_family) |
| 230 |
assert result.ret |
| 231 |
node = dom.get_first_by_tag("testsuite") |
| 232 |
node.assert_attr(name="pytest", errors=0, failures=1, skipped=2, tests=5) |
| 233 |
|
| 234 |
@parametrize_families |
| 235 |
def test_summing_simple_with_errors( |
| 236 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 237 |
) -> None: |
| 238 |
pytester.makepyfile( |
| 239 |
""" |
| 240 |
import pytest |
| 241 |
@pytest.fixture |
| 242 |
def fixture(): |
| 243 |
raise Exception() |
| 244 |
def test_pass(): |
| 245 |
pass |
| 246 |
def test_fail(): |
| 247 |
assert 0 |
| 248 |
def test_error(fixture): |
| 249 |
pass |
| 250 |
@pytest.mark.xfail |
| 251 |
def test_xfail(): |
| 252 |
assert False |
| 253 |
@pytest.mark.xfail(strict=True) |
| 254 |
def test_xpass(): |
| 255 |
assert True |
| 256 |
""" |
| 257 |
) |
| 258 |
result, dom = run_and_parse(family=xunit_family) |
| 259 |
assert result.ret |
| 260 |
node = dom.get_first_by_tag("testsuite") |
| 261 |
node.assert_attr(name="pytest", errors=1, failures=2, skipped=1, tests=5) |
| 262 |
|
| 263 |
@parametrize_families |
| 264 |
def test_hostname_in_xml( |
| 265 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 266 |
) -> None: |
| 267 |
pytester.makepyfile( |
| 268 |
""" |
| 269 |
def test_pass(): |
| 270 |
pass |
| 271 |
""" |
| 272 |
) |
| 273 |
_result, dom = run_and_parse(family=xunit_family) |
| 274 |
node = dom.get_first_by_tag("testsuite") |
| 275 |
node.assert_attr(hostname=platform.node()) |
| 276 |
|
| 277 |
@parametrize_families |
| 278 |
def test_timestamp_in_xml( |
| 279 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 280 |
) -> None: |
| 281 |
pytester.makepyfile( |
| 282 |
""" |
| 283 |
def test_pass(): |
| 284 |
pass |
| 285 |
""" |
| 286 |
) |
| 287 |
start_time = datetime.now(timezone.utc) |
| 288 |
_result, dom = run_and_parse(family=xunit_family) |
| 289 |
node = dom.get_first_by_tag("testsuite") |
| 290 |
timestamp = datetime.fromisoformat(node["timestamp"]) |
| 291 |
assert start_time <= timestamp < datetime.now(timezone.utc) |
| 292 |
|
| 293 |
def test_timing_function( |
| 294 |
self, |
| 295 |
pytester: Pytester, |
| 296 |
run_and_parse: RunAndParse, |
| 297 |
mock_timing: _pytest.timing.MockTiming, |
| 298 |
) -> None: |
| 299 |
pytester.makepyfile( |
| 300 |
""" |
| 301 |
from _pytest import timing |
| 302 |
def setup_module(): |
| 303 |
timing.sleep(1) |
| 304 |
def teardown_module(): |
| 305 |
timing.sleep(2) |
| 306 |
def test_sleep(): |
| 307 |
timing.sleep(4) |
| 308 |
""" |
| 309 |
) |
| 310 |
_result, dom = run_and_parse() |
| 311 |
node = dom.get_first_by_tag("testsuite") |
| 312 |
tnode = node.get_first_by_tag("testcase") |
| 313 |
val = tnode["time"] |
| 314 |
assert val is not None |
| 315 |
assert float(val) == 7.0 |
| 316 |
|
| 317 |
@pytest.mark.parametrize("duration_report", ["call", "total"]) |
| 318 |
def test_junit_duration_report( |
| 319 |
self, |
| 320 |
pytester: Pytester, |
| 321 |
monkeypatch: MonkeyPatch, |
| 322 |
duration_report: str, |
| 323 |
run_and_parse: RunAndParse, |
| 324 |
) -> None: |
| 325 |
# mock LogXML.node_reporter so it always sets a known duration to each test report object |
| 326 |
original_node_reporter = LogXML.node_reporter |
| 327 |
|
| 328 |
def node_reporter_wrapper(s: Any, report: TestReport) -> Any: |
| 329 |
report.duration = 1.0 |
| 330 |
reporter = original_node_reporter(s, report) |
| 331 |
return reporter |
| 332 |
|
| 333 |
monkeypatch.setattr(LogXML, "node_reporter", node_reporter_wrapper) |
| 334 |
|
| 335 |
pytester.makepyfile( |
| 336 |
""" |
| 337 |
def test_foo(): |
| 338 |
pass |
| 339 |
""" |
| 340 |
) |
| 341 |
_result, dom = run_and_parse("-o", f"junit_duration_report={duration_report}") |
| 342 |
node = dom.get_first_by_tag("testsuite") |
| 343 |
tnode = node.get_first_by_tag("testcase") |
| 344 |
val = float(tnode["time"]) |
| 345 |
if duration_report == "total": |
| 346 |
assert val == 3.0 |
| 347 |
else: |
| 348 |
assert duration_report == "call" |
| 349 |
assert val == 1.0 |
| 350 |
|
| 351 |
@parametrize_families |
| 352 |
def test_setup_error( |
| 353 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 354 |
) -> None: |
| 355 |
pytester.makepyfile( |
| 356 |
""" |
| 357 |
import pytest |
| 358 |
|
| 359 |
@pytest.fixture |
| 360 |
def arg(request): |
| 361 |
raise ValueError("Error reason") |
| 362 |
def test_function(arg): |
| 363 |
pass |
| 364 |
""" |
| 365 |
) |
| 366 |
result, dom = run_and_parse(family=xunit_family) |
| 367 |
assert result.ret |
| 368 |
node = dom.get_first_by_tag("testsuite") |
| 369 |
node.assert_attr(errors=1, tests=1) |
| 370 |
tnode = node.get_first_by_tag("testcase") |
| 371 |
tnode.assert_attr(classname="test_setup_error", name="test_function") |
| 372 |
fnode = tnode.get_first_by_tag("error") |
| 373 |
fnode.assert_attr(message='failed on setup with "ValueError: Error reason"') |
| 374 |
assert "ValueError" in fnode.toxml() |
| 375 |
|
| 376 |
@parametrize_families |
| 377 |
def test_teardown_error( |
| 378 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 379 |
) -> None: |
| 380 |
pytester.makepyfile( |
| 381 |
""" |
| 382 |
import pytest |
| 383 |
|
| 384 |
@pytest.fixture |
| 385 |
def arg(): |
| 386 |
yield |
| 387 |
raise ValueError('Error reason') |
| 388 |
def test_function(arg): |
| 389 |
pass |
| 390 |
""" |
| 391 |
) |
| 392 |
result, dom = run_and_parse(family=xunit_family) |
| 393 |
assert result.ret |
| 394 |
node = dom.get_first_by_tag("testsuite") |
| 395 |
node.assert_attr(errors=1, tests=1) |
| 396 |
tnode = node.get_first_by_tag("testcase") |
| 397 |
tnode.assert_attr(classname="test_teardown_error", name="test_function") |
| 398 |
fnode = tnode.get_first_by_tag("error") |
| 399 |
fnode.assert_attr(message='failed on teardown with "ValueError: Error reason"') |
| 400 |
assert "ValueError" in fnode.toxml() |
| 401 |
|
| 402 |
@parametrize_families |
| 403 |
def test_call_failure_teardown_error( |
| 404 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 405 |
) -> None: |
| 406 |
pytester.makepyfile( |
| 407 |
""" |
| 408 |
import pytest |
| 409 |
|
| 410 |
@pytest.fixture |
| 411 |
def arg(): |
| 412 |
yield |
| 413 |
raise Exception("Teardown Exception") |
| 414 |
def test_function(arg): |
| 415 |
raise Exception("Call Exception") |
| 416 |
""" |
| 417 |
) |
| 418 |
result, dom = run_and_parse(family=xunit_family) |
| 419 |
assert result.ret |
| 420 |
node = dom.get_first_by_tag("testsuite") |
| 421 |
node.assert_attr(errors=1, failures=1, tests=2) |
| 422 |
first, second = dom.find_by_tag("testcase") |
| 423 |
assert first |
| 424 |
assert second |
| 425 |
assert first != second |
| 426 |
fnode = first.get_first_by_tag("failure") |
| 427 |
fnode.assert_attr(message="Exception: Call Exception") |
| 428 |
snode = second.get_first_by_tag("error") |
| 429 |
snode.assert_attr( |
| 430 |
message='failed on teardown with "Exception: Teardown Exception"' |
| 431 |
) |
| 432 |
|
| 433 |
@parametrize_families |
| 434 |
def test_skip_contains_name_reason( |
| 435 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 436 |
) -> None: |
| 437 |
pytester.makepyfile( |
| 438 |
""" |
| 439 |
import pytest |
| 440 |
def test_skip(): |
| 441 |
pytest.skip("hello23") |
| 442 |
""" |
| 443 |
) |
| 444 |
result, dom = run_and_parse(family=xunit_family) |
| 445 |
assert result.ret == 0 |
| 446 |
node = dom.get_first_by_tag("testsuite") |
| 447 |
node.assert_attr(skipped=1) |
| 448 |
tnode = node.get_first_by_tag("testcase") |
| 449 |
tnode.assert_attr(classname="test_skip_contains_name_reason", name="test_skip") |
| 450 |
snode = tnode.get_first_by_tag("skipped") |
| 451 |
snode.assert_attr(type="pytest.skip", message="hello23") |
| 452 |
|
| 453 |
@parametrize_families |
| 454 |
def test_mark_skip_contains_name_reason( |
| 455 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 456 |
) -> None: |
| 457 |
pytester.makepyfile( |
| 458 |
""" |
| 459 |
import pytest |
| 460 |
@pytest.mark.skip(reason="hello24") |
| 461 |
def test_skip(): |
| 462 |
assert True |
| 463 |
""" |
| 464 |
) |
| 465 |
result, dom = run_and_parse(family=xunit_family) |
| 466 |
assert result.ret == 0 |
| 467 |
node = dom.get_first_by_tag("testsuite") |
| 468 |
node.assert_attr(skipped=1) |
| 469 |
tnode = node.get_first_by_tag("testcase") |
| 470 |
tnode.assert_attr( |
| 471 |
classname="test_mark_skip_contains_name_reason", name="test_skip" |
| 472 |
) |
| 473 |
snode = tnode.get_first_by_tag("skipped") |
| 474 |
snode.assert_attr(type="pytest.skip", message="hello24") |
| 475 |
|
| 476 |
@parametrize_families |
| 477 |
def test_mark_skipif_contains_name_reason( |
| 478 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 479 |
) -> None: |
| 480 |
pytester.makepyfile( |
| 481 |
""" |
| 482 |
import pytest |
| 483 |
GLOBAL_CONDITION = True |
| 484 |
@pytest.mark.skipif(GLOBAL_CONDITION, reason="hello25") |
| 485 |
def test_skip(): |
| 486 |
assert True |
| 487 |
""" |
| 488 |
) |
| 489 |
result, dom = run_and_parse(family=xunit_family) |
| 490 |
assert result.ret == 0 |
| 491 |
node = dom.get_first_by_tag("testsuite") |
| 492 |
node.assert_attr(skipped=1) |
| 493 |
tnode = node.get_first_by_tag("testcase") |
| 494 |
tnode.assert_attr( |
| 495 |
classname="test_mark_skipif_contains_name_reason", name="test_skip" |
| 496 |
) |
| 497 |
snode = tnode.get_first_by_tag("skipped") |
| 498 |
snode.assert_attr(type="pytest.skip", message="hello25") |
| 499 |
|
| 500 |
@parametrize_families |
| 501 |
def test_mark_skip_doesnt_capture_output( |
| 502 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 503 |
) -> None: |
| 504 |
pytester.makepyfile( |
| 505 |
""" |
| 506 |
import pytest |
| 507 |
@pytest.mark.skip(reason="foo") |
| 508 |
def test_skip(): |
| 509 |
print("bar!") |
| 510 |
""" |
| 511 |
) |
| 512 |
result, dom = run_and_parse(family=xunit_family) |
| 513 |
assert result.ret == 0 |
| 514 |
node_xml = dom.get_first_by_tag("testsuite").toxml() |
| 515 |
assert "bar!" not in node_xml |
| 516 |
|
| 517 |
@parametrize_families |
| 518 |
def test_classname_instance( |
| 519 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 520 |
) -> None: |
| 521 |
pytester.makepyfile( |
| 522 |
""" |
| 523 |
class TestClass(object): |
| 524 |
def test_method(self): |
| 525 |
assert 0 |
| 526 |
""" |
| 527 |
) |
| 528 |
result, dom = run_and_parse(family=xunit_family) |
| 529 |
assert result.ret |
| 530 |
node = dom.get_first_by_tag("testsuite") |
| 531 |
node.assert_attr(failures=1) |
| 532 |
tnode = node.get_first_by_tag("testcase") |
| 533 |
tnode.assert_attr( |
| 534 |
classname="test_classname_instance.TestClass", name="test_method" |
| 535 |
) |
| 536 |
|
| 537 |
@parametrize_families |
| 538 |
def test_classname_nested_dir( |
| 539 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 540 |
) -> None: |
| 541 |
p = pytester.mkdir("sub").joinpath("test_hello.py") |
| 542 |
p.write_text("def test_func(): 0/0", encoding="utf-8") |
| 543 |
result, dom = run_and_parse(family=xunit_family) |
| 544 |
assert result.ret |
| 545 |
node = dom.get_first_by_tag("testsuite") |
| 546 |
node.assert_attr(failures=1) |
| 547 |
tnode = node.get_first_by_tag("testcase") |
| 548 |
tnode.assert_attr(classname="sub.test_hello", name="test_func") |
| 549 |
|
| 550 |
@parametrize_families |
| 551 |
def test_internal_error( |
| 552 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 553 |
) -> None: |
| 554 |
pytester.makeconftest("def pytest_runtest_protocol(): 0 / 0") |
| 555 |
pytester.makepyfile("def test_function(): pass") |
| 556 |
result, dom = run_and_parse(family=xunit_family) |
| 557 |
assert result.ret |
| 558 |
node = dom.get_first_by_tag("testsuite") |
| 559 |
node.assert_attr(errors=1, tests=1) |
| 560 |
tnode = node.get_first_by_tag("testcase") |
| 561 |
tnode.assert_attr(classname="pytest", name="internal") |
| 562 |
fnode = tnode.get_first_by_tag("error") |
| 563 |
fnode.assert_attr(message="internal error") |
| 564 |
assert "Division" in fnode.toxml() |
| 565 |
|
| 566 |
@pytest.mark.parametrize( |
| 567 |
"junit_logging", ["no", "log", "system-out", "system-err", "out-err", "all"] |
| 568 |
) |
| 569 |
@parametrize_families |
| 570 |
def test_failure_function( |
| 571 |
self, |
| 572 |
pytester: Pytester, |
| 573 |
junit_logging: str, |
| 574 |
run_and_parse: RunAndParse, |
| 575 |
xunit_family: str, |
| 576 |
) -> None: |
| 577 |
pytester.makepyfile( |
| 578 |
""" |
| 579 |
import logging |
| 580 |
import sys |
| 581 |
|
| 582 |
def test_fail(): |
| 583 |
print("hello-stdout") |
| 584 |
sys.stderr.write("hello-stderr\\n") |
| 585 |
logging.info('info msg') |
| 586 |
logging.warning('warning msg') |
| 587 |
raise ValueError(42) |
| 588 |
""" |
| 589 |
) |
| 590 |
|
| 591 |
result, dom = run_and_parse( |
| 592 |
"-o", f"junit_logging={junit_logging}", family=xunit_family |
| 593 |
) |
| 594 |
assert result.ret, "Expected ret > 0" |
| 595 |
node = dom.get_first_by_tag("testsuite") |
| 596 |
node.assert_attr(failures=1, tests=1) |
| 597 |
tnode = node.get_first_by_tag("testcase") |
| 598 |
tnode.assert_attr(classname="test_failure_function", name="test_fail") |
| 599 |
fnode = tnode.get_first_by_tag("failure") |
| 600 |
fnode.assert_attr(message="ValueError: 42") |
| 601 |
assert "ValueError" in fnode.toxml(), "ValueError not included" |
| 602 |
|
| 603 |
if junit_logging in ["log", "all"]: |
| 604 |
logdata = tnode.get_first_by_tag("system-out") |
| 605 |
log_xml = logdata.toxml() |
| 606 |
assert logdata.tag == "system-out", "Expected tag: system-out" |
| 607 |
assert "info msg" not in log_xml, "Unexpected INFO message" |
| 608 |
assert "warning msg" in log_xml, "Missing WARN message" |
| 609 |
if junit_logging in ["system-out", "out-err", "all"]: |
| 610 |
systemout = tnode.get_first_by_tag("system-out") |
| 611 |
systemout_xml = systemout.toxml() |
| 612 |
assert systemout.tag == "system-out", "Expected tag: system-out" |
| 613 |
assert "info msg" not in systemout_xml, "INFO message found in system-out" |
| 614 |
assert "hello-stdout" in systemout_xml, ( |
| 615 |
"Missing 'hello-stdout' in system-out" |
| 616 |
) |
| 617 |
if junit_logging in ["system-err", "out-err", "all"]: |
| 618 |
systemerr = tnode.get_first_by_tag("system-err") |
| 619 |
systemerr_xml = systemerr.toxml() |
| 620 |
assert systemerr.tag == "system-err", "Expected tag: system-err" |
| 621 |
assert "info msg" not in systemerr_xml, "INFO message found in system-err" |
| 622 |
assert "hello-stderr" in systemerr_xml, ( |
| 623 |
"Missing 'hello-stderr' in system-err" |
| 624 |
) |
| 625 |
assert "warning msg" not in systemerr_xml, ( |
| 626 |
"WARN message found in system-err" |
| 627 |
) |
| 628 |
if junit_logging == "no": |
| 629 |
assert not tnode.find_by_tag("log"), "Found unexpected content: log" |
| 630 |
assert not tnode.find_by_tag("system-out"), ( |
| 631 |
"Found unexpected content: system-out" |
| 632 |
) |
| 633 |
assert not tnode.find_by_tag("system-err"), ( |
| 634 |
"Found unexpected content: system-err" |
| 635 |
) |
| 636 |
|
| 637 |
@parametrize_families |
| 638 |
def test_failure_verbose_message( |
| 639 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 640 |
) -> None: |
| 641 |
pytester.makepyfile( |
| 642 |
""" |
| 643 |
import sys |
| 644 |
def test_fail(): |
| 645 |
assert 0, "An error" |
| 646 |
""" |
| 647 |
) |
| 648 |
_result, dom = run_and_parse(family=xunit_family) |
| 649 |
node = dom.get_first_by_tag("testsuite") |
| 650 |
tnode = node.get_first_by_tag("testcase") |
| 651 |
fnode = tnode.get_first_by_tag("failure") |
| 652 |
fnode.assert_attr(message="AssertionError: An error\nassert 0") |
| 653 |
|
| 654 |
@parametrize_families |
| 655 |
def test_failure_escape( |
| 656 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 657 |
) -> None: |
| 658 |
pytester.makepyfile( |
| 659 |
""" |
| 660 |
import pytest |
| 661 |
@pytest.mark.parametrize('arg1', "<&'", ids="<&'") |
| 662 |
def test_func(arg1): |
| 663 |
print(arg1) |
| 664 |
assert 0 |
| 665 |
""" |
| 666 |
) |
| 667 |
result, dom = run_and_parse( |
| 668 |
"-o", "junit_logging=system-out", family=xunit_family |
| 669 |
) |
| 670 |
assert result.ret |
| 671 |
node = dom.get_first_by_tag("testsuite") |
| 672 |
node.assert_attr(failures=3, tests=3) |
| 673 |
tnodes = node.find_by_tag("testcase") |
| 674 |
for tnode, char in zip(tnodes, "<&'", strict=True): |
| 675 |
tnode.assert_attr( |
| 676 |
classname="test_failure_escape", name=f"test_func[{char}]" |
| 677 |
) |
| 678 |
sysout = tnode.get_first_by_tag("system-out") |
| 679 |
text = sysout.text |
| 680 |
assert f"{char}\n" in text |
| 681 |
|
| 682 |
@parametrize_families |
| 683 |
def test_junit_prefixing( |
| 684 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 685 |
) -> None: |
| 686 |
pytester.makepyfile( |
| 687 |
""" |
| 688 |
def test_func(): |
| 689 |
assert 0 |
| 690 |
class TestHello(object): |
| 691 |
def test_hello(self): |
| 692 |
pass |
| 693 |
""" |
| 694 |
) |
| 695 |
result, dom = run_and_parse("--junitprefix=xyz", family=xunit_family) |
| 696 |
assert result.ret |
| 697 |
node = dom.get_first_by_tag("testsuite") |
| 698 |
node.assert_attr(failures=1, tests=2) |
| 699 |
tnode = node.get_first_by_tag("testcase") |
| 700 |
tnode.assert_attr(classname="xyz.test_junit_prefixing", name="test_func") |
| 701 |
tnode = node.find_by_tag("testcase")[1] |
| 702 |
tnode.assert_attr( |
| 703 |
classname="xyz.test_junit_prefixing.TestHello", name="test_hello" |
| 704 |
) |
| 705 |
|
| 706 |
@parametrize_families |
| 707 |
def test_xfailure_function( |
| 708 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 709 |
) -> None: |
| 710 |
pytester.makepyfile( |
| 711 |
""" |
| 712 |
import pytest |
| 713 |
def test_xfail(): |
| 714 |
pytest.xfail("42") |
| 715 |
""" |
| 716 |
) |
| 717 |
result, dom = run_and_parse(family=xunit_family) |
| 718 |
assert not result.ret |
| 719 |
node = dom.get_first_by_tag("testsuite") |
| 720 |
node.assert_attr(skipped=1, tests=1) |
| 721 |
tnode = node.get_first_by_tag("testcase") |
| 722 |
tnode.assert_attr(classname="test_xfailure_function", name="test_xfail") |
| 723 |
fnode = tnode.get_first_by_tag("skipped") |
| 724 |
fnode.assert_attr(type="pytest.xfail", message="42") |
| 725 |
|
| 726 |
@parametrize_families |
| 727 |
def test_xfailure_marker( |
| 728 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 729 |
) -> None: |
| 730 |
pytester.makepyfile( |
| 731 |
""" |
| 732 |
import pytest |
| 733 |
@pytest.mark.xfail(reason="42") |
| 734 |
def test_xfail(): |
| 735 |
assert False |
| 736 |
""" |
| 737 |
) |
| 738 |
result, dom = run_and_parse(family=xunit_family) |
| 739 |
assert not result.ret |
| 740 |
node = dom.get_first_by_tag("testsuite") |
| 741 |
node.assert_attr(skipped=1, tests=1) |
| 742 |
tnode = node.get_first_by_tag("testcase") |
| 743 |
tnode.assert_attr(classname="test_xfailure_marker", name="test_xfail") |
| 744 |
fnode = tnode.get_first_by_tag("skipped") |
| 745 |
fnode.assert_attr(type="pytest.xfail", message="42") |
| 746 |
|
| 747 |
@pytest.mark.parametrize( |
| 748 |
"junit_logging", ["no", "log", "system-out", "system-err", "out-err", "all"] |
| 749 |
) |
| 750 |
def test_xfail_captures_output_once( |
| 751 |
self, pytester: Pytester, junit_logging: str, run_and_parse: RunAndParse |
| 752 |
) -> None: |
| 753 |
pytester.makepyfile( |
| 754 |
""" |
| 755 |
import sys |
| 756 |
import pytest |
| 757 |
|
| 758 |
@pytest.mark.xfail() |
| 759 |
def test_fail(): |
| 760 |
sys.stdout.write('XFAIL This is stdout') |
| 761 |
sys.stderr.write('XFAIL This is stderr') |
| 762 |
assert 0 |
| 763 |
""" |
| 764 |
) |
| 765 |
_result, dom = run_and_parse("-o", f"junit_logging={junit_logging}") |
| 766 |
node = dom.get_first_by_tag("testsuite") |
| 767 |
tnode = node.get_first_by_tag("testcase") |
| 768 |
|
| 769 |
has_err_logging = junit_logging in ["system-err", "out-err", "all"] |
| 770 |
expected_err_output_len = 1 if has_err_logging else 0 |
| 771 |
assert len(tnode.find_by_tag("system-err")) == expected_err_output_len |
| 772 |
|
| 773 |
has_out_logigng = junit_logging in ("log", "system-out", "out-err", "all") |
| 774 |
expected_out_output_len = 1 if has_out_logigng else 0 |
| 775 |
|
| 776 |
assert len(tnode.find_by_tag("system-out")) == expected_out_output_len |
| 777 |
|
| 778 |
@parametrize_families |
| 779 |
def test_xfailure_xpass( |
| 780 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 781 |
) -> None: |
| 782 |
pytester.makepyfile( |
| 783 |
""" |
| 784 |
import pytest |
| 785 |
@pytest.mark.xfail |
| 786 |
def test_xpass(): |
| 787 |
pass |
| 788 |
""" |
| 789 |
) |
| 790 |
_result, dom = run_and_parse(family=xunit_family) |
| 791 |
# assert result.ret |
| 792 |
node = dom.get_first_by_tag("testsuite") |
| 793 |
node.assert_attr(skipped=0, tests=1) |
| 794 |
tnode = node.get_first_by_tag("testcase") |
| 795 |
tnode.assert_attr(classname="test_xfailure_xpass", name="test_xpass") |
| 796 |
|
| 797 |
@parametrize_families |
| 798 |
def test_xfailure_xpass_strict( |
| 799 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 800 |
) -> None: |
| 801 |
pytester.makepyfile( |
| 802 |
""" |
| 803 |
import pytest |
| 804 |
@pytest.mark.xfail(strict=True, reason="This needs to fail!") |
| 805 |
def test_xpass(): |
| 806 |
pass |
| 807 |
""" |
| 808 |
) |
| 809 |
_result, dom = run_and_parse(family=xunit_family) |
| 810 |
# assert result.ret |
| 811 |
node = dom.get_first_by_tag("testsuite") |
| 812 |
node.assert_attr(skipped=0, tests=1) |
| 813 |
tnode = node.get_first_by_tag("testcase") |
| 814 |
tnode.assert_attr(classname="test_xfailure_xpass_strict", name="test_xpass") |
| 815 |
fnode = tnode.get_first_by_tag("failure") |
| 816 |
fnode.assert_attr(message="[XPASS(strict)] This needs to fail!") |
| 817 |
|
| 818 |
@parametrize_families |
| 819 |
def test_collect_error( |
| 820 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 821 |
) -> None: |
| 822 |
pytester.makepyfile("syntax error") |
| 823 |
result, dom = run_and_parse(family=xunit_family) |
| 824 |
assert result.ret |
| 825 |
node = dom.get_first_by_tag("testsuite") |
| 826 |
node.assert_attr(errors=1, tests=1) |
| 827 |
tnode = node.get_first_by_tag("testcase") |
| 828 |
fnode = tnode.get_first_by_tag("error") |
| 829 |
fnode.assert_attr(message="collection failure") |
| 830 |
assert "SyntaxError" in fnode.toxml() |
| 831 |
|
| 832 |
def test_unicode(self, pytester: Pytester, run_and_parse: RunAndParse) -> None: |
| 833 |
value = "hx\xc4\x85\xc4\x87\n" |
| 834 |
pytester.makepyfile( |
| 835 |
f"""\ |
| 836 |
# coding: latin1 |
| 837 |
def test_hello(): |
| 838 |
print({value!r}) |
| 839 |
assert 0 |
| 840 |
""" |
| 841 |
) |
| 842 |
result, dom = run_and_parse() |
| 843 |
assert result.ret == 1 |
| 844 |
tnode = dom.get_first_by_tag("testcase") |
| 845 |
fnode = tnode.get_first_by_tag("failure") |
| 846 |
assert "hx" in fnode.toxml() |
| 847 |
|
| 848 |
def test_assertion_binchars( |
| 849 |
self, pytester: Pytester, run_and_parse: RunAndParse |
| 850 |
) -> None: |
| 851 |
"""This test did fail when the escaping wasn't strict.""" |
| 852 |
pytester.makepyfile( |
| 853 |
""" |
| 854 |
|
| 855 |
M1 = '\x01\x02\x03\x04' |
| 856 |
M2 = '\x01\x02\x03\x05' |
| 857 |
|
| 858 |
def test_str_compare(): |
| 859 |
assert M1 == M2 |
| 860 |
""" |
| 861 |
) |
| 862 |
_result, dom = run_and_parse() |
| 863 |
print(dom.toxml()) |
| 864 |
|
| 865 |
@pytest.mark.parametrize("junit_logging", ["no", "system-out"]) |
| 866 |
def test_pass_captures_stdout( |
| 867 |
self, pytester: Pytester, run_and_parse: RunAndParse, junit_logging: str |
| 868 |
) -> None: |
| 869 |
pytester.makepyfile( |
| 870 |
""" |
| 871 |
def test_pass(): |
| 872 |
print('hello-stdout') |
| 873 |
""" |
| 874 |
) |
| 875 |
_result, dom = run_and_parse("-o", f"junit_logging={junit_logging}") |
| 876 |
node = dom.get_first_by_tag("testsuite") |
| 877 |
pnode = node.get_first_by_tag("testcase") |
| 878 |
if junit_logging == "no": |
| 879 |
assert not node.find_by_tag("system-out"), ( |
| 880 |
"system-out should not be generated" |
| 881 |
) |
| 882 |
if junit_logging == "system-out": |
| 883 |
systemout = pnode.get_first_by_tag("system-out") |
| 884 |
assert "hello-stdout" in systemout.toxml(), ( |
| 885 |
"'hello-stdout' should be in system-out" |
| 886 |
) |
| 887 |
|
| 888 |
@pytest.mark.parametrize("junit_logging", ["no", "system-err"]) |
| 889 |
def test_pass_captures_stderr( |
| 890 |
self, pytester: Pytester, run_and_parse: RunAndParse, junit_logging: str |
| 891 |
) -> None: |
| 892 |
pytester.makepyfile( |
| 893 |
""" |
| 894 |
import sys |
| 895 |
def test_pass(): |
| 896 |
sys.stderr.write('hello-stderr') |
| 897 |
""" |
| 898 |
) |
| 899 |
_result, dom = run_and_parse("-o", f"junit_logging={junit_logging}") |
| 900 |
node = dom.get_first_by_tag("testsuite") |
| 901 |
pnode = node.get_first_by_tag("testcase") |
| 902 |
if junit_logging == "no": |
| 903 |
assert not node.find_by_tag("system-err"), ( |
| 904 |
"system-err should not be generated" |
| 905 |
) |
| 906 |
if junit_logging == "system-err": |
| 907 |
systemerr = pnode.get_first_by_tag("system-err") |
| 908 |
assert "hello-stderr" in systemerr.toxml(), ( |
| 909 |
"'hello-stderr' should be in system-err" |
| 910 |
) |
| 911 |
|
| 912 |
@pytest.mark.parametrize("junit_logging", ["no", "system-out"]) |
| 913 |
def test_setup_error_captures_stdout( |
| 914 |
self, pytester: Pytester, run_and_parse: RunAndParse, junit_logging: str |
| 915 |
) -> None: |
| 916 |
pytester.makepyfile( |
| 917 |
""" |
| 918 |
import pytest |
| 919 |
|
| 920 |
@pytest.fixture |
| 921 |
def arg(request): |
| 922 |
print('hello-stdout') |
| 923 |
raise ValueError() |
| 924 |
def test_function(arg): |
| 925 |
pass |
| 926 |
""" |
| 927 |
) |
| 928 |
_result, dom = run_and_parse("-o", f"junit_logging={junit_logging}") |
| 929 |
node = dom.get_first_by_tag("testsuite") |
| 930 |
pnode = node.get_first_by_tag("testcase") |
| 931 |
if junit_logging == "no": |
| 932 |
assert not node.find_by_tag("system-out"), ( |
| 933 |
"system-out should not be generated" |
| 934 |
) |
| 935 |
if junit_logging == "system-out": |
| 936 |
systemout = pnode.get_first_by_tag("system-out") |
| 937 |
assert "hello-stdout" in systemout.toxml(), ( |
| 938 |
"'hello-stdout' should be in system-out" |
| 939 |
) |
| 940 |
|
| 941 |
@pytest.mark.parametrize("junit_logging", ["no", "system-err"]) |
| 942 |
def test_setup_error_captures_stderr( |
| 943 |
self, pytester: Pytester, run_and_parse: RunAndParse, junit_logging: str |
| 944 |
) -> None: |
| 945 |
pytester.makepyfile( |
| 946 |
""" |
| 947 |
import sys |
| 948 |
import pytest |
| 949 |
|
| 950 |
@pytest.fixture |
| 951 |
def arg(request): |
| 952 |
sys.stderr.write('hello-stderr') |
| 953 |
raise ValueError() |
| 954 |
def test_function(arg): |
| 955 |
pass |
| 956 |
""" |
| 957 |
) |
| 958 |
_result, dom = run_and_parse("-o", f"junit_logging={junit_logging}") |
| 959 |
node = dom.get_first_by_tag("testsuite") |
| 960 |
pnode = node.get_first_by_tag("testcase") |
| 961 |
if junit_logging == "no": |
| 962 |
assert not node.find_by_tag("system-err"), ( |
| 963 |
"system-err should not be generated" |
| 964 |
) |
| 965 |
if junit_logging == "system-err": |
| 966 |
systemerr = pnode.get_first_by_tag("system-err") |
| 967 |
assert "hello-stderr" in systemerr.toxml(), ( |
| 968 |
"'hello-stderr' should be in system-err" |
| 969 |
) |
| 970 |
|
| 971 |
@pytest.mark.parametrize("junit_logging", ["no", "system-out"]) |
| 972 |
def test_avoid_double_stdout( |
| 973 |
self, pytester: Pytester, run_and_parse: RunAndParse, junit_logging: str |
| 974 |
) -> None: |
| 975 |
pytester.makepyfile( |
| 976 |
""" |
| 977 |
import sys |
| 978 |
import pytest |
| 979 |
|
| 980 |
@pytest.fixture |
| 981 |
def arg(request): |
| 982 |
yield |
| 983 |
sys.stdout.write('hello-stdout teardown') |
| 984 |
raise ValueError() |
| 985 |
def test_function(arg): |
| 986 |
sys.stdout.write('hello-stdout call') |
| 987 |
""" |
| 988 |
) |
| 989 |
_result, dom = run_and_parse("-o", f"junit_logging={junit_logging}") |
| 990 |
node = dom.get_first_by_tag("testsuite") |
| 991 |
pnode = node.get_first_by_tag("testcase") |
| 992 |
if junit_logging == "no": |
| 993 |
assert not node.find_by_tag("system-out"), ( |
| 994 |
"system-out should not be generated" |
| 995 |
) |
| 996 |
if junit_logging == "system-out": |
| 997 |
systemout = pnode.get_first_by_tag("system-out") |
| 998 |
assert "hello-stdout call" in systemout.toxml() |
| 999 |
assert "hello-stdout teardown" in systemout.toxml() |
| 1000 |
|
| 1001 |
|
| 1002 |
def test_mangle_test_address() -> None: |
| 1003 |
from _pytest.junitxml import mangle_test_address |
| 1004 |
|
| 1005 |
address = "::".join(["a/my.py.thing.py", "Class", "method", "[a-1-::]"]) |
| 1006 |
newnames = mangle_test_address(address) |
| 1007 |
assert newnames == ["a.my.py.thing", "Class", "method", "[a-1-::]"] |
| 1008 |
|
| 1009 |
|
| 1010 |
def test_dont_configure_on_workers(tmp_path: Path) -> None: |
| 1011 |
gotten: list[object] = [] |
| 1012 |
|
| 1013 |
class FakeConfig: |
| 1014 |
if TYPE_CHECKING: |
| 1015 |
workerinput = None |
| 1016 |
|
| 1017 |
def __init__(self) -> None: |
| 1018 |
self.pluginmanager = self |
| 1019 |
self.option = self |
| 1020 |
self.stash = Stash() |
| 1021 |
|
| 1022 |
def getini(self, name: str) -> str: |
| 1023 |
return "pytest" |
| 1024 |
|
| 1025 |
junitprefix = None |
| 1026 |
# XXX: shouldn't need tmp_path ? |
| 1027 |
xmlpath = str(tmp_path.joinpath("junix.xml")) |
| 1028 |
register = gotten.append |
| 1029 |
|
| 1030 |
fake_config = cast(Config, FakeConfig()) |
| 1031 |
from _pytest import junitxml |
| 1032 |
|
| 1033 |
junitxml.pytest_configure(fake_config) |
| 1034 |
assert len(gotten) == 1 |
| 1035 |
FakeConfig.workerinput = None |
| 1036 |
junitxml.pytest_configure(fake_config) |
| 1037 |
assert len(gotten) == 1 |
| 1038 |
|
| 1039 |
|
| 1040 |
class TestNonPython: |
| 1041 |
@parametrize_families |
| 1042 |
def test_summing_simple( |
| 1043 |
self, pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 1044 |
) -> None: |
| 1045 |
pytester.makeconftest( |
| 1046 |
""" |
| 1047 |
import pytest |
| 1048 |
def pytest_collect_file(file_path, parent): |
| 1049 |
if file_path.suffix == ".xyz": |
| 1050 |
return MyItem.from_parent(name=file_path.name, parent=parent) |
| 1051 |
class MyItem(pytest.Item): |
| 1052 |
def runtest(self): |
| 1053 |
raise ValueError(42) |
| 1054 |
def repr_failure(self, excinfo): |
| 1055 |
return "custom item runtest failed" |
| 1056 |
""" |
| 1057 |
) |
| 1058 |
pytester.path.joinpath("myfile.xyz").write_text("hello", encoding="utf-8") |
| 1059 |
result, dom = run_and_parse(family=xunit_family) |
| 1060 |
assert result.ret |
| 1061 |
node = dom.get_first_by_tag("testsuite") |
| 1062 |
node.assert_attr(errors=0, failures=1, skipped=0, tests=1) |
| 1063 |
tnode = node.get_first_by_tag("testcase") |
| 1064 |
tnode.assert_attr(name="myfile.xyz") |
| 1065 |
fnode = tnode.get_first_by_tag("failure") |
| 1066 |
fnode.assert_attr(message="custom item runtest failed") |
| 1067 |
assert "custom item runtest failed" in fnode.toxml() |
| 1068 |
|
| 1069 |
|
| 1070 |
@pytest.mark.parametrize("junit_logging", ["no", "system-out"]) |
| 1071 |
def test_nullbyte(pytester: Pytester, junit_logging: str) -> None: |
| 1072 |
# A null byte cannot occur in XML (see section 2.2 of the spec) |
| 1073 |
pytester.makepyfile( |
| 1074 |
""" |
| 1075 |
import sys |
| 1076 |
def test_print_nullbyte(): |
| 1077 |
sys.stdout.write('Here the null -->' + chr(0) + '<--') |
| 1078 |
sys.stdout.write('In repr form -->' + repr(chr(0)) + '<--') |
| 1079 |
assert False |
| 1080 |
""" |
| 1081 |
) |
| 1082 |
xmlf = pytester.path.joinpath("junit.xml") |
| 1083 |
pytester.runpytest(f"--junitxml={xmlf}", "-o", f"junit_logging={junit_logging}") |
| 1084 |
text = xmlf.read_text(encoding="utf-8") |
| 1085 |
assert "\x00" not in text |
| 1086 |
if junit_logging == "system-out": |
| 1087 |
assert "#x00" in text |
| 1088 |
if junit_logging == "no": |
| 1089 |
assert "#x00" not in text |
| 1090 |
|
| 1091 |
|
| 1092 |
@pytest.mark.parametrize("junit_logging", ["no", "system-out"]) |
| 1093 |
def test_nullbyte_replace(pytester: Pytester, junit_logging: str) -> None: |
| 1094 |
# Check if the null byte gets replaced |
| 1095 |
pytester.makepyfile( |
| 1096 |
""" |
| 1097 |
import sys |
| 1098 |
def test_print_nullbyte(): |
| 1099 |
sys.stdout.write('Here the null -->' + chr(0) + '<--') |
| 1100 |
sys.stdout.write('In repr form -->' + repr(chr(0)) + '<--') |
| 1101 |
assert False |
| 1102 |
""" |
| 1103 |
) |
| 1104 |
xmlf = pytester.path.joinpath("junit.xml") |
| 1105 |
pytester.runpytest(f"--junitxml={xmlf}", "-o", f"junit_logging={junit_logging}") |
| 1106 |
text = xmlf.read_text(encoding="utf-8") |
| 1107 |
if junit_logging == "system-out": |
| 1108 |
assert "#x0" in text |
| 1109 |
if junit_logging == "no": |
| 1110 |
assert "#x0" not in text |
| 1111 |
|
| 1112 |
|
| 1113 |
def test_invalid_xml_escape() -> None: |
| 1114 |
# Test some more invalid xml chars, the full range should be |
| 1115 |
# tested really but let's just test the edges of the ranges |
| 1116 |
# instead. |
| 1117 |
# XXX Testing 0xD (\r) is tricky as it overwrites the just written |
| 1118 |
# line in the output, so we skip it too. |
| 1119 |
invalid = ( |
| 1120 |
0x00, |
| 1121 |
0x1, |
| 1122 |
0xB, |
| 1123 |
0xC, |
| 1124 |
0xE, |
| 1125 |
0x19, |
| 1126 |
27, # issue #126 |
| 1127 |
0xD800, |
| 1128 |
0xDFFF, |
| 1129 |
0xFFFE, |
| 1130 |
0x0FFFF, |
| 1131 |
) |
| 1132 |
valid = (0x9, 0xA, 0x20, 0xD, 0xD7FF, 0xE000, 0xFFFD, 0x10000, 0x10FFFF) |
| 1133 |
|
| 1134 |
for i in invalid: |
| 1135 |
got = bin_xml_escape(chr(i)) |
| 1136 |
if i <= 0xFF: |
| 1137 |
expected = f"#x{i:02X}" |
| 1138 |
else: |
| 1139 |
expected = f"#x{i:04X}" |
| 1140 |
assert got == expected |
| 1141 |
for i in valid: |
| 1142 |
assert chr(i) == bin_xml_escape(chr(i)) |
| 1143 |
|
| 1144 |
|
| 1145 |
def test_logxml_path_expansion(tmp_path: Path, monkeypatch: MonkeyPatch) -> None: |
| 1146 |
home_tilde = Path(os.path.expanduser("~")).joinpath("test.xml") |
| 1147 |
xml_tilde = LogXML(Path("~", "test.xml"), None) |
| 1148 |
assert xml_tilde.logfile == str(home_tilde) |
| 1149 |
|
| 1150 |
monkeypatch.setenv("HOME", str(tmp_path)) |
| 1151 |
home_var = os.path.normpath(os.path.expandvars("$HOME/test.xml")) |
| 1152 |
xml_var = LogXML(Path("$HOME", "test.xml"), None) |
| 1153 |
assert xml_var.logfile == str(home_var) |
| 1154 |
|
| 1155 |
|
| 1156 |
def test_logxml_changingdir(pytester: Pytester) -> None: |
| 1157 |
pytester.makepyfile( |
| 1158 |
""" |
| 1159 |
def test_func(): |
| 1160 |
import os |
| 1161 |
os.chdir("a") |
| 1162 |
""" |
| 1163 |
) |
| 1164 |
pytester.mkdir("a") |
| 1165 |
result = pytester.runpytest("--junitxml=a/x.xml") |
| 1166 |
assert result.ret == 0 |
| 1167 |
assert pytester.path.joinpath("a/x.xml").exists() |
| 1168 |
|
| 1169 |
|
| 1170 |
def test_logxml_makedir(pytester: Pytester) -> None: |
| 1171 |
"""--junitxml should automatically create directories for the xml file""" |
| 1172 |
pytester.makepyfile( |
| 1173 |
""" |
| 1174 |
def test_pass(): |
| 1175 |
pass |
| 1176 |
""" |
| 1177 |
) |
| 1178 |
result = pytester.runpytest("--junitxml=path/to/results.xml") |
| 1179 |
assert result.ret == 0 |
| 1180 |
assert pytester.path.joinpath("path/to/results.xml").exists() |
| 1181 |
|
| 1182 |
|
| 1183 |
def test_logxml_check_isdir(pytester: Pytester) -> None: |
| 1184 |
"""Give an error if --junit-xml is a directory (#2089)""" |
| 1185 |
result = pytester.runpytest("--junit-xml=.") |
| 1186 |
result.stderr.fnmatch_lines(["*--junitxml must be a filename*"]) |
| 1187 |
|
| 1188 |
|
| 1189 |
def test_escaped_parametrized_names_xml( |
| 1190 |
pytester: Pytester, run_and_parse: RunAndParse |
| 1191 |
) -> None: |
| 1192 |
pytester.makepyfile( |
| 1193 |
"""\ |
| 1194 |
import pytest |
| 1195 |
@pytest.mark.parametrize('char', ["\\x00"]) |
| 1196 |
def test_func(char): |
| 1197 |
assert char |
| 1198 |
""" |
| 1199 |
) |
| 1200 |
result, dom = run_and_parse() |
| 1201 |
assert result.ret == 0 |
| 1202 |
node = dom.get_first_by_tag("testcase") |
| 1203 |
node.assert_attr(name="test_func[\\x00]") |
| 1204 |
|
| 1205 |
|
| 1206 |
def test_double_colon_split_function_issue469( |
| 1207 |
pytester: Pytester, run_and_parse: RunAndParse |
| 1208 |
) -> None: |
| 1209 |
pytester.makepyfile( |
| 1210 |
""" |
| 1211 |
import pytest |
| 1212 |
@pytest.mark.parametrize('param', ["double::colon"]) |
| 1213 |
def test_func(param): |
| 1214 |
pass |
| 1215 |
""" |
| 1216 |
) |
| 1217 |
result, dom = run_and_parse() |
| 1218 |
assert result.ret == 0 |
| 1219 |
node = dom.get_first_by_tag("testcase") |
| 1220 |
node.assert_attr(classname="test_double_colon_split_function_issue469") |
| 1221 |
node.assert_attr(name="test_func[double::colon]") |
| 1222 |
|
| 1223 |
|
| 1224 |
def test_double_colon_split_method_issue469( |
| 1225 |
pytester: Pytester, run_and_parse: RunAndParse |
| 1226 |
) -> None: |
| 1227 |
pytester.makepyfile( |
| 1228 |
""" |
| 1229 |
import pytest |
| 1230 |
class TestClass(object): |
| 1231 |
@pytest.mark.parametrize('param', ["double::colon"]) |
| 1232 |
def test_func(self, param): |
| 1233 |
pass |
| 1234 |
""" |
| 1235 |
) |
| 1236 |
result, dom = run_and_parse() |
| 1237 |
assert result.ret == 0 |
| 1238 |
node = dom.get_first_by_tag("testcase") |
| 1239 |
node.assert_attr(classname="test_double_colon_split_method_issue469.TestClass") |
| 1240 |
node.assert_attr(name="test_func[double::colon]") |
| 1241 |
|
| 1242 |
|
| 1243 |
def test_unicode_issue368(pytester: Pytester) -> None: |
| 1244 |
path = pytester.path.joinpath("test.xml") |
| 1245 |
log = LogXML(str(path), None) |
| 1246 |
ustr = "ВНИ!" |
| 1247 |
|
| 1248 |
class Report(BaseReport): |
| 1249 |
longrepr = ustr |
| 1250 |
sections: list[tuple[str, str]] = [] |
| 1251 |
nodeid = "something" |
| 1252 |
location = "tests/filename.py", 42, "TestClass.method" |
| 1253 |
when = "teardown" |
| 1254 |
|
| 1255 |
test_report = cast(TestReport, Report()) |
| 1256 |
|
| 1257 |
# hopefully this is not too brittle ... |
| 1258 |
log.pytest_sessionstart() |
| 1259 |
node_reporter = log._opentestcase(test_report) |
| 1260 |
node_reporter.append_failure(test_report) |
| 1261 |
node_reporter.append_collect_error(test_report) |
| 1262 |
node_reporter.append_collect_skipped(test_report) |
| 1263 |
node_reporter.append_error(test_report) |
| 1264 |
test_report.longrepr = "filename", 1, ustr |
| 1265 |
node_reporter.append_skipped(test_report) |
| 1266 |
test_report.longrepr = "filename", 1, "Skipped: 卡嘣嘣" |
| 1267 |
node_reporter.append_skipped(test_report) |
| 1268 |
test_report.wasxfail = ustr |
| 1269 |
node_reporter.append_skipped(test_report) |
| 1270 |
log.pytest_sessionfinish() |
| 1271 |
|
| 1272 |
|
| 1273 |
def test_record_property(pytester: Pytester, run_and_parse: RunAndParse) -> None: |
| 1274 |
pytester.makepyfile( |
| 1275 |
""" |
| 1276 |
import pytest |
| 1277 |
|
| 1278 |
@pytest.fixture |
| 1279 |
def other(record_property): |
| 1280 |
record_property("bar", 1) |
| 1281 |
def test_record(record_property, other): |
| 1282 |
record_property("foo", "<1"); |
| 1283 |
""" |
| 1284 |
) |
| 1285 |
result, dom = run_and_parse() |
| 1286 |
node = dom.get_first_by_tag("testsuite") |
| 1287 |
tnode = node.get_first_by_tag("testcase") |
| 1288 |
psnode = tnode.get_first_by_tag("properties") |
| 1289 |
pnodes = psnode.find_by_tag("property") |
| 1290 |
pnodes[0].assert_attr(name="bar", value="1") |
| 1291 |
pnodes[1].assert_attr(name="foo", value="<1") |
| 1292 |
result.stdout.fnmatch_lines(["*= 1 passed in *"]) |
| 1293 |
|
| 1294 |
|
| 1295 |
def test_record_property_on_test_and_teardown_failure( |
| 1296 |
pytester: Pytester, run_and_parse: RunAndParse |
| 1297 |
) -> None: |
| 1298 |
pytester.makepyfile( |
| 1299 |
""" |
| 1300 |
import pytest |
| 1301 |
|
| 1302 |
@pytest.fixture |
| 1303 |
def other(record_property): |
| 1304 |
record_property("bar", 1) |
| 1305 |
yield |
| 1306 |
assert 0 |
| 1307 |
|
| 1308 |
def test_record(record_property, other): |
| 1309 |
record_property("foo", "<1") |
| 1310 |
assert 0 |
| 1311 |
""" |
| 1312 |
) |
| 1313 |
result, dom = run_and_parse() |
| 1314 |
node = dom.get_first_by_tag("testsuite") |
| 1315 |
tnodes = node.find_by_tag("testcase") |
| 1316 |
for tnode in tnodes: |
| 1317 |
psnode = tnode.get_first_by_tag("properties") |
| 1318 |
assert psnode, f"testcase didn't had expected properties:\n{tnode}" |
| 1319 |
pnodes = psnode.find_by_tag("property") |
| 1320 |
pnodes[0].assert_attr(name="bar", value="1") |
| 1321 |
pnodes[1].assert_attr(name="foo", value="<1") |
| 1322 |
result.stdout.fnmatch_lines(["*= 1 failed, 1 error *"]) |
| 1323 |
|
| 1324 |
|
| 1325 |
def test_record_property_same_name( |
| 1326 |
pytester: Pytester, run_and_parse: RunAndParse |
| 1327 |
) -> None: |
| 1328 |
pytester.makepyfile( |
| 1329 |
""" |
| 1330 |
def test_record_with_same_name(record_property): |
| 1331 |
record_property("foo", "bar") |
| 1332 |
record_property("foo", "baz") |
| 1333 |
""" |
| 1334 |
) |
| 1335 |
_result, dom = run_and_parse() |
| 1336 |
node = dom.get_first_by_tag("testsuite") |
| 1337 |
tnode = node.get_first_by_tag("testcase") |
| 1338 |
psnode = tnode.get_first_by_tag("properties") |
| 1339 |
pnodes = psnode.find_by_tag("property") |
| 1340 |
pnodes[0].assert_attr(name="foo", value="bar") |
| 1341 |
pnodes[1].assert_attr(name="foo", value="baz") |
| 1342 |
|
| 1343 |
|
| 1344 |
@pytest.mark.parametrize("fixture_name", ["record_property", "record_xml_attribute"]) |
| 1345 |
def test_record_fixtures_without_junitxml( |
| 1346 |
pytester: Pytester, fixture_name: str |
| 1347 |
) -> None: |
| 1348 |
pytester.makepyfile( |
| 1349 |
f""" |
| 1350 |
def test_record({fixture_name}): |
| 1351 |
{fixture_name}("foo", "bar") |
| 1352 |
""" |
| 1353 |
) |
| 1354 |
result = pytester.runpytest() |
| 1355 |
assert result.ret == 0 |
| 1356 |
|
| 1357 |
|
| 1358 |
@pytest.mark.filterwarnings("default") |
| 1359 |
def test_record_attribute(pytester: Pytester, run_and_parse: RunAndParse) -> None: |
| 1360 |
pytester.makeini( |
| 1361 |
""" |
| 1362 |
[pytest] |
| 1363 |
junit_family = xunit1 |
| 1364 |
""" |
| 1365 |
) |
| 1366 |
pytester.makepyfile( |
| 1367 |
""" |
| 1368 |
import pytest |
| 1369 |
|
| 1370 |
@pytest.fixture |
| 1371 |
def other(record_xml_attribute): |
| 1372 |
record_xml_attribute("bar", 1) |
| 1373 |
def test_record(record_xml_attribute, other): |
| 1374 |
record_xml_attribute("foo", "<1"); |
| 1375 |
""" |
| 1376 |
) |
| 1377 |
result, dom = run_and_parse() |
| 1378 |
node = dom.get_first_by_tag("testsuite") |
| 1379 |
tnode = node.get_first_by_tag("testcase") |
| 1380 |
tnode.assert_attr(bar="1") |
| 1381 |
tnode.assert_attr(foo="<1") |
| 1382 |
result.stdout.fnmatch_lines( |
| 1383 |
["*test_record_attribute.py:6:*record_xml_attribute is an experimental feature"] |
| 1384 |
) |
| 1385 |
|
| 1386 |
|
| 1387 |
@pytest.mark.filterwarnings("default") |
| 1388 |
@pytest.mark.parametrize("fixture_name", ["record_xml_attribute", "record_property"]) |
| 1389 |
def test_record_fixtures_xunit2( |
| 1390 |
pytester: Pytester, fixture_name: str, run_and_parse: RunAndParse |
| 1391 |
) -> None: |
| 1392 |
"""Ensure record_xml_attribute and record_property drop values when outside of legacy family.""" |
| 1393 |
pytester.makeini( |
| 1394 |
""" |
| 1395 |
[pytest] |
| 1396 |
junit_family = xunit2 |
| 1397 |
""" |
| 1398 |
) |
| 1399 |
pytester.makepyfile( |
| 1400 |
f""" |
| 1401 |
import pytest |
| 1402 |
|
| 1403 |
@pytest.fixture |
| 1404 |
def other({fixture_name}): |
| 1405 |
{fixture_name}("bar", 1) |
| 1406 |
def test_record({fixture_name}, other): |
| 1407 |
{fixture_name}("foo", "<1"); |
| 1408 |
""" |
| 1409 |
) |
| 1410 |
|
| 1411 |
result, _dom = run_and_parse(family=None) |
| 1412 |
expected_lines = [] |
| 1413 |
if fixture_name == "record_xml_attribute": |
| 1414 |
expected_lines.append( |
| 1415 |
"*test_record_fixtures_xunit2.py:6:*record_xml_attribute is an experimental feature" |
| 1416 |
) |
| 1417 |
expected_lines = [ |
| 1418 |
f"*test_record_fixtures_xunit2.py:6:*{fixture_name} is incompatible " |
| 1419 |
"with junit_family 'xunit2' (use 'legacy' or 'xunit1')" |
| 1420 |
] |
| 1421 |
result.stdout.fnmatch_lines(expected_lines) |
| 1422 |
|
| 1423 |
|
| 1424 |
def test_random_report_log_xdist( |
| 1425 |
pytester: Pytester, monkeypatch: MonkeyPatch, run_and_parse: RunAndParse |
| 1426 |
) -> None: |
| 1427 |
"""`xdist` calls pytest_runtest_logreport as they are executed by the workers, |
| 1428 |
with nodes from several nodes overlapping, so junitxml must cope with that |
| 1429 |
to produce correct reports (#1064).""" |
| 1430 |
pytest.importorskip("xdist") |
| 1431 |
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) |
| 1432 |
pytester.makepyfile( |
| 1433 |
""" |
| 1434 |
import pytest, time |
| 1435 |
@pytest.mark.parametrize('i', list(range(30))) |
| 1436 |
def test_x(i): |
| 1437 |
assert i != 22 |
| 1438 |
""" |
| 1439 |
) |
| 1440 |
_, dom = run_and_parse("-n2") |
| 1441 |
suite_node = dom.get_first_by_tag("testsuite") |
| 1442 |
failed = [] |
| 1443 |
for case_node in suite_node.find_by_tag("testcase"): |
| 1444 |
if case_node.find_first_by_tag("failure"): |
| 1445 |
failed.append(case_node["name"]) |
| 1446 |
|
| 1447 |
assert failed == ["test_x[22]"] |
| 1448 |
|
| 1449 |
|
| 1450 |
@parametrize_families |
| 1451 |
def test_root_testsuites_tag( |
| 1452 |
pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 1453 |
) -> None: |
| 1454 |
pytester.makepyfile( |
| 1455 |
""" |
| 1456 |
def test_x(): |
| 1457 |
pass |
| 1458 |
""" |
| 1459 |
) |
| 1460 |
_, dom = run_and_parse(family=xunit_family) |
| 1461 |
root = dom.get_unique_child |
| 1462 |
assert root.tag == "testsuites" |
| 1463 |
root.assert_attr(name="pytest tests") |
| 1464 |
suite_node = root.get_unique_child |
| 1465 |
assert suite_node.tag == "testsuite" |
| 1466 |
|
| 1467 |
|
| 1468 |
def test_runs_twice(pytester: Pytester, run_and_parse: RunAndParse) -> None: |
| 1469 |
f = pytester.makepyfile( |
| 1470 |
""" |
| 1471 |
def test_pass(): |
| 1472 |
pass |
| 1473 |
""" |
| 1474 |
) |
| 1475 |
|
| 1476 |
result, dom = run_and_parse("--keep-duplicates", f, f) |
| 1477 |
result.stdout.no_fnmatch_line("*INTERNALERROR*") |
| 1478 |
first, second = (x["classname"] for x in dom.find_by_tag("testcase")) |
| 1479 |
assert first == second |
| 1480 |
|
| 1481 |
|
| 1482 |
def test_runs_twice_xdist( |
| 1483 |
pytester: Pytester, monkeypatch: MonkeyPatch, run_and_parse: RunAndParse |
| 1484 |
) -> None: |
| 1485 |
pytest.importorskip("xdist") |
| 1486 |
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD") |
| 1487 |
f = pytester.makepyfile( |
| 1488 |
""" |
| 1489 |
def test_pass(): |
| 1490 |
pass |
| 1491 |
""" |
| 1492 |
) |
| 1493 |
|
| 1494 |
result, dom = run_and_parse(f, "--dist", "each", "--tx", "2*popen") |
| 1495 |
result.stdout.no_fnmatch_line("*INTERNALERROR*") |
| 1496 |
first, second = (x["classname"] for x in dom.find_by_tag("testcase")) |
| 1497 |
assert first == second |
| 1498 |
|
| 1499 |
|
| 1500 |
def test_fancy_items_regression(pytester: Pytester, run_and_parse: RunAndParse) -> None: |
| 1501 |
# issue 1259 |
| 1502 |
pytester.makeconftest( |
| 1503 |
""" |
| 1504 |
import pytest |
| 1505 |
class FunItem(pytest.Item): |
| 1506 |
def runtest(self): |
| 1507 |
pass |
| 1508 |
class NoFunItem(pytest.Item): |
| 1509 |
def runtest(self): |
| 1510 |
pass |
| 1511 |
|
| 1512 |
class FunCollector(pytest.File): |
| 1513 |
def collect(self): |
| 1514 |
return [ |
| 1515 |
FunItem.from_parent(name='a', parent=self), |
| 1516 |
NoFunItem.from_parent(name='a', parent=self), |
| 1517 |
NoFunItem.from_parent(name='b', parent=self), |
| 1518 |
] |
| 1519 |
|
| 1520 |
def pytest_collect_file(file_path, parent): |
| 1521 |
if file_path.suffix == '.py': |
| 1522 |
return FunCollector.from_parent(path=file_path, parent=parent) |
| 1523 |
""" |
| 1524 |
) |
| 1525 |
|
| 1526 |
pytester.makepyfile( |
| 1527 |
""" |
| 1528 |
def test_pass(): |
| 1529 |
pass |
| 1530 |
""" |
| 1531 |
) |
| 1532 |
|
| 1533 |
result, dom = run_and_parse() |
| 1534 |
|
| 1535 |
result.stdout.no_fnmatch_line("*INTERNALERROR*") |
| 1536 |
|
| 1537 |
items = sorted( |
| 1538 |
f"{x['classname']} {x['name']}" |
| 1539 |
# dom is a DomNode not a mapping, it's not possible to ** it. |
| 1540 |
for x in dom.find_by_tag("testcase") |
| 1541 |
) |
| 1542 |
|
| 1543 |
import pprint |
| 1544 |
|
| 1545 |
pprint.pprint(items) |
| 1546 |
assert items == [ |
| 1547 |
"conftest a", |
| 1548 |
"conftest a", |
| 1549 |
"conftest b", |
| 1550 |
"test_fancy_items_regression a", |
| 1551 |
"test_fancy_items_regression a", |
| 1552 |
"test_fancy_items_regression b", |
| 1553 |
"test_fancy_items_regression test_pass", |
| 1554 |
] |
| 1555 |
|
| 1556 |
|
| 1557 |
@parametrize_families |
| 1558 |
def test_global_properties(pytester: Pytester, xunit_family: str) -> None: |
| 1559 |
path = pytester.path.joinpath("test_global_properties.xml") |
| 1560 |
log = LogXML(str(path), None, family=xunit_family) |
| 1561 |
|
| 1562 |
class Report(BaseReport): |
| 1563 |
sections: list[tuple[str, str]] = [] |
| 1564 |
nodeid = "test_node_id" |
| 1565 |
|
| 1566 |
log.pytest_sessionstart() |
| 1567 |
log.add_global_property("foo", "1") |
| 1568 |
log.add_global_property("bar", "2") |
| 1569 |
log.pytest_sessionfinish() |
| 1570 |
|
| 1571 |
dom = minidom.parse(str(path)) |
| 1572 |
|
| 1573 |
properties = dom.getElementsByTagName("properties") |
| 1574 |
|
| 1575 |
assert properties.length == 1, "There must be one <properties> node" |
| 1576 |
|
| 1577 |
property_list = dom.getElementsByTagName("property") |
| 1578 |
|
| 1579 |
assert property_list.length == 2, "There most be only 2 property nodes" |
| 1580 |
|
| 1581 |
expected = {"foo": "1", "bar": "2"} |
| 1582 |
actual = {} |
| 1583 |
|
| 1584 |
for p in property_list: |
| 1585 |
k = str(p.getAttribute("name")) |
| 1586 |
v = str(p.getAttribute("value")) |
| 1587 |
actual[k] = v |
| 1588 |
|
| 1589 |
assert actual == expected |
| 1590 |
|
| 1591 |
|
| 1592 |
def test_url_property(pytester: Pytester) -> None: |
| 1593 |
test_url = "http://www.github.com/pytest-dev" |
| 1594 |
path = pytester.path.joinpath("test_url_property.xml") |
| 1595 |
log = LogXML(str(path), None) |
| 1596 |
|
| 1597 |
class Report(BaseReport): |
| 1598 |
longrepr = "FooBarBaz" |
| 1599 |
sections: list[tuple[str, str]] = [] |
| 1600 |
nodeid = "something" |
| 1601 |
location = "tests/filename.py", 42, "TestClass.method" |
| 1602 |
url = test_url |
| 1603 |
|
| 1604 |
test_report = cast(TestReport, Report()) |
| 1605 |
|
| 1606 |
log.pytest_sessionstart() |
| 1607 |
node_reporter = log._opentestcase(test_report) |
| 1608 |
node_reporter.append_failure(test_report) |
| 1609 |
log.pytest_sessionfinish() |
| 1610 |
|
| 1611 |
test_case = minidom.parse(str(path)).getElementsByTagName("testcase")[0] |
| 1612 |
|
| 1613 |
assert test_case.getAttribute("url") == test_url, ( |
| 1614 |
"The URL did not get written to the xml" |
| 1615 |
) |
| 1616 |
|
| 1617 |
|
| 1618 |
@parametrize_families |
| 1619 |
def test_record_testsuite_property( |
| 1620 |
pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 1621 |
) -> None: |
| 1622 |
pytester.makepyfile( |
| 1623 |
""" |
| 1624 |
def test_func1(record_testsuite_property): |
| 1625 |
record_testsuite_property("stats", "all good") |
| 1626 |
|
| 1627 |
def test_func2(record_testsuite_property): |
| 1628 |
record_testsuite_property("stats", 10) |
| 1629 |
""" |
| 1630 |
) |
| 1631 |
result, dom = run_and_parse(family=xunit_family) |
| 1632 |
assert result.ret == 0 |
| 1633 |
node = dom.get_first_by_tag("testsuite") |
| 1634 |
properties_node = node.get_first_by_tag("properties") |
| 1635 |
p1_node, p2_node = properties_node.find_by_tag( |
| 1636 |
"property", |
| 1637 |
)[:2] |
| 1638 |
p1_node.assert_attr(name="stats", value="all good") |
| 1639 |
p2_node.assert_attr(name="stats", value="10") |
| 1640 |
|
| 1641 |
|
| 1642 |
def test_record_testsuite_property_junit_disabled(pytester: Pytester) -> None: |
| 1643 |
pytester.makepyfile( |
| 1644 |
""" |
| 1645 |
def test_func1(record_testsuite_property): |
| 1646 |
record_testsuite_property("stats", "all good") |
| 1647 |
""" |
| 1648 |
) |
| 1649 |
result = pytester.runpytest() |
| 1650 |
assert result.ret == 0 |
| 1651 |
|
| 1652 |
|
| 1653 |
@pytest.mark.parametrize("junit", [True, False]) |
| 1654 |
def test_record_testsuite_property_type_checking( |
| 1655 |
pytester: Pytester, junit: bool |
| 1656 |
) -> None: |
| 1657 |
pytester.makepyfile( |
| 1658 |
""" |
| 1659 |
def test_func1(record_testsuite_property): |
| 1660 |
record_testsuite_property(1, 2) |
| 1661 |
""" |
| 1662 |
) |
| 1663 |
args = ("--junitxml=tests.xml",) if junit else () |
| 1664 |
result = pytester.runpytest(*args) |
| 1665 |
assert result.ret == 1 |
| 1666 |
result.stdout.fnmatch_lines( |
| 1667 |
["*TypeError: name parameter needs to be a string, but int given"] |
| 1668 |
) |
| 1669 |
|
| 1670 |
|
| 1671 |
@pytest.mark.parametrize("suite_name", ["my_suite", ""]) |
| 1672 |
@parametrize_families |
| 1673 |
def test_set_suite_name( |
| 1674 |
pytester: Pytester, suite_name: str, run_and_parse: RunAndParse, xunit_family: str |
| 1675 |
) -> None: |
| 1676 |
if suite_name: |
| 1677 |
pytester.makeini( |
| 1678 |
f""" |
| 1679 |
[pytest] |
| 1680 |
junit_suite_name={suite_name} |
| 1681 |
junit_family={xunit_family} |
| 1682 |
""" |
| 1683 |
) |
| 1684 |
expected = suite_name |
| 1685 |
else: |
| 1686 |
expected = "pytest" |
| 1687 |
pytester.makepyfile( |
| 1688 |
""" |
| 1689 |
import pytest |
| 1690 |
|
| 1691 |
def test_func(): |
| 1692 |
pass |
| 1693 |
""" |
| 1694 |
) |
| 1695 |
result, dom = run_and_parse(family=xunit_family, suite_name=expected) |
| 1696 |
assert result.ret == 0 |
| 1697 |
node = dom.get_first_by_tag("testsuite") |
| 1698 |
node.assert_attr(name=expected) |
| 1699 |
|
| 1700 |
|
| 1701 |
def test_escaped_skipreason_issue3533( |
| 1702 |
pytester: Pytester, run_and_parse: RunAndParse |
| 1703 |
) -> None: |
| 1704 |
pytester.makepyfile( |
| 1705 |
""" |
| 1706 |
import pytest |
| 1707 |
@pytest.mark.skip(reason='1 <> 2') |
| 1708 |
def test_skip(): |
| 1709 |
pass |
| 1710 |
""" |
| 1711 |
) |
| 1712 |
_, dom = run_and_parse() |
| 1713 |
node = dom.get_first_by_tag("testcase") |
| 1714 |
snode = node.get_first_by_tag("skipped") |
| 1715 |
assert "1 <> 2" in snode.text |
| 1716 |
snode.assert_attr(message="1 <> 2") |
| 1717 |
|
| 1718 |
|
| 1719 |
def test_bin_escaped_skipreason(pytester: Pytester, run_and_parse: RunAndParse) -> None: |
| 1720 |
"""Escape special characters from mark.skip reason (#11842).""" |
| 1721 |
pytester.makepyfile( |
| 1722 |
""" |
| 1723 |
import pytest |
| 1724 |
@pytest.mark.skip("\33[31;1mred\33[0m") |
| 1725 |
def test_skip(): |
| 1726 |
pass |
| 1727 |
""" |
| 1728 |
) |
| 1729 |
_, dom = run_and_parse() |
| 1730 |
node = dom.get_first_by_tag("testcase") |
| 1731 |
snode = node.get_first_by_tag("skipped") |
| 1732 |
assert "#x1B[31;1mred#x1B[0m" in snode.text |
| 1733 |
snode.assert_attr(message="#x1B[31;1mred#x1B[0m") |
| 1734 |
|
| 1735 |
|
| 1736 |
def test_escaped_setup_teardown_error( |
| 1737 |
pytester: Pytester, run_and_parse: RunAndParse |
| 1738 |
) -> None: |
| 1739 |
pytester.makepyfile( |
| 1740 |
""" |
| 1741 |
import pytest |
| 1742 |
|
| 1743 |
@pytest.fixture() |
| 1744 |
def my_setup(): |
| 1745 |
raise Exception("error: \033[31mred\033[m") |
| 1746 |
|
| 1747 |
def test_esc(my_setup): |
| 1748 |
pass |
| 1749 |
""" |
| 1750 |
) |
| 1751 |
_, dom = run_and_parse() |
| 1752 |
node = dom.get_first_by_tag("testcase") |
| 1753 |
snode = node.get_first_by_tag("error") |
| 1754 |
assert "#x1B[31mred#x1B[m" in snode["message"] |
| 1755 |
assert "#x1B[31mred#x1B[m" in snode.text |
| 1756 |
|
| 1757 |
|
| 1758 |
@parametrize_families |
| 1759 |
def test_logging_passing_tests_disabled_does_not_log_test_output( |
| 1760 |
pytester: Pytester, run_and_parse: RunAndParse, xunit_family: str |
| 1761 |
) -> None: |
| 1762 |
pytester.makeini( |
| 1763 |
f""" |
| 1764 |
[pytest] |
| 1765 |
junit_log_passing_tests=False |
| 1766 |
junit_logging=system-out |
| 1767 |
junit_family={xunit_family} |
| 1768 |
""" |
| 1769 |
) |
| 1770 |
pytester.makepyfile( |
| 1771 |
""" |
| 1772 |
import pytest |
| 1773 |
import logging |
| 1774 |
import sys |
| 1775 |
|
| 1776 |
def test_func(): |
| 1777 |
sys.stdout.write('This is stdout') |
| 1778 |
sys.stderr.write('This is stderr') |
| 1779 |
logging.warning('hello') |
| 1780 |
""" |
| 1781 |
) |
| 1782 |
result, dom = run_and_parse(family=xunit_family) |
| 1783 |
assert result.ret == 0 |
| 1784 |
node = dom.get_first_by_tag("testcase") |
| 1785 |
assert len(node.find_by_tag("system-err")) == 0 |
| 1786 |
assert len(node.find_by_tag("system-out")) == 0 |
| 1787 |
|
| 1788 |
|
| 1789 |
@parametrize_families |
| 1790 |
@pytest.mark.parametrize("junit_logging", ["no", "system-out", "system-err"]) |
| 1791 |
def test_logging_passing_tests_disabled_logs_output_for_failing_test_issue5430( |
| 1792 |
pytester: Pytester, |
| 1793 |
junit_logging: str, |
| 1794 |
run_and_parse: RunAndParse, |
| 1795 |
xunit_family: str, |
| 1796 |
) -> None: |
| 1797 |
pytester.makeini( |
| 1798 |
f""" |
| 1799 |
[pytest] |
| 1800 |
junit_log_passing_tests=False |
| 1801 |
junit_family={xunit_family} |
| 1802 |
""" |
| 1803 |
) |
| 1804 |
pytester.makepyfile( |
| 1805 |
""" |
| 1806 |
import pytest |
| 1807 |
import logging |
| 1808 |
import sys |
| 1809 |
|
| 1810 |
def test_func(): |
| 1811 |
logging.warning('hello') |
| 1812 |
assert 0 |
| 1813 |
""" |
| 1814 |
) |
| 1815 |
result, dom = run_and_parse( |
| 1816 |
"-o", f"junit_logging={junit_logging}", family=xunit_family |
| 1817 |
) |
| 1818 |
assert result.ret == 1 |
| 1819 |
node = dom.get_first_by_tag("testcase") |
| 1820 |
if junit_logging == "system-out": |
| 1821 |
assert len(node.find_by_tag("system-err")) == 0 |
| 1822 |
assert len(node.find_by_tag("system-out")) == 1 |
| 1823 |
elif junit_logging == "system-err": |
| 1824 |
assert len(node.find_by_tag("system-err")) == 1 |
| 1825 |
assert len(node.find_by_tag("system-out")) == 0 |
| 1826 |
else: |
| 1827 |
assert junit_logging == "no" |
| 1828 |
assert len(node.find_by_tag("system-err")) == 0 |
| 1829 |
assert len(node.find_by_tag("system-out")) == 0 |
| 1830 |
|
| 1831 |
|
| 1832 |
def test_no_message_quiet(pytester: Pytester) -> None: |
| 1833 |
"""Do not show the summary banner when --quiet is given (#13700).""" |
| 1834 |
pytester.makepyfile("def test(): pass") |
| 1835 |
result = pytester.runpytest("--junitxml=pytest.xml") |
| 1836 |
result.stdout.fnmatch_lines("* generated xml file: *") |
| 1837 |
|
| 1838 |
result = pytester.runpytest("--junitxml=pytest.xml", "--quiet") |
| 1839 |
result.stdout.no_fnmatch_line("* generated xml file: *") |