pytest插件,用于生成curl命令行报告
项目描述
要求
Python 2.6或3.3及更高版本
功能
安装
$ pip install pytest-curl-report
快速开始
创建一个请求通过http[s]访问Web服务器的测试文件,并打算使测试失败。
$ vi test.py # -*- coding: utf-8 -*- import requests def test_requests_get(): r = requests.get('http://httpbin.org/get') assert False
然后,pytest会显示如下报告。
$ py.test test.py ============================= test session starts ============================== platform darwin -- Python 2.7.9 -- py-1.4.27 -- pytest-2.6.4 plugins: curl-report, httpbin, cache, capturelog, cov, flakes, pep8 collected 1 items test.py F =================================== FAILURES =================================== ______________________________ test_requests_get _______________________________ def test_requests_get(): r = requests.get('http://httpbin.org/get') > assert False E assert False test.py:7: AssertionError -------------------------- How to reproduce with curl -------------------------- curl -X GET -H "Connection: keep-alive" -H "Accept-Encoding: gzip, deflate" -H "Accept: */*" -H "User-Agent: python-requests/2.7.0 CPython/2.7.9 Darwin/14.3.0" "http://httpbin.org/get"
pytest-curl-report插件生成cURL命令行以重现对Web服务器的http请求。复制cURL命令并在终端粘贴,然后您可以立即运行该命令。当测试失败时,这可能有助于开发者了解如何重现问题。
cURL报告将从测试代码中的请求对象生成,因此您无需配置某些设置或使用特定的片段。
用法
有几个选项。
$ py.test -h ... curl report: --no-curl-report not generate curl report when a testcase is failed --curl-report-only strip pytest assertion log and generate curl report only ...
–curl-report-only如果您只想确认cURL命令,则非常有用。例如,您更喜欢先测试概念,并使用cli进行交互式开发。
$ vi test.py ... def test_requests_post(): r = requests.post('https://httpbin.org/post', data={"test": "example"}) assert False $ py.test --curl-report-only test.py =================================== FAILURES =================================== ______________________________ test_requests_get _______________________________ -------------------------- How to reproduce with curl -------------------------- curl -X GET -H "Connection: keep-alive" -H "Accept-Encoding: gzip, deflate" -H "Accept: */*" -H "User-Agent: python-requests/2.7.0 CPython/2.7.9 Darwin/14.3.0" "http://httpbin.org/get" ______________________________ test_requests_post ______________________________ -------------------------- How to reproduce with curl -------------------------- curl -X POST -H "Content-Length: 12" -H "Accept-Encoding: gzip, deflate" -H "Accept: */*" -H "User-Agent: python-requests/2.7.0 CPython/2.7.9 Darwin/14.3.0" -H "Connection: keep-alive" -H "Content-Type: application/x-www-form-urlencoded" -d "test=example" "https://httpbin.org/post" =========================== 2 failed in 1.33 seconds ===========================
如上所述,您可能会认为一些头信息是多余的。在conftest.py中添加一些代码,然后限制所需的头信息。
$ vi conftest.py def pytest_namespace(): return {'curl_report': {'headers': ['Content-Type']}} $ py.test test.py ... ______________________________ test_requests_post ______________________________ -------------------------- How to reproduce with curl -------------------------- curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "test=example" "https://httpbin.org/post"
在这种情况下,仅生成Content-Type头信息。
代理设置
遗憾的是,似乎Request对象不保留代理设置。代理设置从平台上的环境变量中获取。因此,即使您以其他方式提供设置,也请通过插件添加环境变量以检测设置。
$ vi test.py def test_requests_proxy_post(): import os os.environ['HTTPS_PROXY'] = 'https://127.0.0.1:8888' r = requests.post('https://httpbin.org/post', data={"test": "example"}) assert False $ py.test test.py ... -------------------------- How to reproduce with curl -------------------------- curl -X POST -x https://127.0.0.1:8888 -H "Content-Type: application/x-www-form-urlencoded" -d "test=example" "https://httpbin.org/post"
变更日志
0.5.4 (2016-12-11)
修复一些失败的测试
0.5.3 (2016-12-04)
修复当multipart/form-data为二进制时的错误
0.5.2 (2015-05-19)
修复一个小的错别字
0.5.1 (2015-05-19)
修复当给出–curl-report-only时的一个问题
0.5.0 (2015-05-17)
首次发布
项目详情
关闭
pytest-curl-report-0.5.4.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ecedaaef39d141a8c2dc5f734a461fce1f751d59570b5c88e392576487201fbe |
|
MD5 | a27abb4ade1bf4598e52ff7f35f6c6cc |
|
BLAKE2b-256 | 4ce56aa391bda0ecbc6d63039edb41dcf50428060d337a792628cfef1dc75c54 |