生成curl命令行报告的pytest插件

pytest-curl-report的Python项目详细描述


https://drone.io/bitbucket.org/pytest-dev/pytest-curl-report/status.png

要求

  • python 2.6或3.3及更高版本

功能

  • 如果测试用例包含http请求将失败,则提供附加的cURL报告
  • 支持urllib/urllib2和requests作为http请求对象
  • 支持选项下方的curl命令行
    • 方法
    • 代理
    • 标题
    • 表单数据
    • 附件文件

安装

$ 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)

  • 首次发布

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
eclipse java btc交易应用程序编译时错误(HTTP组件)   java客户端状态保存,我能看到发送到客户端的状态数据吗?   spring中的java内部字段注入工作以及为什么不建议使用它   在iReports、Jasper和JavaBean数据源中使用表功能   如何在Java树中查找节点   用于日志分析的java模式匹配   java如何在Spring中处理json列表?   java SQL注入和可能的攻击   java如果我将实体转换为DTO,那么转换代码应该存在于何处?   java素数方法不起作用   通过NFC启动java Android Q隐私更改活动   java在Spring引导时为Spring AMQP和RabbitMQ动态设置主机   swing Java Paste From Clipboard不适用于Linux上的所有应用程序   java从不同对象访问对象的内部类   java Ektorp CouchDB测试连接   十进制前8位、十进制后2位和整个字符串的java正则表达式不应计算为零   在Java中以长格式存储两个整数之和   ConfigurationProperties中嵌套属性的java验证