[PR #100] [MERGED] Use parametrization to simplify tests. #99

Closed
opened 2026-03-02 16:48:12 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/initialcommit-com/git-sim/pull/100
Author: @ehmatthes
Created: 7/17/2023
Status: Merged
Merged: 7/17/2023
Merged by: @initialcommit-io

Base: devHead: simplify_tests


📝 Commits (5)

  • 6db0a15 Parametrization works for two simple commands.
  • 6c8e426 All simple commands are parametrized.
  • 98c5f79 Removed redundant tests.
  • d9a1e7f All complex commands are parametrized.
  • 650fffc All commands are tested by one function.

📊 Changes

1 file changed (+40 additions, -253 deletions)

View changed files

📝 tests/e2e_tests/test_core_commands.py (+40 -253)

📄 Description

Working on #99, I played around with parametrization. I didn't think it was going to help, but it is pretty compelling. Instead of 18 separate test functions, there's only one test function. All of the commands that are being tested are moved to one list, git_sim_commands:

git_sim_commands = [
    # Simple commands.
    "git-sim add",
    "git-sim log",
    ...

    # Complex commands.
    "git-sim branch new_branch",
    "git-sim checkout branch2",
    ...
]

Then test_command() is called, with each of these passed as an argument called raw_cmd:

@pytest.mark.parametrize("raw_cmd", git_sim_commands)
def test_command(tmp_repo, raw_cmd):
    """Test a  git-sim command.

    This function works for any command of the forms
      `git-sim <command`
      `git-sim <command> <arg>`
    """
   ...

Pros:

  • It's really easy to see at a glance which commands are being tested. See here to see what the merged file would look like.
  • It's much easier to add a test for any commands that follow the supported format.

Cons:

  • It's a slightly more complex use of pytest, so it requires a bit more understanding to maintain the test suite.
  • It's impossible at the moment to run a test for a single command, ie you can no longer do $ pytest -k "test_log". That functionality can be built back in, I believe.

When a test fails, you still get really clear information about what specific behavior is broken:

============ short test summary info ==========================================================
FAILED tests/e2e_tests/test_core_commands.py::test_complex_command[git-sim cherry-pick branch2]
- AssertionError: assert 'git-sim-cherry-pick' in
  '/private/var/folders/gk/y2n2jsfj23g864pdr38rv4ch0000gn/T/pytest...
============ 1 failed, 4 passed, 18 deselected in 28.28s ======================================

Overall, I think this is a change worth making. But that's entirely up to you, and I'd be quite happy to keep helping out with or without this change.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/initialcommit-com/git-sim/pull/100 **Author:** [@ehmatthes](https://github.com/ehmatthes) **Created:** 7/17/2023 **Status:** ✅ Merged **Merged:** 7/17/2023 **Merged by:** [@initialcommit-io](https://github.com/initialcommit-io) **Base:** `dev` ← **Head:** `simplify_tests` --- ### 📝 Commits (5) - [`6db0a15`](https://github.com/initialcommit-com/git-sim/commit/6db0a1576a0c263fd90a9e43e30016f6d9e0a7ca) Parametrization works for two simple commands. - [`6c8e426`](https://github.com/initialcommit-com/git-sim/commit/6c8e426fd72a4901a21a0bc1831442eef0d2245b) All simple commands are parametrized. - [`98c5f79`](https://github.com/initialcommit-com/git-sim/commit/98c5f79de2c3d9865a94dcd72001d2178cdde582) Removed redundant tests. - [`d9a1e7f`](https://github.com/initialcommit-com/git-sim/commit/d9a1e7f08e5a2148fcdf8f9a85b510368149bc08) All complex commands are parametrized. - [`650fffc`](https://github.com/initialcommit-com/git-sim/commit/650fffc31b79af86276f1731913ae9887d16032e) All commands are tested by one function. ### 📊 Changes **1 file changed** (+40 additions, -253 deletions) <details> <summary>View changed files</summary> 📝 `tests/e2e_tests/test_core_commands.py` (+40 -253) </details> ### 📄 Description Working on #99, I played around with parametrization. I didn't think it was going to help, but it is pretty compelling. Instead of 18 separate test functions, there's only one test function. All of the commands that are being tested are moved to one list, `git_sim_commands`: ```python git_sim_commands = [ # Simple commands. "git-sim add", "git-sim log", ... # Complex commands. "git-sim branch new_branch", "git-sim checkout branch2", ... ] ``` Then `test_command()` is called, with each of these passed as an argument called `raw_cmd`: ```python @pytest.mark.parametrize("raw_cmd", git_sim_commands) def test_command(tmp_repo, raw_cmd): """Test a git-sim command. This function works for any command of the forms `git-sim <command` `git-sim <command> <arg>` """ ... ``` Pros: - It's really easy to see at a glance which commands are being tested. See [here](https://github.com/ehmatthes/git-sim/blob/simplify_tests/tests/e2e_tests/test_core_commands.py) to see what the merged file would look like. - It's *much* easier to add a test for any commands that follow the supported format. Cons: - It's a slightly more complex use of pytest, so it requires a bit more understanding to maintain the test suite. - It's impossible at the moment to run a test for a single command, ie you can no longer do `$ pytest -k "test_log"`. That functionality can be built back in, I believe. When a test fails, you still get really clear information about what specific behavior is broken: ```sh ============ short test summary info ========================================================== FAILED tests/e2e_tests/test_core_commands.py::test_complex_command[git-sim cherry-pick branch2] - AssertionError: assert 'git-sim-cherry-pick' in '/private/var/folders/gk/y2n2jsfj23g864pdr38rv4ch0000gn/T/pytest... ============ 1 failed, 4 passed, 18 deselected in 28.28s ====================================== ``` Overall, I think this is a change worth making. But that's entirely up to you, and I'd be quite happy to keep helping out with or without this change. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-02 16:48:12 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/git-sim#99
No description provided.