Debian Codegraph @

hypothesis-python/tests/pytest/test_junit.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 import xml.etree.ElementTree as ET
12 from pathlib import Path
13
14 pytest_plugins = "pytester"
15
16
17 TESTSUITE = """
18 from hypothesis import given
19 from hypothesis.strategies import integers
20
21 @given(integers())
22 def test_valid(x):
23 assert x == x
24
25 @given(integers())
26 def test_invalid(x):
27 assert x != x
28 """
29
30
31 def _run_and_get_junit(testdir, *args):
32 script = testdir.makepyfile(TESTSUITE)
33 testdir.runpytest(script, "--junit-xml=out.xml", *args)
34 return ET.parse(Path(testdir.tmpdir) / "out.xml").getroot()
35
36
37 def _findall_from_root(junit_xml, path):
38 if junit_xml.tag == "testsuites":
39 return junit_xml.findall(f"./testsuite/{path}")
40 # This case only exists for tests against Pytest before 5.1.0;
41 # see https://github.com/pytest-dev/pytest/commit/a43ba78d3bde
42 return junit_xml.findall(f"./{path}")
43
44
45 def suite_properties_ok(junit_xml):
46 # Check whether <properties> is included in <testsuite>. This is currently not
47 # the case when using pytest-xdist, which is a shame, but we can live with it.
48 testsuite_props = _findall_from_root(junit_xml, "properties")
49 return len(testsuite_props) == 1 and {
50 prop.get("name") for prop in testsuite_props[0].findall("property")
51 } == {
52 "hypothesis-statistics-test_outputs_valid_xunit2.py::test_valid",
53 "hypothesis-statistics-test_outputs_valid_xunit2.py::test_invalid",
54 }
55
56
57 def test_outputs_valid_xunit2(testdir):
58 # The thing we really care about with pytest-xdist + junitxml is that we don't
59 # break xunit2 compatibility by putting <properties> inside <testcase>.
60 junit_xml = _run_and_get_junit(testdir)
61 testcase_props = _findall_from_root(junit_xml, "testcase/properties")
62 assert len(testcase_props) == 0
63 # Check whether <properties> is included in <testsuite>
64 assert suite_properties_ok(junit_xml)
65
66
67 def test_outputs_valid_xunit2_with_xdist(testdir):
68 junit_xml = _run_and_get_junit(testdir, "-n2")
69 testcase_props = _findall_from_root(junit_xml, "testcase/properties")
70 assert len(testcase_props) == 0
71 # If <properties> is included in <testsuite>, this assertion will fail.
72 # That would be a GOOD THING, and we would remove the `not` to prevent regressions.
73 assert not suite_properties_ok(junit_xml)