久久久久久久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(檢查多個模擬的調用順序)
主站蜘蛛池模板: 国产精品久久久久久影院8一贰佰 | 欧美日韩精品一区 | 国产精品爱久久久久久久 | 天天天操| 亚州精品天堂中文字幕 | h视频在线播放 | 国产精品视频久久久久久 | 久久久久久国产精品三区 | 成人a视频片观看免费 | 欧美日韩亚洲视频 | 亚洲精品乱码久久久久久按摩 | 中文字幕高清在线 | 欧美乱码精品一区二区三区 | 亚洲一区精品在线 | 亚洲日日操| 日本精品一区二区 | 国产精品96久久久久久 | 欧美一二三 | 九九久久国产 | 中文字幕在线免费观看 | av一级久久 | 91 久久| av黄色片| 色免费视频| 国产一级电影在线 | 欧美成人精品欧美一级 | 亚洲视频在线观看一区二区三区 | 国产精品国产三级国产aⅴ中文 | 日韩一区精品 | 日韩欧美一级片 | 国产日韩欧美二区 | 最新日韩精品 | 日韩欧美三级电影 | 国产999精品久久久 日本视频一区二区三区 | 在线观看av网站永久 | 国产日产久久高清欧美一区 | 91在线视频在线观看 | 欧美综合在线观看 | 久久久久久高潮国产精品视 | 在线免费观看毛片 | 亚洲精品二区 |