Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
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.
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.
pip install djangomockingbird
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.
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'})
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)
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.
Read about the how functional programming principles were used in the library here and on metaprogramming features here.
FAQs
The fastest way to write the fastest Django unit tests
We found that djangomockingbird demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.