问题
I am using mock
module for Python 2.7 to mock my other functions and using
unittest
for writing unit tests.
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?
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.
So appreciate if you could provide an example or tutorial on this.
Code to Test
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)
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?
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'}
}
回答1:
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
回答2:
You can certainly mock PyMongo, but I recommend mocking the MongoDB server itself. I've written a pure-Python emulator for MongoDB that can control fully, which responds to MongoDB Wire Protocol messages however you choose:
http://mockupdb.readthedocs.io/tutorial.html
Here's an example of using MockupDB with a Python application:
https://emptysqua.re/blog/test-mongodb-failures-mockupdb/
It requires intimate knowledge of the MongoDB wire protocol, but that's a useful skill to acquire anyway.
回答3:
You can also do this if you're just doing something simple, and you don't really need to retrieve by field.
@mock.patch("pymongo.collection.Collection.find")
def test_name(self, mock_find):
mock_find.return_value = {'name' : 'Kelly', 'email' : 'kelly@gmail.com'}
# rest of test
来源:https://stackoverflow.com/questions/42239241/how-to-mock-mongodb-for-python-unittests