All Projects → mdomke → pytest-mongodb

mdomke / pytest-mongodb

Licence: other
pytest plugin for mocking MongoDB with fixtures

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to pytest-mongodb

Pytest Responsemock
Simplified requests calls mocking for pytest
Stars: ✭ 24 (-57.14%)
Mutual labels:  mocking, pytest
pytest-tap
Test Anything Protocol (TAP) reporting plugin for pytest
Stars: ✭ 34 (-39.29%)
Mutual labels:  pytest
bokor
Bokor is a simple, Record and Playback Mock Server written in Node.js, utilized for Service Virtualization.
Stars: ✭ 24 (-57.14%)
Mutual labels:  mocking
chrome-extension-mocker
The most convenient tool to mock requests for axios, with built-in Chrome extension support.
Stars: ✭ 37 (-33.93%)
Mutual labels:  mocking
Python-Studies
All studies about python
Stars: ✭ 56 (+0%)
Mutual labels:  pytest
pytest-pycodestyle
pytest plugin to run pycodestyle
Stars: ✭ 15 (-73.21%)
Mutual labels:  pytest
pytest-datafiles
pytest plugin to create a tmpdir containing a preconfigured set of files and/or directories.
Stars: ✭ 75 (+33.93%)
Mutual labels:  pytest
publishing-python-packages
Examples and exercises for Publishing Python Packages from Manning Books 🐍 📦 ⬆️
Stars: ✭ 25 (-55.36%)
Mutual labels:  pytest
python-appium-framework
Complete Python Appium framework in 360 degree
Stars: ✭ 43 (-23.21%)
Mutual labels:  pytest
pytest-dependency
Manage dependencies of tests
Stars: ✭ 113 (+101.79%)
Mutual labels:  pytest
wiremock
A tool for mocking HTTP services
Stars: ✭ 5,239 (+9255.36%)
Mutual labels:  mocking
PyTest
pytest runner and view annotator for sublime text 3
Stars: ✭ 20 (-64.29%)
Mutual labels:  pytest
MockAlamofire
A simple example showing how to override the URLProtocol to return mock data on Alamofire responses. Helpful if you are looking for a simple way to mock an Alamofire response, with out any additional dependencies.
Stars: ✭ 22 (-60.71%)
Mutual labels:  mocking
continuous-integration-with-python
How to test your python code. How to automatically run your tests for your Python code. How to get reports of the tests coverage
Stars: ✭ 25 (-55.36%)
Mutual labels:  pytest
pytest-subprocess
Pytest plugin to fake subprocess.
Stars: ✭ 83 (+48.21%)
Mutual labels:  pytest
pygments-pytest
A pygments lexer for pytest output
Stars: ✭ 23 (-58.93%)
Mutual labels:  pytest
pytest-involve
A pytest plugin to run tests pertaining to a specific file or changeset
Stars: ✭ 28 (-50%)
Mutual labels:  pytest
salt-toaster
Salt Toaster: An ultimate test suite for Salt
Stars: ✭ 24 (-57.14%)
Mutual labels:  pytest
ipython pytest
Pytest magic for IPython notebooks
Stars: ✭ 33 (-41.07%)
Mutual labels:  pytest
drf-chat-server-example
A chat server example used Django REST framework with pytest
Stars: ✭ 15 (-73.21%)
Mutual labels:  pytest
https://travis-ci.org/mdomke/pytest-mongodb.svg?branch=master

What is this?

This is a pytest plugin, 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 the mongomock library, 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 from humongous to pytest-mongodb in order to conform to the pytest plugin naming convention and to be easier to find on the Python package index. See the migration section for more information.

Configuration

If you don't want to put your database fixtures on the top-level directory of your package you have to specify a directory where pytest-mongodb looks for your data definitions.

To do so put a line like the following under the pytest section of your pytest.ini-file put a

[pytest]
mongodb_fixture_dir =
  tests/unit/fixtures

pytest-mongodb would then look for files ending in .yaml or .json in that directory.

If you want only a subset of the available fixtures to be loaded, you can use the mongodb_fixtures config option. It takes a list of collection file-names without the file-extension. E.g.:

[pytest]
mongodb_fixtures =
  players
  championships

In 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 your pytest.ini to adapt the settings to your needs:

[pytest]
mongodb_engine = pymongo
mongodb_host = mongodb://user:[email protected]
mongodb_dbname = mydbname

Basic usage

After you configured pytest-mongodb so 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 named players.yaml with the following content:

-
  name: Mario
  surname: Götze
  position: striker

-
  name: Manuel
  surname: Neuer
  position: keeper

you'd end up with a collection players that 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 the mongodb fixture like so:

def test_players(mongodb):
    assert 'players' in mongodb.list_collection_names()
    manuel = mongodb.players.find_one({'name': 'Manuel'})
    assert manuel['surname'] == 'Neuer'

For further information refer to the mongomock documentation.

If you want to skip specific tests if the engine is ie. a mongomock engine you could do that like so:

from pytest_mongodb.plugin import mongo_engine
from pytest import mark

@mark.skipif(mongo_engine() == 'mongomock', reason="mongomock does not support that")
def test_players(mongodb):
    assert 'players' in mongodb.list_collection_names()
    manuel = mongodb.players.find_one({'name': 'Manuel'})
    assert manuel['surname'] == 'Neuer'

Migration from humongous

In the course of migrating the package name from humongous to pytest-mongodb most configuration values which previously were prefixed with humongous_ have been renamed to a mongodb_-prefixed counterpart. The only notable exception is the humongous_basedir config value, which now is named mongodb_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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].