How to use iterparentnodeids method in Pytest

Best Python code snippet using pytest

Run Pytest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

test_nodes.py

Source: test_nodes.py Github

copy
1from typing import List
2from typing import Type
3
4import py
5
6import pytest
7from _pytest import nodes
8from _pytest.pytester import Pytester
9from _pytest.warning_types import PytestWarning
10
11
12@pytest.mark.parametrize(
13    ("nodeid", "expected"),
14    (
15        ("", [""]),
16        ("a", ["", "a"]),
17        ("aa/b", ["", "aa", "aa/b"]),
18        ("a/b/c", ["", "a", "a/b", "a/b/c"]),
19        ("a/bbb/c::D", ["", "a", "a/bbb", "a/bbb/c", "a/bbb/c::D"]),
20        ("a/b/c::D::eee", ["", "a", "a/b", "a/b/c", "a/b/c::D", "a/b/c::D::eee"]),
21        # :: considered only at the last component.
22        ("::xx", ["", "::xx"]),
23        ("a/b/c::D/d::e", ["", "a", "a/b", "a/b/c::D", "a/b/c::D/d", "a/b/c::D/d::e"]),
24        # : alone is not a separator.
25        ("a/b::D:e:f::g", ["", "a", "a/b", "a/b::D:e:f", "a/b::D:e:f::g"]),
26    ),
27)
28def test_iterparentnodeids(nodeid: str, expected: List[str]) -> None:
29    result = list(nodes.iterparentnodeids(nodeid))
30    assert result == expected
31
32
33def test_node_from_parent_disallowed_arguments() -> None:
34    with pytest.raises(TypeError, match="session is"):
35        nodes.Node.from_parent(None, session=None)  # type: ignore[arg-type]
36    with pytest.raises(TypeError, match="config is"):
37        nodes.Node.from_parent(None, config=None)  # type: ignore[arg-type]
38
39
40@pytest.mark.parametrize(
41    "warn_type, msg", [(DeprecationWarning, "deprecated"), (PytestWarning, "pytest")]
42)
43def test_node_warn_is_no_longer_only_pytest_warnings(
44    pytester: Pytester, warn_type: Type[Warning], msg: str
45) -> None:
46    items = pytester.getitems(
47        """
48        def test():
49            pass
50    """
51    )
52    with pytest.warns(warn_type, match=msg):
53        items[0].warn(warn_type(msg))
54
55
56def test_node_warning_enforces_warning_types(pytester: Pytester) -> None:
57    items = pytester.getitems(
58        """
59        def test():
60            pass
61    """
62    )
63    with pytest.raises(
64        ValueError, match="warning must be an instance of Warning or subclass"
65    ):
66        items[0].warn(Exception("ok"))  # type: ignore[arg-type]
67
68
69def test__check_initialpaths_for_relpath() -> None:
70    """Ensure that it handles dirs, and does not always use dirname."""
71    cwd = py.path.local()
72
73    class FakeSession1:
74        _initialpaths = [cwd]
75
76    assert nodes._check_initialpaths_for_relpath(FakeSession1, cwd) == ""
77
78    sub = cwd.join("file")
79
80    class FakeSession2:
81        _initialpaths = [cwd]
82
83    assert nodes._check_initialpaths_for_relpath(FakeSession2, sub) == "file"
84
85    outside = py.path.local("/outside")
86    assert nodes._check_initialpaths_for_relpath(FakeSession2, outside) is None
87
88
89def test_failure_with_changed_cwd(pytester: Pytester) -> None:
90    """
91    Test failure lines should use absolute paths if cwd has changed since
92    invocation, so the path is correct (#6428).
93    """
94    p = pytester.makepyfile(
95        """
96        import os
97        import pytest
98
99        @pytest.fixture
100        def private_dir():
101            out_dir = 'ddd'
102            os.mkdir(out_dir)
103            old_dir = os.getcwd()
104            os.chdir(out_dir)
105            yield out_dir
106            os.chdir(old_dir)
107
108        def test_show_wrong_path(private_dir):
109            assert False
110    """
111    )
112    result = pytester.runpytest()
113    result.stdout.fnmatch_lines([str(p) + ":*: AssertionError", "*1 failed in *"])
114
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Pytest on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)