Skip to main content

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

Usage

from djangomockingbird import mock_model

@mock_model('myapp.myfile.MyModel')
def test_my_test():
    some_test_query = MyModel.objects.filter(bar='bar').filter.(foo='foo').first()
    #some more code
    #assertions here

With the mock_model decorator this test will pass no matter how complicated the query and will never touch the database.

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_model('myapp.myfile.MyModel', specs={'field': '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_model('myapp.myfile.MyModel', 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.

Release files for djangomockingbird 1.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for djangomockingbird 1.0.9
File Size Uploaded
djangomockingbird-1.0.9.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for djangomockingbird 1.0.9
File Interpreter ABI Platform
djangomockingbird-1.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 17.2 kB

Release files / djangomockingbird-1.0.9.tar.gz

Download URL djangomockingbird-1.0.9.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1b7d9fe252ffa8c79a328a5479df737e090791fec130e052fe3cbdb8a61821b6
BLAKE2b-256 checksum
How to use checksums
cbd92f35be335ec68d334df47496e6485b33b150da6bde991338e153d072b3ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.9.12 Darwin/21.3.0

Release files / djangomockingbird-1.0.9-py3-none-any.whl

Download URL djangomockingbird-1.0.9-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
10fea1ea49da860b94071756a50a7a1da57caaba34384aa1819f20c9ade0e44e
BLAKE2b-256 checksum
How to use checksums
c64cff5144d26b91c2abd584cdff9c666f98c9069599637c7657f1a6aeed3392
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.9.12 Darwin/21.3.0

Release history Release notifications | RSS feed

This release

1.0.9 This release

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.4

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page