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

對使用 requests 庫的 python 應(yīng)用程序進行單元測試

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

問題描述

我正在編寫一個使用 Kenneth Reitz 的 requests library 執(zhí)行 REST 操作的應(yīng)用程序,我正在努力尋找對這些應(yīng)用程序進行單元測試的好方法,因為 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.

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

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

推薦答案

實際上有點奇怪的是,這個庫有一個關(guān)于最終用戶單元測試的空白頁面,同時目標(biāo)是用戶友好性和易用性.然而,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)

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

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

相關(guān)文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測試 Python 代碼時,我應(yīng)該如何驗證日志消息?)
Patch __call__ of a function(修補函數(shù)的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對象的模擬方法中調(diào)用 self?)
Mocking only a single method on an object(僅模擬對象上的單個方法)
Mocking a subprocess call in Python(在 Python 中模擬子進程調(diào)用)
Checking call order across multiple mocks(檢查多個模擬的調(diào)用順序)
主站蜘蛛池模板: 久草超碰| 欧美在线综合 | 欧美一区二区三区不卡 | 国产精品久久久久久久久久久久久久 | 欧美在线不卡 | 天天天天操 | 国产精品一区二区三区不卡 | 午夜不卡视频 | 三级a毛片 | 国产精品一区二区三区免费 | 日韩精品在线观看视频 | 亚洲一区二区三区在线视频 | 91黄色大片| 五月开心网 | 国内精品一区二区三区 | av一级在线| 亚洲毛片av | 狠狠的操 | 久久国产精品一区二区 | 日本加勒比在线 | 午夜激情福利视频 | 国产精品一二三 | 国产成人在线观看免费网站 | 亚洲黄色网址 | 日韩精品福利 | 国产精品自拍一区 | 三级在线观看 | 亚洲精品久 | 成年免费视频黄网站在线观看 | 久久久久久艹 | 国产永久免费视频 | 夜夜狠狠擅视频 | 欧美aaaaa | 超碰av在线播放 | 亚洲黄色成人 | 欧美性猛交一区二区三区精品 | 日韩精品影视 | 亚洲精品国产精品国自产观看浪潮 | 国产日韩欧美在线 | 精品一区视频 | 在线观看国产小视频 |