Use mock MongoDB server for unit test

后端 未结 4 523
萌比男神i
萌比男神i 2020-12-09 17:22

I have to implement nosetests for Python code using a MongoDB store. Is there any python library which permits me initializing a mock in-memory MongoDB server?

I am

4条回答
  •  天涯浪人
    2020-12-09 17:58

    You can use Ming which has an in-memory mongo db pymongo connection replacement.

    import ming
    mg = ming.create_datastore('mim://')
    mg.conn # is the connection
    mg.db # is a db with no name
    mg.conn.somedb.somecol
    # >> mim.Collection(mim.Database(somedb), somecol)
    col = mg.conn.somedb.somecol
    col.insert({'a': 1})
    # >> ObjectId('5216ac3fe0323a1218f4e9aa')
    col.find().count()
    # >> 1
    

提交回复
热议问题