typer icon indicating copy to clipboard operation
typer copied to clipboard

Testing: runner.invoke reuses 'app' object, causing cached properties to exist when they're not supposed to

Open Dymstro opened this issue 2 years ago • 0 comments

First Check

  • [X] I added a very descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the Typer documentation, with the integrated search.
  • [X] I already searched in Google "How to X in Typer" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to Typer but to Click.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

from typer.testing import CliRunner
from .main import app

runner = CliRunner()

def test_list_databases() -> None:
    # This command causes an internal 'databases' object to be cached.

    result = runner.invoke(app, ["databases", "list"]
    assert result.exit_code == 0


def test_create_database() -> None:
    # This command accesses some cached properties (using cached-property package).
    # Those objects aren't supposed to exist yet, but they were already created in previous tests.
    # This causes the command to use old data and crash.

    result = runner.invoke(app, ["databases", "create", "tests-db", "MariaDB"])
    assert result.exit_code == 0

Description

CliRunner.invoke reuses 'app' causing different behavior than just running them outside tests.

My code uses some cached properties that get cached in earlier tests. This causes some commands to use old data, which causes it to crash. Normally this would never happen as the program exits after a single command.

Operating System

Linux

Operating System Details

No response

Typer Version

0.4.0

Python Version

3.7

Additional Context

No response

Dymstro avatar Mar 01 '22 12:03 Dymstro