生成curl命令行报告的pytest插件
pytest-curl-report的Python项目详细描述
要求
- 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命令行来复制http 请求Web服务器。复制curl命令并粘贴到终端上, 然后您可以立即运行该命令。告诉他们 开发人员如何在测试失败时重现问题。
curl报告将从测试代码中的request对象生成, 所以您不必配置某些设置或使用特定片段。
使用量
有几种选择。
$ 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命令,curl report only非常有用。 例如,您更喜欢test-first概念,并使用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)
- 修复多部分/窗体数据为二进制时的错误
0.5.2(2015-05-19)
- 修复一个小错误
0.5.1(2015-05-19)
- 在仅提供-curl报告时修复问题
0.5.0(2015-05-17)
- 首次发布