summaryrefslogtreecommitdiffstats
path: root/library/python/pytest/ut/test_tools.py
blob: 99e093aa303047852a30641f64f8ad67869e6c71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import pytest
import sys

from library.python.pytest import config
from library.python.pytest import yatest_tools

config.set_test_mode()


@pytest.fixture(params=["", "[param1,param2]"])
def parameters(request):
    return request.param


@pytest.mark.parametrize(
    "node_id,expected_class_name,expected_test_name",
    (
        ("package.test_script.py::test_name", "package.test_script.py", "test_name"),
        ("package.test_script.py", "package.test_script.py", "package.test_script.py"),
        ("package.test_script.py::class_name::test_name", "package.test_script.py::class_name", "test_name"),
        (
            "package.test_script.py::class_name::subclass_name::test_name",
            "package.test_script.py::class_name",
            "test_name",
        ),
    ),
)
def test_split_node_id_without_path(parameters, node_id, expected_class_name, expected_test_name):
    got = yatest_tools.split_node_id(node_id + parameters)
    assert (expected_class_name, expected_test_name + parameters) == got


@pytest.mark.parametrize(
    "node_id,expected_class_name,expected_test_name",
    (
        ("/arcadia/root/package/test_script.py", "package.test_script.py", "package.test_script.py"),
        ("/arcadia/root/package/test_script.py::test_name", "package.test_script.py", "test_name"),
        (
            "/arcadia/root/package/test_script.py::class_name::test_name",
            "package.test_script.py::class_name",
            "test_name",
        ),
        (
            "/arcadia/root/package/test_script.py::class_name::subclass_name::test_name",
            "package.test_script.py::class_name",
            "test_name",
        ),
    ),
)
def test_split_node_id_with_path(monkeypatch, parameters, node_id, expected_class_name, expected_test_name):
    mapping = {
        '/arcadia/root/package/test_script.py': 'package.test_script',
    }
    with monkeypatch.context() as context:
        context.setattr(sys, 'extra_modules', sys.extra_modules | {'__tests__.package.test_script'})
        context.setattr(yatest_tools, '_nodeid_to_display_module_name_mapping', lambda: mapping)
        got = yatest_tools.split_node_id(node_id + parameters)
        assert (expected_class_name, expected_test_name + parameters) == got


def test_missing_module(parameters):
    # An errno must be raised if module is not found in sys.extra_modules
    with pytest.raises(yatest_tools.MissingTestModule):
        yatest_tools.split_node_id("/arcadia/root/package/test_script2.py::test_name" + parameters)


@pytest.mark.parametrize(
    "node_id,expected_class_name,expected_test_name",
    (
        ("package.test_script.py::test_name", "package.test_script.py", "test_suffix"),
        ("package.test_script.py", "package.test_script.py", "test_suffix"),
        ("package.test_script.py::class_name::test_name", "package.test_script.py", "test_suffix"),
        ("package.test_script.py::class_name::subclass_name::test_name", "package.test_script.py", "test_suffix"),
    ),
)
def test_split_node_id_with_test_suffix(parameters, node_id, expected_class_name, expected_test_name):
    got = yatest_tools.split_node_id(node_id + parameters, "test_suffix")
    assert (expected_class_name, expected_test_name + parameters) == got


@pytest.mark.parametrize(
    "node_id,expected_class_name,expected_test_name",
    [
        ("/arcadia/data/b/a/test.py::test_b_a", "b.a.test.py", "test_b_a"),
        ("/arcadia/data/a/test.py::test_a", "a.test.py", "test_a"),
        ("/arcadia/data/test.py::test", "test.py", "test"),
    ],
)
def test_path_resolving_for_local_conftest_load_policy(
    monkeypatch, parameters, node_id, expected_class_name, expected_test_name
):
    extra_modules = [
        '__tests__.b.a.test',
        '__tests__.test',
        '__tests__.a.test',
    ]
    mapping = {
        '/arcadia/data/b/a/test.py': 'b.a.test',
        '/arcadia/data/test.py': 'test',
        '/arcadia/data/a/test.py': 'a.test',
    }
    with monkeypatch.context() as context:
        context.setattr(sys, 'extra_modules', extra_modules)
        context.setattr(yatest_tools, '_nodeid_to_display_module_name_mapping', lambda: mapping)
        got = yatest_tools.split_node_id(node_id + parameters)
        assert (expected_class_name, expected_test_name + parameters) == got


DATA = [
    (
        "simple_bt",
        """
path/test.py:15: in test
    return foo(0)
path/test.py:5: in foo
    b, a = 1, 1 / p
E   ZeroDivisionError: integer division or modulo by zero
""",
    ),
    (
        "nested_by",
        """
path/test.py:13: in test
    raise Exception("123\n123\npath/test.py:15: in test")
E   Exception: 123
E   123
E   path/test.py:15: in test

During handling of the above exception, another exception occurred:
path/test.py:15: in test
    return foo(0)
path/test.py:5: in foo
    b, a = 1, 1 / p
E   ZeroDivisionError: division by zero
""",
    ),
]


@pytest.mark.parametrize("bt", [x[1] for x in DATA], ids=([x[0] for x in DATA]))
def test_colorize_pytest_error(bt):
    return yatest_tools.colorize_pytest_error(bt)