Skip to main content

The fastest way to write the fastest Django unit tests

Project description

Django Mockingbird: the fastest way to write the fastest Django unit tests

GitHub PyPI

1. What is Django Mockingbird and why would I need it?

Until now, there were two options for writing tests for a Django application: either create objects in the database for every test or mock the database queries using Unit test’s Mock. While the former is slow, the latter is complicated to write and read. Both add a lot of setup code to our tests. Django Mockingbird introduces a new way to write tests for Django, which is fast to run as well as simple to write.

2. How does it work?

It works by creating a mock object which behaves exactly like the Django model, but does not execute any queries under the hood. It only takes one line of code to use it in your test. It is not meant to be used in place of frameworks like Pytest, but to complement them.

3. How do I use it?

Installation

pip install djangomockingbird

Constructions the mocks

from models import Model
from djangomockingbird import make_mocks

my_mock = make_mocks(Model)

my_mock is now an object that mimics Model's behaviour exactly.

Using the mocks

The most straightforward way is to monkeypatch the object directly using its absolute path. In this example function_to_test is a function that can containt any amount of queries involving Model. With Django Mockingbird only the code below is necessary for this kind of test to pass, no matter how complicated the queries.

from djangomockingbird import make_mocks
import myapp
import function_to_test

def test_my_test_case():

    myapp.myfile.Model = make_mocks(Model)
    result = function_to_test()
    #assertions here

A good way to use Django mockingbird is also with Pytest's monkeypatch.

import pytest
from djangomockingbird import make_mocks
import myapp
import function_to_test

def test_my_test_case(monkeypatch):
    mock_model = make_mocks(Model)
    monkeypatch.setattr('myapp.myfile.Model', mock_model)
    result = function_to_test()
    #assertions here

Specifiying mock return data

You can specify the values of specific fields of the model object you are mocking. If you don’t empty strings will be returned. Construct a dictionary with field names as keys and desired returs as values and pass it to the 'specs' argument of make_mocks. If you try to specify a nonexisant field as a key an error will be thrown, but you can specify any kind of value you want.

mock = make_mocks(Model, specs={'model_field':'desired_value'})

Attention! Cases where returns must be specified: Model methods

If your model has custom methods and they are used by the test, you must specify their names and return data to the mock, otherwise your tests won't pass.

 model_method_specs = {'to_dict': {'': ''}}
 mock = make_mocks(Model, model_method_specs=model_method_specs)
 

4. Is it production ready? Can I help make it better?

This is still a very new project, but is quite stable for the general use case. However, there are advanced use cases that are not yet supported, most notably custom model managers. For those test cases you can try supplementing Django Mockingbird with your own code or other libraries. Because this tool is really just one elaborate mock model it is very flexible and plays well with pretty much anything.

We would appreciate you opening issues to bring any defects or oversights to light. Contributions are also kindly accepted - see more on the code arhitecture principles below if you are interested.

5. Where can I read more details on the architecture?

Read about the how functional programming principles were used in the library here and on metaprogramming features here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djangomockingbird-1.0.5.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

djangomockingbird-1.0.5-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file djangomockingbird-1.0.5.tar.gz.

File metadata

  • Download URL: djangomockingbird-1.0.5.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.4 Darwin/20.6.0

File hashes

Hashes for djangomockingbird-1.0.5.tar.gz
Algorithm Hash digest
SHA256 608f5f1f6e7faee20addbbe4ec14db201f9d981fc33e67211da95242ec1899a7
MD5 52f7e760af28e931065b4017a93d67ff
BLAKE2b-256 b3efc9e657db52d6330e3f5bdf9a3e37a3141dbca311326dc77218a47b4379bc

See more details on using hashes here.

File details

Details for the file djangomockingbird-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: djangomockingbird-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.4 Darwin/20.6.0

File hashes

Hashes for djangomockingbird-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b10e54105dc68cee3982097a1cb572a482efe0126e02822d758104251dbe1719
MD5 9837783e71d132b7770eb713b7511a07
BLAKE2b-256 50928ec1d5a675a192d7825ceb9a30876697ec0aa51d78ffe722cd861dc10644

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page