Package: pytest-mongodb
What is this?This is apytestplugin, that enables you to test your code that relies on a database connection to
a MongoDB and expects certain data to be present.  It allows you to specify fixtures for database
collections in JSON/BSON or YAML format. Under the hood we use themongomocklibrary, that you
should consult for documentation on how to use MongoDB mock objects. If suitable you can also use a
real MongoDB server.Note: This project has been renamed fromhumongoustopytest-mongodbin order to conform
to the pytest plugin naming convention and to be easier to find on the Python package index. See themigration sectionfor more information.ConfigurationIf you don’t want to put your database fixtures on the top-level directory of your package you have
to specify a directory wherepytest-mongodblooks for your data definitions.To do so put a line like the following under thepytestsection of yourpytest.ini-file put
a[pytest]mongodb_fixture_dir=tests/unit/fixturespytest-mongodbwould then look for files ending in.yamlor.jsonin that directory.If you want only a subset of the available fixtures to be loaded, you can use themongodb_fixturesconfig option. It takes a list of collection file-names without the file-extension. E.g.:[pytest]mongodb_fixtures=playerschampionshipsIn this case only the collections “players” and “championships” will be loaded.You can also choose to use a real MongoDB server for your tests. In that case you might also want to
configure the hostname and/or the credentials if you don’t want to stick with the default (localhost
and no credentials). Use the following configuration values in yourpytest.inito adapt the
settings to your needs:[pytest]mongodb_engine=pymongomongodb_host=mongodb://user:passwd@server.tldmongodb_dbname=mydbnameFor Mac users, who installed mongodb using homebrew, you can configure the executable to be picked up from/usr/local/bin/mongodinstead of/usr/local/bin/mongodby usingmongo_exec = /usr/local/bin/mongodin thepytest.inifile.Basic usageAfter you configuredpytest-mongodbso that it can find your fixtures you’re ready to specify
some data. Regardless of the markup language you choose, the data is provided as a list of documents
(dicts). The collection that these documents are being inserted into is given by the filename of
your fixture-file. E.g.: If you had a file namedplayers.yamlwith the following content:-name:Mariosurname:Götzeposition:striker-name:Manuelsurname:Neuerposition:keeperyou’d end up with a collectionplayersthat has the above player definitions inserted. If your
fixture file is in JSON/BSON format you can also use BSON specific types like$oid,$date,
etc.You get ahold of the database in your test-function by using themongodbfixture like so:deftest_players(mongodb):assert'players'inmongodb.list_collection_names()manuel=mongodb.players.find_one({'name':'Manuel'})assertmanuel['surname']=='Neuer'For further information refer to themongomockdocumentation.If you want to skip specific tests if the engine is ie. a mongomock engine you could do that like
so:frompytest_mongodb.pluginimportmongo_enginefrompytestimportmark@mark.skipif(mongo_engine()=='mongomock',reason="mongomock does not support that")deftest_players(mongodb):assert'players'inmongodb.list_collection_names()manuel=mongodb.players.find_one({'name':'Manuel'})assertmanuel['surname']=='Neuer'Migration from humongousIn the course of migrating the package name fromhumongoustopytest-mongodbmost
configuration values which previously were prefixed withhumongous_have been renamed to amongodb_-prefixed counterpart. The only notable exception is thehumongous_basedirconfig
value, which now is namedmongodb_fixture_dir.  Additionally the commandline options have been
unified, in a way that multi-word option names are now consistently separated with dashes instead of
underscores.