`retry_until()`: Improve pytest error message via rewriting

Open
#376 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
testing-qa

Research direction

Start with the retry_until() entry point shown in the traceback and run tests/test_workspacebuilder.py::test_automatic_rename_option to reproduce the current failure. Investigate pytest assertion rewriting and make the timeout failure retain the higher-level assertion context, ending with the reported WaitTimeout in check_window_name_match() wording.

Written by the indexing model from the issue text.

Description

#372 added retry_until()

pytest assertion rewriting provides pytest_assertrepr_compare().

Now

________________________________________________ test_automatic_rename_option ________________________________________________

session = Session($1 libtmux_yn08bma4)

    def test_automatic_rename_option(session):
        """With option automatic-rename: on."""
        yaml_config = test_utils.read_config_file(
            "workspacebuilder/window_automatic_rename.yaml"
        )
        s = session
        sconfig = kaptan.Kaptan(handler="yaml")                                                                                                                                                                                                     [56/238]        sconfig = sconfig.import_config(yaml_config).get()

        # This should be a command guaranteed to be terminal name across systems
        portable_command = sconfig["windows"][0]["panes"][0]["shell_command"][0]["cmd"]
        # If a command is like "man ls", get the command base name, "ls"
        if " " in portable_command:
            portable_command = portable_command.split(" ")[0]

        builder = WorkspaceBuilder(sconf=sconfig)

        window_count = len(session._windows)  # current window count
        assert len(s._windows) == window_count
        for w, wconf in builder.iter_create_windows(s):

            for p in builder.iter_create_panes(w, wconf):
                w.select_layout("tiled")  # fix glitch with pane size
                p = p
                assert len(s._windows), window_count
            assert isinstance(w, Window)
            assert w.show_window_option("automatic-rename") == "on"

            assert len(s._windows) == window_count

            window_count += 1
            w.select_layout(wconf["layout"])

        assert s.name != "tmuxp"
        w = s.windows[0]

        def check_window_name_mismatch() -> bool:
            session.server._update_windows()
            return w.name != portable_command

        assert retry_until(check_window_name_mismatch, 2, interval=0.25)

        pane_base_index = w.show_window_option("pane-base-index", g=True)
        w.select_pane(pane_base_index)

        def check_window_name_match() -> bool:
            session.server._update_windows()
            return w.name == portable_command

>       assert retry_until(check_window_name_match, 2, interval=0.25)
                                                                                                                                                                                                                                                            tests/test_workspacebuilder.py:402:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

fun = <function test_automatic_rename_option.<locals>.check_window_name_match at 0x7f484aee43a0>, seconds = 2

    def retry_until(
        fun: Callable,
        seconds: float = RETRY_TIMEOUT_SECONDS,
        *,
        interval: Optional[float] = RETRY_INTERVAL_SECONDS,
        raises: Optional[bool] = True,
    ) -> bool:                                                                                                                                                                                                                                       [0/238]        """
        Retry a function until a condition meets or the specified time passes.

        Parameters
        ----------
        fun : callable
            A function that will be called repeatedly until it returns ``True``  or
            the specified time passes.
        seconds : float
            Seconds to retry. Defaults to ``8``, which is configurable via
            ``RETRY_TIMEOUT_SECONDS`` environment variables.
        interval : float
            Time in seconds to wait between calls. Defaults to ``0.05`` and is
            configurable via ``RETRY_INTERVAL_SECONDS`` environment variable.
        raises : bool
            Wether or not to raise an exception on timeout. Defaults to ``True``.

        Examples
        --------

        >>> def f():
        ...     p = w.attached_pane
        ...     p.server._update_panes()
        ...     return p.current_path == pane_path
        ...
        ... retry(f)

        In pytest:

        >>> assert retry(f, raises=False)
        """
        ini = time.time()

        while not fun():
            end = time.time()
            if end - ini >= seconds:
                if raises:
>                   raise WaitTimeout()
E                   libtmux.exc.WaitTimeout

.venv/lib/python3.10/site-packages/libtmux/test.py:103: WaitTimeout
FAILED tests/test_workspacebuilder.py::test_automatic_rename_option - libtmux.exc.WaitTimeout

What we want

We want the higher level frame in the stack:

        def check_window_name_match() -> bool:
            session.server._update_windows()
            return w.name == portable_command

>       assert retry_until(check_window_name_match, 2, interval=0.25)

.venv/lib/python3.10/site-packages/libtmux/test.py:103: WaitTimeout

FAILED tests/test_workspacebuilder.py::test_automatic_rename_option - libtmux.exc.WaitTimeout in check_window_name_match()
Dominant language
Python
Stars
1.2k
Forks
127
Avg merge
2h 13m
Merged PRs (30d)
1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tmux-python/libtmux

All issues in tmux-python/libtmux

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.