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

如何為 python 單元測(cè)試模擬 mongodb?

How to mock mongodb for python unittests?(如何為 python 單元測(cè)試模擬 mongodb?)
本文介紹了如何為 python 單元測(cè)試模擬 mongodb?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在使用 Python 2.7 的 mock 模塊來(lái)模擬我的其他函數(shù)并使用

I am using mock module for Python 2.7 to mock my other functions and using

unittest 用于編寫單元測(cè)試.

我想知道模擬 MongoDB 是否與使用模擬功能不同(mock.patch 一個(gè)被調(diào)用的函數(shù)?)或者我需要為此目的使用另一個(gè)不同的包?

I am wondering if mocking the MongoDB is different than using mock functionality (mock.patch a function that is being called?) Or I need to use another different package for that purpose?

我認(rèn)為我不想運(yùn)行測(cè)試 mongodb 實(shí)例.我想要的只是一些速度數(shù)據(jù)并且能夠調(diào)用 pymongo 功能.我只是有點(diǎn)迷失在思考是否有一種方法可以為模塊(如 pymongo)編寫模擬,或者任何可以通過(guò) mock 模塊實(shí)現(xiàn)的方法.

I do not think I want to have a test mongodb instance running. All I want is some tempo data and being able to call pymongo functionality. I am just a bit lost in thinking of is there a way to write a mock for a module (like pymongo), or anything is achievable by mock module.

如果您能提供一個(gè)示例或教程,非常感謝.

So appreciate if you could provide an example or tutorial on this.

from pymongo import MongoClient

monog_url = 'mongodb://localhost:27017'
client = MongoClient(monog_url)
db = client.db

class Dao(object):
   def __init__(self):
      pass

   def save(self, user):
      db_doc = {
        'name': user.name,
        'email': user.email
      }
      db.users.save(db_doc)

   def getbyname(self, user):
      db_doc = {
        'name': user.name,
      }
      return db.users.find(db_doc)

為了測(cè)試這個(gè),我真的不希望測(cè)試 mongodb 啟動(dòng)并運(yùn)行!而且,我想我不想模擬 db.userssave 和 db.users.find 因?yàn)槲蚁M嬲軌驒z索我保存的數(shù)據(jù)并確保它在數(shù)據(jù)庫(kù)中.我認(rèn)為我需要為記憶中的每個(gè)模型創(chuàng)建一些 fixtures 并與它們一起使用.只是我需要一個(gè)外部工具來(lái)執(zhí)行此操作嗎?

To test this, I do not really want a test mongodb up and running! But also, I think I do not want to mock db.userssave and db.users.find because I want to actually be able to retrieve the data that I saved and make sure it is in the db. I think I need to create some fixtures per models that are in my memory and work with them. Just do I need an external tool to do so?

我正在考慮保留一些這樣的假數(shù)據(jù),只是不知道如何正確處理.

I am thinking of keeping some fake data like this, just do not know how to properly deal with it.

users = { 
    {'name' : 'Kelly', 'email' : 'kelly@gmail.com'},
    {'name': 'Sam', 'email': 'sam@gmail.com'}
}

推薦答案

我推薦使用 mongomock 來(lái)模擬 mongodb.它基本上是一個(gè)帶有 pymongo 接口的內(nèi)存中 mongodb,專門為此目的而制作.

I recommend using mongomock for mocking mongodb. It's basically an in-memory mongodb with pymongo interface and made specifically for this purpose.

https://github.com/mongomock/mongomock

這篇關(guān)于如何為 python 單元測(cè)試模擬 mongodb?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 一级做a爱片性色毛片 | www.国产精品 | 国产成人97精品免费看片 | 国产又粗又长又爽 | 一区在线视频 | 亚洲视频免费观看 | 亚洲精品一区二区在线观看 | 日韩在线免费观看视频 | 免费性网站 | 韩日一级片 | 国产精品美女久久久 | 亚洲成人毛片 | 国产一级一片免费播放放a 国产黄色大片 | 日韩欧美亚洲 | 欧美日韩成人在线观看 | 伊人网综合 | www.午夜| 亚洲香蕉在线 | 成人在线免费视频观看 | 日日操夜夜爽 | 国产在线视频网站 | 欧美在线观看一区 | 欧美区日韩区 | 日韩免费毛片 | 日韩精品久久久久 | 青青青在线视频 | 欧美精品乱码99久久蜜桃 | 玖玖色资源 | 美女福利视频 | 自拍偷在线精品自拍偷无码专区 | 色综合一区 | 一区二区小视频 | 狠狠操天天操 | 国产乱码一区二区三区 | 欧美日韩精品久久 | 日韩精品成人 | 亚洲一区二区三区四区在线 | 久久国内精品 | 亚洲精品欧美 | 日韩精品一级毛片在线播放 | 成人看片免费 |