最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Using explicit mocks for Typer CLI tests - Stack Overflow

programmeradmin11浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论