将Starlette的TestClient从`requests`替换为`httpx`! 🎉
项目描述
bump-testclient
Starlette 0.21.0改变了TestClient
实现:它用httpx
替换了requests
库。由于这些库具有不同的API,此更改破坏了Starlette用户的测试。
此代码修改使过渡到Starlette 0.21.0更容易。它使测试再次工作所需的所有更改。
为什么?
让你的生活更轻松。由Sebastián Ramírez开玩笑地建议,但...我做到了。
转换
- 将
client.<method>(...)
替换为client.request("<method>", ...)
方法("delete","get","head","options")不接受content
,data
,json
和files
参数。
此转换的条件
- 使用
client.<method>
和` in ("delete","get","head","options")。 - 使用
content
,data
,json
或files
参数。
- 将
client.<method>(..., allow_redirects=...)
替换为client.<method>(..., follow_redirects=...)
HTTPX使用follow_redirects
而不是allow_redirects
。
- 将
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许可协议。
关闭
bump_testclient-0.3.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 55139fe9aed45702a0215701ef6d37ad7dd449813bd95e65b58cd7430637964f |
|
MD5 | 94ba948011f94c22e6a18099ae05ead3 |
|
BLAKE2b-256 | 3028b2f62ebcd2fb35892e788c497b2d4d0629d7c83559e8f3855acafaa626f1 |