Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

225
Visualizações
How to mock mongodb for python unittests?

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'}
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório

0

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
over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda