久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

對使用 requests 庫的 python 應用程序進行單元測試

Unit testing a python app that uses the requests library(對使用 requests 庫的 python 應用程序進行單元測試)
本文介紹了對使用 requests 庫的 python 應用程序進行單元測試的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在編寫一個使用 Kenneth Reitz 的 requests library 執行 REST 操作的應用程序,我正在努力尋找對這些應用程序進行單元測試的好方法,因為 requests 通過模塊級方法提供其方法.

I am writing an application that performs REST operations using Kenneth Reitz's requests library and I'm struggling to find a nice way to unit test these applications, because requests provides its methods via module-level methods.

我想要的是合成雙方對話的能力;提供一系列請求斷言和響應.

What I want is the ability to synthesize the conversation between the two sides; provide a series of request assertions and responses.

推薦答案

實際上有點奇怪的是,這個庫有一個關于最終用戶單元測試的空白頁面,同時目標是用戶友好性和易用性.然而,Dropbox 有一個易于使用的庫,不出所料地稱為 responses.這是它的介紹帖.它說他們沒有使用 httpretty,同時聲明沒有失敗的原因,寫了一個類似API的庫.

It is in fact a little strange that the library has a blank page about end-user unit testing, while targeting user-friendliness and ease of use. There's however an easy-to-use library by Dropbox, unsurprisingly called responses. Here is its intro post. It says they've failed to employ httpretty, while stating no reason of the fail, and written a library with similar API.

import unittest

import requests
import responses


class TestCase(unittest.TestCase):

  @responses.activate  
  def testExample(self):
    responses.add(**{
      'method'         : responses.GET,
      'url'            : 'http://example.com/api/123',
      'body'           : '{"error": "reason"}',
      'status'         : 404,
      'content_type'   : 'application/json',
      'adding_headers' : {'X-Foo': 'Bar'}
    })

    response = requests.get('http://example.com/api/123')

    self.assertEqual({'error': 'reason'}, response.json())
    self.assertEqual(404, response.status_code)

這篇關于對使用 requests 庫的 python 應用程序進行單元測試的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測試 Python 代碼時,我應該如何驗證日志消息?)
Patch __call__ of a function(修補函數的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對象的模擬方法中調用 self?)
Mocking only a single method on an object(僅模擬對象上的單個方法)
Mocking a subprocess call in Python(在 Python 中模擬子進程調用)
Checking call order across multiple mocks(檢查多個模擬的調用順序)
主站蜘蛛池模板: 日韩国产欧美视频 | 国产精品99久久久久久久久久久久 | 成人网址在线观看 | 久久久久高清 | 欧美日韩国产一区二区三区 | 午夜久久久 | 久久久国产一区 | 久久久久国产一区二区 | 视频在线观看一区 | 久久久精品综合 | 99热首页 | 成人精品一区亚洲午夜久久久 | 黄色片在线观看网址 | 色视频成人在线观看免 | 一级欧美 | 欧美精品第一区 | 97影院在线午夜 | 一区二区三区久久 | 久久久久久久亚洲精品 | 国产精品国产三级国产aⅴ原创 | 欧美日一区 | 欧美日韩亚洲国产综合 | 免费精品| 中文字幕一区在线观看视频 | 日本一本视频 | 日本精品视频在线 | 国产精品国产精品国产专区不片 | 久久久123 | 久久久久久久久99精品 | 日韩视频在线播放 | 久久综合九色综合欧美狠狠 | 国产伦精品一区二区三区高清 | 午夜电影日韩 | 在线国产一区二区三区 | 在线观看视频91 | 精品欧美一区二区三区久久久 | 国产视频一区在线观看 | 欧美一级欧美一级在线播放 | 欧美乱码精品一区二区三区 | 日韩av成人在线 | 国产日韩电影 |