Skip to content

Commit 83250b4

Browse files
authored
Improve test function names (#3852)
1 parent 031038e commit 83250b4

13 files changed

+114
-77
lines changed

.config/requirements-test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ psutil # soft-dep of pytest-xdist
99
pylint # IDE support
1010
pytest >= 7.2.2
1111
pytest-mock
12-
pytest-plus >= 0.2 # for PYTEST_REQPASS
12+
pytest-plus >= 0.6 # for PYTEST_REQPASS
1313
pytest-xdist >= 2.1.0
1414
ruamel.yaml>=0.17.31,<0.18 # only the latest is expected to pass our tests
1515
ruamel-yaml-clib # needed for mypy

.config/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pylint==2.17.6
8181
pymdown-extensions==10.3
8282
pytest==7.4.2
8383
pytest-mock==3.11.1
84-
pytest-plus==0.4.0
84+
pytest-plus==0.6.0
8585
pytest-xdist==3.3.1
8686
python-dateutil==2.8.2
8787
python-slugify==8.0.1

src/ansiblelint/rules/schema.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,12 @@ def matchyaml(self, file: Lintable) -> list[MatchError]:
232232
],
233233
id="execution-environment-broken",
234234
),
235-
("examples/meta/runtime.yml", "meta-runtime", []),
235+
pytest.param(
236+
"examples/meta/runtime.yml",
237+
"meta-runtime",
238+
[],
239+
id="meta-runtime",
240+
),
236241
pytest.param(
237242
"examples/broken_collection_meta_runtime/meta/runtime.yml",
238243
"meta-runtime",

src/ansiblelint/schemas/__store__.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/inventory.json"
2525
},
2626
"meta": {
27-
"etag": "80d83d5c61044aa67496ea22c74aef08b0216c20e318400d3c939927a2761d51",
27+
"etag": "9eb5c611e25cc9e0a180119904f8dfe39c59409b37da972f66a6c60f040a3a08",
2828
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/meta.json"
2929
},
3030
"meta-runtime": {

test/test_file_path_evaluation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@
105105
@pytest.mark.parametrize(
106106
"ansible_project_layout",
107107
(
108-
pytest.param(LAYOUT_IMPORTS, id="using only import_tasks"),
109-
pytest.param(LAYOUT_INCLUDES, id="using only include_tasks"),
108+
pytest.param(LAYOUT_IMPORTS, id="using-only-import_tasks"),
109+
pytest.param(LAYOUT_INCLUDES, id="using-only-include_tasks"),
110110
),
111111
)
112112
def test_file_path_evaluation(

test/test_formatter_base.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@
1212
@pytest.mark.parametrize(
1313
("base_dir", "relative_path"),
1414
(
15-
(None, True),
16-
("/whatever", False),
17-
(Path("/whatever"), False),
15+
pytest.param(None, True, id="0"),
16+
pytest.param("/whatever", False, id="1"),
17+
pytest.param(Path("/whatever"), False, id="2"),
18+
),
19+
)
20+
@pytest.mark.parametrize(
21+
"path",
22+
(
23+
pytest.param("/whatever/string", id="a"),
24+
pytest.param(Path("/whatever/string"), id="b"),
1825
),
1926
)
20-
@pytest.mark.parametrize("path", ("/whatever/string", Path("/whatever/string")))
2127
def test_base_formatter_when_base_dir(
2228
base_dir: Any,
2329
relative_path: bool,
@@ -44,11 +50,17 @@ def test_base_formatter_when_base_dir(
4450
@pytest.mark.parametrize(
4551
"base_dir",
4652
(
47-
Path("/whatever"),
48-
"/whatever",
53+
pytest.param(Path("/whatever"), id="0"),
54+
pytest.param("/whatever", id="1"),
55+
),
56+
)
57+
@pytest.mark.parametrize(
58+
"path",
59+
(
60+
pytest.param("/whatever/string", id="a"),
61+
pytest.param(Path("/whatever/string"), id="b"),
4962
),
5063
)
51-
@pytest.mark.parametrize("path", ("/whatever/string", Path("/whatever/string")))
5264
def test_base_formatter_when_base_dir_is_given_and_relative_is_true(
5365
path: str | Path,
5466
base_dir: str | Path,

test/test_formatter_json.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def setup_class(self) -> None:
5353
display_relative_path=True,
5454
)
5555

56-
def test_format_list(self) -> None:
56+
def test_json_format_list(self) -> None:
5757
"""Test if the return value is a string."""
5858
assert isinstance(self.formatter, CodeclimateJSONFormatter)
5959
assert isinstance(self.formatter.format_result(self.matches), str)
@@ -66,7 +66,7 @@ def test_result_is_json(self) -> None:
6666
# https://github.com/ansible/ansible-navigator/issues/1490
6767
assert "\n" not in output
6868

69-
def test_single_match(self) -> None:
69+
def test_json_single_match(self) -> None:
7070
"""Test negative case. Only lists are allowed. Otherwise a RuntimeError will be raised."""
7171
assert isinstance(self.formatter, CodeclimateJSONFormatter)
7272
with pytest.raises(RuntimeError):

test/test_formatter_sarif.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,20 @@ def setup_class(self) -> None:
7676

7777
self.formatter = SarifFormatter(pathlib.Path.cwd(), display_relative_path=True)
7878

79-
def test_format_list(self) -> None:
79+
def test_sarif_format_list(self) -> None:
8080
"""Test if the return value is a string."""
8181
assert isinstance(self.formatter, SarifFormatter)
8282
assert isinstance(self.formatter.format_result(self.matches), str)
8383

84-
def test_result_is_json(self) -> None:
84+
def test_sarif_result_is_json(self) -> None:
8585
"""Test if returned string value is a JSON."""
8686
assert isinstance(self.formatter, SarifFormatter)
8787
output = self.formatter.format_result(self.matches)
8888
json.loads(output)
8989
# https://github.com/ansible/ansible-navigator/issues/1490
9090
assert "\n" not in output
9191

92-
def test_single_match(self) -> None:
92+
def test_sarif_single_match(self) -> None:
9393
"""Test negative case. Only lists are allowed. Otherwise, a RuntimeError will be raised."""
9494
assert isinstance(self.formatter, SarifFormatter)
9595
with pytest.raises(RuntimeError):
@@ -182,8 +182,8 @@ def test_sarif_parsable_ignored() -> None:
182182
@pytest.mark.parametrize(
183183
("file", "return_code"),
184184
(
185-
pytest.param("examples/playbooks/valid.yml", 0),
186-
pytest.param("playbook.yml", 2),
185+
pytest.param("examples/playbooks/valid.yml", 0, id="0"),
186+
pytest.param("playbook.yml", 2, id="1"),
187187
),
188188
)
189189
def test_sarif_file(file: str, return_code: int) -> None:
@@ -204,7 +204,7 @@ def test_sarif_file(file: str, return_code: int) -> None:
204204

205205
@pytest.mark.parametrize(
206206
("file", "return_code"),
207-
(pytest.param("examples/playbooks/valid.yml", 0),),
207+
(pytest.param("examples/playbooks/valid.yml", 0, id="0"),),
208208
)
209209
def test_sarif_file_creates_it_if_none_exists(file: str, return_code: int) -> None:
210210
"""Test ability to create sarif file if none exists and dump output to it (--sarif-file)."""

test/test_main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def test_call_from_outside_venv(expected_warning: bool) -> None:
5252
@pytest.mark.parametrize(
5353
("ver_diff", "found", "check", "outlen"),
5454
(
55-
("v1.2.2", True, "pre-release", 1),
56-
("v1.2.3", False, "", 1),
57-
("v1.2.4", True, "new release", 2),
55+
pytest.param("v1.2.2", True, "pre-release", 1, id="0"),
56+
pytest.param("v1.2.3", False, "", 1, id="1"),
57+
pytest.param("v1.2.4", True, "new release", 2, id="2"),
5858
),
5959
)
6060
def test_get_version_warning(

test/test_matcherrror.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,18 @@ def test_match_error_not_equal(
121121
@pytest.mark.parametrize(
122122
"other",
123123
(
124-
None,
125-
"foo",
126-
42,
127-
Exception("foo"),
124+
pytest.param(None, id="none"),
125+
pytest.param("foo", id="str"),
126+
pytest.param(42, id="int"),
127+
pytest.param(Exception("foo"), id="exc"),
128128
),
129129
ids=repr,
130130
)
131131
@pytest.mark.parametrize(
132132
("operation", "operator_char"),
133133
(
134-
pytest.param(operator.le, "<=", id="<="),
135-
pytest.param(operator.gt, ">", id=">"),
134+
pytest.param(operator.le, "<=", id="le"),
135+
pytest.param(operator.gt, ">", id="gt"),
136136
),
137137
)
138138
def test_matcherror_compare_no_other_fallback(
@@ -154,21 +154,20 @@ def test_matcherror_compare_no_other_fallback(
154154
@pytest.mark.parametrize(
155155
"other",
156156
(
157-
None,
158-
"foo",
159-
42,
160-
Exception("foo"),
161-
DummyTestObject(),
157+
pytest.param(None, id="none"),
158+
pytest.param("foo", id="str"),
159+
pytest.param(42, id="int"),
160+
pytest.param(Exception("foo"), id="exception"),
161+
pytest.param(DummyTestObject(), id="obj"),
162162
),
163163
ids=repr,
164164
)
165165
@pytest.mark.parametrize(
166166
("operation", "expected_value"),
167167
(
168-
(operator.eq, False),
169-
(operator.ne, True),
168+
pytest.param(operator.eq, False, id="eq"),
169+
pytest.param(operator.ne, True, id="ne"),
170170
),
171-
ids=("==", "!="),
172171
)
173172
def test_matcherror_compare_with_other_fallback(
174173
other: object,
@@ -182,16 +181,15 @@ def test_matcherror_compare_with_other_fallback(
182181
@pytest.mark.parametrize(
183182
("operation", "expected_value"),
184183
(
185-
(operator.eq, "EQ_SENTINEL"),
186-
(operator.ne, "NE_SENTINEL"),
184+
pytest.param(operator.eq, "EQ_SENTINEL", id="eq"),
185+
pytest.param(operator.ne, "NE_SENTINEL", id="ne"),
187186
# NOTE: these are swapped because when we do `x < y`, and `x.__lt__(y)`
188187
# NOTE: returns `NotImplemented`, Python will reverse the check into
189188
# NOTE: `y > x`, and so `y.__gt__(x) is called.
190189
# Ref: https://docs.python.org/3/reference/datamodel.html#object.__lt__
191-
(operator.lt, "GT_SENTINEL"),
192-
(operator.gt, "LT_SENTINEL"),
190+
pytest.param(operator.lt, "GT_SENTINEL", id="gt"),
191+
pytest.param(operator.gt, "LT_SENTINEL", id="lt"),
193192
),
194-
ids=("==", "!=", "<", ">"),
195193
)
196194
def test_matcherror_compare_with_dummy_sentinel(
197195
operation: Callable[..., bool],

test/test_runner.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ def test_runner_exclude_paths(default_rules_collection: RulesCollection) -> None
8686
assert len(matches) == 0
8787

8888

89-
@pytest.mark.parametrize(("exclude_path"), ("**/playbooks/*.yml",))
89+
@pytest.mark.parametrize(
90+
("exclude_path"),
91+
(pytest.param("**/playbooks/*.yml", id="1"),),
92+
)
9093
def test_runner_exclude_globs(
9194
default_rules_collection: RulesCollection,
9295
exclude_path: str,

test/test_skiputils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
@pytest.mark.parametrize(
4040
("line", "expected"),
4141
(
42-
("foo # noqa: bar", "bar"),
43-
("foo # noqa bar", "bar"),
42+
pytest.param("foo # noqa: bar", "bar", id="0"),
43+
pytest.param("foo # noqa bar", "bar", id="1"),
4444
),
4545
)
4646
def test_get_rule_skips_from_line(line: str, expected: str) -> None:

test/test_transform_mixin.py

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -55,72 +55,91 @@ def test_seek_with_bad_path(
5555
@pytest.mark.parametrize(
5656
("yaml_path", "data", "expected"),
5757
(
58-
([], DUMMY_MAP, DUMMY_MAP),
59-
(["foo"], DUMMY_MAP, DUMMY_MAP["foo"]),
60-
(["bar"], DUMMY_MAP, DUMMY_MAP["bar"]),
61-
(["bar", "some"], DUMMY_MAP, DUMMY_MAP["bar"]["some"]),
62-
(["fruits"], DUMMY_MAP, DUMMY_MAP["fruits"]),
63-
(["fruits", 0], DUMMY_MAP, DUMMY_MAP["fruits"][0]),
64-
(["fruits", 1], DUMMY_MAP, DUMMY_MAP["fruits"][1]),
65-
(["answer"], DUMMY_MAP, DUMMY_MAP["answer"]),
66-
(["answer", 0], DUMMY_MAP, DUMMY_MAP["answer"][0]),
67-
(["answer", 0, "forty-two"], DUMMY_MAP, DUMMY_MAP["answer"][0]["forty-two"]),
68-
(
58+
pytest.param([], DUMMY_MAP, DUMMY_MAP, id="0"),
59+
pytest.param(["foo"], DUMMY_MAP, DUMMY_MAP["foo"], id="1"),
60+
pytest.param(["bar"], DUMMY_MAP, DUMMY_MAP["bar"], id="2"),
61+
pytest.param(["bar", "some"], DUMMY_MAP, DUMMY_MAP["bar"]["some"], id="3"),
62+
pytest.param(["fruits"], DUMMY_MAP, DUMMY_MAP["fruits"], id="4"),
63+
pytest.param(["fruits", 0], DUMMY_MAP, DUMMY_MAP["fruits"][0], id="5"),
64+
pytest.param(["fruits", 1], DUMMY_MAP, DUMMY_MAP["fruits"][1], id="6"),
65+
pytest.param(["answer"], DUMMY_MAP, DUMMY_MAP["answer"], id="7"),
66+
pytest.param(["answer", 0], DUMMY_MAP, DUMMY_MAP["answer"][0], id="8"),
67+
pytest.param(
68+
["answer", 0, "forty-two"],
69+
DUMMY_MAP,
70+
DUMMY_MAP["answer"][0]["forty-two"],
71+
id="9",
72+
),
73+
pytest.param(
6974
["answer", 0, "forty-two", 0],
7075
DUMMY_MAP,
7176
DUMMY_MAP["answer"][0]["forty-two"][0],
77+
id="10",
7278
),
73-
(
79+
pytest.param(
7480
["answer", 0, "forty-two", 1],
7581
DUMMY_MAP,
7682
DUMMY_MAP["answer"][0]["forty-two"][1],
83+
id="11",
7784
),
78-
(
85+
pytest.param(
7986
["answer", 0, "forty-two", 2],
8087
DUMMY_MAP,
8188
DUMMY_MAP["answer"][0]["forty-two"][2],
89+
id="12",
90+
),
91+
pytest.param([], DUMMY_LIST, DUMMY_LIST, id="13"),
92+
pytest.param([0], DUMMY_LIST, DUMMY_LIST[0], id="14"),
93+
pytest.param([0, "foo"], DUMMY_LIST, DUMMY_LIST[0]["foo"], id="15"),
94+
pytest.param([1], DUMMY_LIST, DUMMY_LIST[1], id="16"),
95+
pytest.param([1, "bar"], DUMMY_LIST, DUMMY_LIST[1]["bar"], id="17"),
96+
pytest.param(
97+
[1, "bar", "some"],
98+
DUMMY_LIST,
99+
DUMMY_LIST[1]["bar"]["some"],
100+
id="18",
82101
),
83-
([], DUMMY_LIST, DUMMY_LIST),
84-
([0], DUMMY_LIST, DUMMY_LIST[0]),
85-
([0, "foo"], DUMMY_LIST, DUMMY_LIST[0]["foo"]),
86-
([1], DUMMY_LIST, DUMMY_LIST[1]),
87-
([1, "bar"], DUMMY_LIST, DUMMY_LIST[1]["bar"]),
88-
([1, "bar", "some"], DUMMY_LIST, DUMMY_LIST[1]["bar"]["some"]),
89-
([1, "fruits"], DUMMY_LIST, DUMMY_LIST[1]["fruits"]),
90-
([1, "fruits", 0], DUMMY_LIST, DUMMY_LIST[1]["fruits"][0]),
91-
([1, "fruits", 1], DUMMY_LIST, DUMMY_LIST[1]["fruits"][1]),
92-
([2], DUMMY_LIST, DUMMY_LIST[2]),
93-
([2, "answer"], DUMMY_LIST, DUMMY_LIST[2]["answer"]),
94-
([2, "answer", 0], DUMMY_LIST, DUMMY_LIST[2]["answer"][0]),
95-
(
102+
pytest.param([1, "fruits"], DUMMY_LIST, DUMMY_LIST[1]["fruits"], id="19"),
103+
pytest.param([1, "fruits", 0], DUMMY_LIST, DUMMY_LIST[1]["fruits"][0], id="20"),
104+
pytest.param([1, "fruits", 1], DUMMY_LIST, DUMMY_LIST[1]["fruits"][1], id="21"),
105+
pytest.param([2], DUMMY_LIST, DUMMY_LIST[2], id="22"),
106+
pytest.param([2, "answer"], DUMMY_LIST, DUMMY_LIST[2]["answer"], id="23"),
107+
pytest.param([2, "answer", 0], DUMMY_LIST, DUMMY_LIST[2]["answer"][0], id="24"),
108+
pytest.param(
96109
[2, "answer", 0, "forty-two"],
97110
DUMMY_LIST,
98111
DUMMY_LIST[2]["answer"][0]["forty-two"],
112+
id="25",
99113
),
100-
(
114+
pytest.param(
101115
[2, "answer", 0, "forty-two", 0],
102116
DUMMY_LIST,
103117
DUMMY_LIST[2]["answer"][0]["forty-two"][0],
118+
id="26",
104119
),
105-
(
120+
pytest.param(
106121
[2, "answer", 0, "forty-two", 1],
107122
DUMMY_LIST,
108123
DUMMY_LIST[2]["answer"][0]["forty-two"][1],
124+
id="27",
109125
),
110-
(
126+
pytest.param(
111127
[2, "answer", 0, "forty-two", 2],
112128
DUMMY_LIST,
113129
DUMMY_LIST[2]["answer"][0]["forty-two"][2],
130+
id="28",
114131
),
115-
(
132+
pytest.param(
116133
[],
117134
"this is a string that should be returned as is, ignoring path.",
118135
"this is a string that should be returned as is, ignoring path.",
136+
id="29",
119137
),
120-
(
138+
pytest.param(
121139
[2, "answer", 0, "forty-two", 2],
122140
"this is a string that should be returned as is, ignoring path.",
123141
"this is a string that should be returned as is, ignoring path.",
142+
id="30",
124143
),
125144
),
126145
)

0 commit comments

Comments
 (0)