Skip to content

Commit c556ec4

Browse files
authored
refactor(tests): Convert parametrized tests to NamedTuple fixtures (#964)
Convert remaining pytest.mark.parametrize() tests to use NamedTuple fixtures for improved maintainability, readability, and type safety. Files updated: - test_cli.py: Added HelpTestFixture - test_convert.py: Added ConvertTestFixture and ConvertJsonTestFixture - test_freeze.py: Added FreezeTestFixture and FreezeOverwriteTestFixture - test_import.py: Added ImportTestFixture, ImportTeamocilTestFixture, and ImportTmuxinatorTestFixture - test_load.py: Added ZshAutotitleTestFixture, LogFileTestFixture, PluginVersionTestFixture, and PluginMissingTestFixture - ..., et al. Each fixture includes docstrings and descriptive test IDs for better failure reporting. No functional changes - purely test structure improvements.
2 parents 5fbc021 + 25d7238 commit c556ec4

12 files changed

+916
-358
lines changed

CHANGES

+16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ $ pipx install --suffix=@next 'tmuxp' --pip-args '\--pre' --force
2121

2222
- _Future release notes will be placed here_
2323

24+
### Development
25+
26+
- Tests: Improve parametrized test suite (#964)
27+
28+
Convert remaining `pytest.mark.parametrize()` tests to `NamedTuple` fixtures:
29+
30+
- Improved test maintainability and readability
31+
- Added descriptive test IDs for better failure reporting
32+
- Added docstrings to test fixtures
33+
- Files updated:
34+
- `test_cli.py`: Added `HelpTestFixture`
35+
- `test_convert.py`: Added `ConvertTestFixture` and `ConvertJsonTestFixture`
36+
- `test_freeze.py`: Added `FreezeTestFixture` and `FreezeOverwriteTestFixture`
37+
- `test_import.py`: Added `ImportTestFixture`, `ImportTeamocilTestFixture`, and `ImportTmuxinatorTestFixture`
38+
- `test_load.py`: Added `ZshAutotitleTestFixture`, `LogFileTestFixture`, `PluginVersionTestFixture`, and `PluginMissingTestFixture`
39+
2440
## tmuxp 1.52.2 (2025-02-02)
2541

2642
### Bug fixes

tests/cli/test_cli.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,32 @@ def test_creates_config_dir_not_exists(tmp_path: pathlib.Path) -> None:
3131
assert tmp_path.exists()
3232

3333

34+
class HelpTestFixture(t.NamedTuple):
35+
"""Test fixture for help command tests."""
36+
37+
test_id: str
38+
cli_args: list[str]
39+
40+
41+
HELP_TEST_FIXTURES: list[HelpTestFixture] = [
42+
HelpTestFixture(
43+
test_id="help_long_flag",
44+
cli_args=["--help"],
45+
),
46+
HelpTestFixture(
47+
test_id="help_short_flag",
48+
cli_args=["-h"],
49+
),
50+
]
51+
52+
3453
@pytest.mark.parametrize(
35-
"cli_args",
36-
[
37-
(["--help"]),
38-
(["-h"]),
39-
],
54+
list(HelpTestFixture._fields),
55+
HELP_TEST_FIXTURES,
56+
ids=[test.test_id for test in HELP_TEST_FIXTURES],
4057
)
4158
def test_help(
59+
test_id: str,
4260
cli_args: list[str],
4361
tmp_path: pathlib.Path,
4462
monkeypatch: pytest.MonkeyPatch,

tests/cli/test_convert.py

+62-14
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,44 @@
1515
import pathlib
1616

1717

18+
class ConvertTestFixture(t.NamedTuple):
19+
"""Test fixture for tmuxp convert command tests."""
20+
21+
test_id: str
22+
cli_args: list[str]
23+
24+
25+
CONVERT_TEST_FIXTURES: list[ConvertTestFixture] = [
26+
ConvertTestFixture(
27+
test_id="convert_current_dir",
28+
cli_args=["convert", "."],
29+
),
30+
ConvertTestFixture(
31+
test_id="convert_yaml_file",
32+
cli_args=["convert", ".tmuxp.yaml"],
33+
),
34+
ConvertTestFixture(
35+
test_id="convert_yaml_file_auto_confirm",
36+
cli_args=["convert", ".tmuxp.yaml", "-y"],
37+
),
38+
ConvertTestFixture(
39+
test_id="convert_yml_file",
40+
cli_args=["convert", ".tmuxp.yml"],
41+
),
42+
ConvertTestFixture(
43+
test_id="convert_yml_file_auto_confirm",
44+
cli_args=["convert", ".tmuxp.yml", "-y"],
45+
),
46+
]
47+
48+
1849
@pytest.mark.parametrize(
19-
"cli_args",
20-
[
21-
(["convert", "."]),
22-
(["convert", ".tmuxp.yaml"]),
23-
(["convert", ".tmuxp.yaml", "-y"]),
24-
(["convert", ".tmuxp.yml"]),
25-
(["convert", ".tmuxp.yml", "-y"]),
26-
],
50+
list(ConvertTestFixture._fields),
51+
CONVERT_TEST_FIXTURES,
52+
ids=[test.test_id for test in CONVERT_TEST_FIXTURES],
2753
)
2854
def test_convert(
55+
test_id: str,
2956
cli_args: list[str],
3057
tmp_path: pathlib.Path,
3158
monkeypatch: pytest.MonkeyPatch,
@@ -57,15 +84,36 @@ def test_convert(
5784
assert tmuxp_json.open().read() == json.dumps({"session_name": "hello"}, indent=2)
5885

5986

87+
class ConvertJsonTestFixture(t.NamedTuple):
88+
"""Test fixture for tmuxp convert json command tests."""
89+
90+
test_id: str
91+
cli_args: list[str]
92+
93+
94+
CONVERT_JSON_TEST_FIXTURES: list[ConvertJsonTestFixture] = [
95+
ConvertJsonTestFixture(
96+
test_id="convert_json_current_dir",
97+
cli_args=["convert", "."],
98+
),
99+
ConvertJsonTestFixture(
100+
test_id="convert_json_file",
101+
cli_args=["convert", ".tmuxp.json"],
102+
),
103+
ConvertJsonTestFixture(
104+
test_id="convert_json_file_auto_confirm",
105+
cli_args=["convert", ".tmuxp.json", "-y"],
106+
),
107+
]
108+
109+
60110
@pytest.mark.parametrize(
61-
"cli_args",
62-
[
63-
(["convert", "."]),
64-
(["convert", ".tmuxp.json"]),
65-
(["convert", ".tmuxp.json", "-y"]),
66-
],
111+
list(ConvertJsonTestFixture._fields),
112+
CONVERT_JSON_TEST_FIXTURES,
113+
ids=[test.test_id for test in CONVERT_JSON_TEST_FIXTURES],
67114
)
68115
def test_convert_json(
116+
test_id: str,
69117
cli_args: list[str],
70118
tmp_path: pathlib.Path,
71119
monkeypatch: pytest.MonkeyPatch,

tests/cli/test_freeze.py

+62-24
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,68 @@
1717
from libtmux.server import Server
1818

1919

20+
class FreezeTestFixture(t.NamedTuple):
21+
"""Test fixture for tmuxp freeze command tests."""
22+
23+
test_id: str
24+
cli_args: list[str]
25+
inputs: list[str]
26+
27+
28+
class FreezeOverwriteTestFixture(t.NamedTuple):
29+
"""Test fixture for tmuxp freeze overwrite command tests."""
30+
31+
test_id: str
32+
cli_args: list[str]
33+
inputs: list[str]
34+
35+
36+
FREEZE_TEST_FIXTURES: list[FreezeTestFixture] = [
37+
FreezeTestFixture(
38+
test_id="freeze_named_session",
39+
cli_args=["freeze", "myfrozensession"],
40+
inputs=["y\n", "./la.yaml\n", "y\n"],
41+
),
42+
FreezeTestFixture(
43+
test_id="freeze_named_session_exists",
44+
cli_args=["freeze", "myfrozensession"],
45+
inputs=["y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"],
46+
),
47+
FreezeTestFixture(
48+
test_id="freeze_current_session",
49+
cli_args=["freeze"],
50+
inputs=["y\n", "./la.yaml\n", "y\n"],
51+
),
52+
FreezeTestFixture(
53+
test_id="freeze_current_session_exists",
54+
cli_args=["freeze"],
55+
inputs=["y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"],
56+
),
57+
]
58+
59+
60+
FREEZE_OVERWRITE_TEST_FIXTURES: list[FreezeOverwriteTestFixture] = [
61+
FreezeOverwriteTestFixture(
62+
test_id="force_overwrite_named_session",
63+
cli_args=["freeze", "mysession", "--force"],
64+
inputs=["\n", "\n", "y\n", "./exists.yaml\n", "y\n"],
65+
),
66+
FreezeOverwriteTestFixture(
67+
test_id="force_overwrite_current_session",
68+
cli_args=["freeze", "--force"],
69+
inputs=["\n", "\n", "y\n", "./exists.yaml\n", "y\n"],
70+
),
71+
]
72+
73+
2074
@pytest.mark.parametrize(
21-
("cli_args", "inputs"),
22-
[
23-
(["freeze", "myfrozensession"], ["y\n", "./la.yaml\n", "y\n"]),
24-
( # Exists
25-
["freeze", "myfrozensession"],
26-
["y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"],
27-
),
28-
( # Imply current session if not entered
29-
["freeze"],
30-
["y\n", "./la.yaml\n", "y\n"],
31-
),
32-
(["freeze"], ["y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"]), # Exists
33-
],
75+
list(FreezeTestFixture._fields),
76+
FREEZE_TEST_FIXTURES,
77+
ids=[test.test_id for test in FREEZE_TEST_FIXTURES],
3478
)
3579
def test_freeze(
3680
server: Server,
81+
test_id: str,
3782
cli_args: list[str],
3883
inputs: list[str],
3984
tmp_path: pathlib.Path,
@@ -72,20 +117,13 @@ def test_freeze(
72117

73118

74119
@pytest.mark.parametrize(
75-
("cli_args", "inputs"),
76-
[
77-
( # Overwrite
78-
["freeze", "mysession", "--force"],
79-
["\n", "\n", "y\n", "./exists.yaml\n", "y\n"],
80-
),
81-
( # Imply current session if not entered
82-
["freeze", "--force"],
83-
["\n", "\n", "y\n", "./exists.yaml\n", "y\n"],
84-
),
85-
],
120+
list(FreezeOverwriteTestFixture._fields),
121+
FREEZE_OVERWRITE_TEST_FIXTURES,
122+
ids=[test.test_id for test in FREEZE_OVERWRITE_TEST_FIXTURES],
86123
)
87124
def test_freeze_overwrite(
88125
server: Server,
126+
test_id: str,
89127
cli_args: list[str],
90128
inputs: list[str],
91129
tmp_path: pathlib.Path,

0 commit comments

Comments
 (0)