久久久久久久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(檢查多個模擬的調用順序)
主站蜘蛛池模板: 亚洲欧美一区二区三区视频 | 日本不卡免费新一二三区 | 欧美11一13sex性hd | 亚洲国产看片 | 久在线视频播放免费视频 | 91欧美精品| 天天干狠狠 | 黑人成人网 | 日本天堂视频 | 精品久久久久久久久久 | 成人免费视频在线观看 | 91视视频在线观看入口直接观看 | 亚洲一区二区久久久 | 91视在线国内在线播放酒店 | 人人干人人看 | 国产二区在线播放 | 国产精品99久久久久久动医院 | 精品欧美一区免费观看α√ | 欧美video | 亚洲免费在线 | 国产一区二区视频免费在线观看 | 艹逼网 | 午夜精品导航 | 亚洲欧美一区二区三区国产精品 | 天天拍天天操 | 日韩一二三区视频 | 午夜电影在线播放 | 亚洲一区二区三区高清 | 91亚洲欧美 | 国产精品久久久久久久久久免费看 | 国产偷录视频叫床高潮对白 | 日韩中文在线 | 91久操视频 | 国产日韩亚洲欧美 | 美女高潮网站 | pacopacomama在线 | 久久久久国产精品午夜一区 | 草草草影院 | 一区二区欧美在线 | 国产一区视频在线 | 国产精品久久久久久久久久免费看 |