跳转到主要内容

将Starlette的TestClient从`requests`替换为`httpx`! 🎉

项目描述

bump-testclient

Package version

Starlette 0.21.0改变了TestClient实现:它用httpx替换了requests库。由于这些库具有不同的API,此更改破坏了Starlette用户的测试。

代码修改使过渡到Starlette 0.21.0更容易。它使测试再次工作所需的所有更改。

为什么?

让你的生活更轻松。由Sebastián Ramírez开玩笑地建议,但...我做到了。

转换

  1. client.<method>(...)替换为client.request("<method>", ...)

方法("delete","get","head","options")不接受contentdatajsonfiles参数。

此转换的条件

  • 使用client.<method>和` in ("delete","get","head","options")。
  • 使用contentdatajsonfiles参数。
  1. client.<method>(..., allow_redirects=...)替换为client.<method>(..., follow_redirects=...)

HTTPX使用follow_redirects而不是allow_redirects

  1. client.<method>(..., data=...)替换为client.<method>(..., content=...)

如果传递给data的参数是文本或字节,则应使用content

此情况发生的条件

  • data参数接收字节/文本参数。

安装

pip install bump-testclient

用法

在您要格式化的存储库中运行以下命令

python -m bump_testclient <files>

您还可以使用pre-commit。将以下内容添加到您的.pre-commit-config.yaml文件中

  - repo: https://github.com/Kludex/bump-testclient
    rev: 0.3.0
    hooks:
      - id: bump_testclient

许可证

本项目遵循MIT许可协议。

由以下支持