Skip to content

Tests: Improved parametrization #964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ $ pipx install --suffix=@next 'tmuxp' --pip-args '\--pre' --force

- _Future release notes will be placed here_

### Development

- Tests: Improve parametrized test suite (#964)

Convert remaining `pytest.mark.parametrize()` tests to `NamedTuple` fixtures:

- Improved test maintainability and readability
- Added descriptive test IDs for better failure reporting
- Added docstrings to test fixtures
- 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`

## tmuxp 1.52.2 (2025-02-02)

### Bug fixes
Expand Down
28 changes: 23 additions & 5 deletions tests/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,32 @@ def test_creates_config_dir_not_exists(tmp_path: pathlib.Path) -> None:
assert tmp_path.exists()


class HelpTestFixture(t.NamedTuple):
"""Test fixture for help command tests."""

test_id: str
cli_args: list[str]


HELP_TEST_FIXTURES: list[HelpTestFixture] = [
HelpTestFixture(
test_id="help_long_flag",
cli_args=["--help"],
),
HelpTestFixture(
test_id="help_short_flag",
cli_args=["-h"],
),
]


@pytest.mark.parametrize(
"cli_args",
[
(["--help"]),
(["-h"]),
],
list(HelpTestFixture._fields),
HELP_TEST_FIXTURES,
ids=[test.test_id for test in HELP_TEST_FIXTURES],
)
def test_help(
test_id: str,
cli_args: list[str],
tmp_path: pathlib.Path,
monkeypatch: pytest.MonkeyPatch,
Expand Down
76 changes: 62 additions & 14 deletions tests/cli/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,44 @@
import pathlib


class ConvertTestFixture(t.NamedTuple):
"""Test fixture for tmuxp convert command tests."""

test_id: str
cli_args: list[str]


CONVERT_TEST_FIXTURES: list[ConvertTestFixture] = [
ConvertTestFixture(
test_id="convert_current_dir",
cli_args=["convert", "."],
),
ConvertTestFixture(
test_id="convert_yaml_file",
cli_args=["convert", ".tmuxp.yaml"],
),
ConvertTestFixture(
test_id="convert_yaml_file_auto_confirm",
cli_args=["convert", ".tmuxp.yaml", "-y"],
),
ConvertTestFixture(
test_id="convert_yml_file",
cli_args=["convert", ".tmuxp.yml"],
),
ConvertTestFixture(
test_id="convert_yml_file_auto_confirm",
cli_args=["convert", ".tmuxp.yml", "-y"],
),
]


@pytest.mark.parametrize(
"cli_args",
[
(["convert", "."]),
(["convert", ".tmuxp.yaml"]),
(["convert", ".tmuxp.yaml", "-y"]),
(["convert", ".tmuxp.yml"]),
(["convert", ".tmuxp.yml", "-y"]),
],
list(ConvertTestFixture._fields),
CONVERT_TEST_FIXTURES,
ids=[test.test_id for test in CONVERT_TEST_FIXTURES],
)
def test_convert(
test_id: str,
cli_args: list[str],
tmp_path: pathlib.Path,
monkeypatch: pytest.MonkeyPatch,
Expand Down Expand Up @@ -57,15 +84,36 @@ def test_convert(
assert tmuxp_json.open().read() == json.dumps({"session_name": "hello"}, indent=2)


class ConvertJsonTestFixture(t.NamedTuple):
"""Test fixture for tmuxp convert json command tests."""

test_id: str
cli_args: list[str]


CONVERT_JSON_TEST_FIXTURES: list[ConvertJsonTestFixture] = [
ConvertJsonTestFixture(
test_id="convert_json_current_dir",
cli_args=["convert", "."],
),
ConvertJsonTestFixture(
test_id="convert_json_file",
cli_args=["convert", ".tmuxp.json"],
),
ConvertJsonTestFixture(
test_id="convert_json_file_auto_confirm",
cli_args=["convert", ".tmuxp.json", "-y"],
),
]


@pytest.mark.parametrize(
"cli_args",
[
(["convert", "."]),
(["convert", ".tmuxp.json"]),
(["convert", ".tmuxp.json", "-y"]),
],
list(ConvertJsonTestFixture._fields),
CONVERT_JSON_TEST_FIXTURES,
ids=[test.test_id for test in CONVERT_JSON_TEST_FIXTURES],
)
def test_convert_json(
test_id: str,
cli_args: list[str],
tmp_path: pathlib.Path,
monkeypatch: pytest.MonkeyPatch,
Expand Down
86 changes: 62 additions & 24 deletions tests/cli/test_freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,68 @@
from libtmux.server import Server


class FreezeTestFixture(t.NamedTuple):
"""Test fixture for tmuxp freeze command tests."""

test_id: str
cli_args: list[str]
inputs: list[str]


class FreezeOverwriteTestFixture(t.NamedTuple):
"""Test fixture for tmuxp freeze overwrite command tests."""

test_id: str
cli_args: list[str]
inputs: list[str]


FREEZE_TEST_FIXTURES: list[FreezeTestFixture] = [
FreezeTestFixture(
test_id="freeze_named_session",
cli_args=["freeze", "myfrozensession"],
inputs=["y\n", "./la.yaml\n", "y\n"],
),
FreezeTestFixture(
test_id="freeze_named_session_exists",
cli_args=["freeze", "myfrozensession"],
inputs=["y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"],
),
FreezeTestFixture(
test_id="freeze_current_session",
cli_args=["freeze"],
inputs=["y\n", "./la.yaml\n", "y\n"],
),
FreezeTestFixture(
test_id="freeze_current_session_exists",
cli_args=["freeze"],
inputs=["y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"],
),
]


FREEZE_OVERWRITE_TEST_FIXTURES: list[FreezeOverwriteTestFixture] = [
FreezeOverwriteTestFixture(
test_id="force_overwrite_named_session",
cli_args=["freeze", "mysession", "--force"],
inputs=["\n", "\n", "y\n", "./exists.yaml\n", "y\n"],
),
FreezeOverwriteTestFixture(
test_id="force_overwrite_current_session",
cli_args=["freeze", "--force"],
inputs=["\n", "\n", "y\n", "./exists.yaml\n", "y\n"],
),
]


@pytest.mark.parametrize(
("cli_args", "inputs"),
[
(["freeze", "myfrozensession"], ["y\n", "./la.yaml\n", "y\n"]),
( # Exists
["freeze", "myfrozensession"],
["y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"],
),
( # Imply current session if not entered
["freeze"],
["y\n", "./la.yaml\n", "y\n"],
),
(["freeze"], ["y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"]), # Exists
],
list(FreezeTestFixture._fields),
FREEZE_TEST_FIXTURES,
ids=[test.test_id for test in FREEZE_TEST_FIXTURES],
)
def test_freeze(
server: Server,
test_id: str,
cli_args: list[str],
inputs: list[str],
tmp_path: pathlib.Path,
Expand Down Expand Up @@ -72,20 +117,13 @@ def test_freeze(


@pytest.mark.parametrize(
("cli_args", "inputs"),
[
( # Overwrite
["freeze", "mysession", "--force"],
["\n", "\n", "y\n", "./exists.yaml\n", "y\n"],
),
( # Imply current session if not entered
["freeze", "--force"],
["\n", "\n", "y\n", "./exists.yaml\n", "y\n"],
),
],
list(FreezeOverwriteTestFixture._fields),
FREEZE_OVERWRITE_TEST_FIXTURES,
ids=[test.test_id for test in FREEZE_OVERWRITE_TEST_FIXTURES],
)
def test_freeze_overwrite(
server: Server,
test_id: str,
cli_args: list[str],
inputs: list[str],
tmp_path: pathlib.Path,
Expand Down
Loading