admin 管理员组

文章数量: 1086019

I want to write unit-tests for a Typer based CLI, that will run the CLI with different inputs and validate correct argument parsing and input validation. The point isn't to test Typer itself, but rather to test my code's integration with Typer.

In addition, my CLI has some logic which I would like to mock during these tests (for example, HTTP API calls). Obviously, I want to make sure that tests that check input validation don't run real API calls.

Here is a simplified version of how my code might look like, where DataFetcher is some class defined elsewhere in the project:

import typer

app = typer.Typer()

@appmand()
def fetch_data(user_id: str):
    with DataFetcher() as data_fetcher:
        result = data_fetcher.fetch_data(user_id)
        typer.echo(result)

if __name__ == "__main__":
    app()

In this case, are there ways to explicitly mock DataFetcher (such as with the built-in mock library?) Ideally, I'd want to use explicit mocking rather than patching/monkeypatching.

本文标签: pythonUsing explicit mocks for Typer CLI testsStack Overflow