skillfed

django-mock-queries

A django library for mocking queryset functions in memory for testing

django-mock-queries v2.3.0 204.8K downloads/30d#9,601 on PyPI324
Permissive license MIT Active released

What it is and what it does

django-mock-queries lets you test Django code that queries models without setting up a database. It provides in-memory MockSet and MockModel classes that mimic Django's QuerySet API, supporting method chaining, filtering with Q objects, aggregates, field lookups, and CRUD operations. This is useful when you want fast unit tests that don't require database setup or migrations.

The library is typically used by patching Django model managers in test cases, allowing you to verify query logic in isolation. It also includes utilities for testing django-rest-framework serializers and can be configured to run an entire test suite without a database by monkey-patching the test database backend, which can significantly speed up test execution for projects with many migrations.

Use it for:

  • Unit test Django view logic that queries models without setting up a test database
  • Mock User.objects or other model managers in test patches to verify filter and query logic
  • Test django-rest-framework serializers with mocked model instances and nested serializers
  • Speed up test suites by running tests without database setup when database-free testing is feasible
  • Verify QuerySet method chaining and complex filter combinations with Q objects in isolation

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Mocks Django QuerySet operations in memory for unit testing, supporting filtering, aggregation, field lookups, and CRUD operations without a database.

Yes. This is a mature, actively maintained testing utility with no dependencies, permissive licensing, and clear use cases for accelerating Django unit tests. Install it if you want to test query logic without database overhead; skip it only if your test suite already has fast database fixtures or you need QuerySet methods not yet supported (annotate, reverse, none, extra, raw, bulk_create, in_bulk, as_manager).

Install

django-mock-queries on PyPI

pip

pip install django-mock-queries

uv

uv add django-mock-queries

poetry

poetry add django-mock-queries

Installing django-mock-queries

Before you install

Low friction install with no runtime dependencies. Actively maintained with recent commits and stable production status since 2016.

License in practice

MIT license permits free use, modification, and distribution with minimal restrictions—suitable for commercial and open-source projects.

Quickstart

pip install django-mock-queries

from django_mock_queries.query import MockSet, MockModel

qs = MockSet(
    MockModel(mock_name='john', email='john@gmail.com'),
    MockModel(mock_name='bill', email='bill@gmail.com'),
)
result = [x for x in qs.all().filter(email__icontains='gmail.com')]

Verify before relying

  • Whether all QuerySet methods used in production code are supported (TODO lists annotate, reverse, none, extra, raw, bulk_create, in_bulk, as_manager)
  • Performance characteristics when mocking large datasets in memory

Package facts

License MIT (permissive)
Python support not specified
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance actively maintained — 687 days since the last release
Last repo commit
First released
Downloads 204,780/month — #9,601 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: django_mock_queries-2.3.0-py2.py3-none-any.whl

Keywords: django, orm, mocking, unit-testing, tdd

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseProgramming Language :: PythonProgramming Language :: Python :: 3 :: OnlyTopic :: Software Development :: TestingTopic :: Software Development :: Testing :: MockingTopic :: Software Development :: Testing :: Unit

Tags

django queryset mockingin-memory django testingmock django ormdjango unit test fixturesdjango query testing without databasemock django modelsdjango test doubles
django-testingunit-testingmocking

More Testing packages

pluggy

Pluggy provides a plugin system that lets you…

permissive · top 100 on PyPI

pytest

pytest is a testing framework that lets you…

permissive · top 100 on PyPI

virtualenv

virtualenv creates isolated Python environments…

permissive · top 100 on PyPI

coverage

Coverage.py measures which lines of Python code…

permissive · top 1,000 on PyPI

pytest-asyncio

pytest-asyncio is a pytest plugin that enables…

permissive · top 1,000 on PyPI

pytest-json-ctrf

A pytest plugin that generates test reports in…

permissive · top 1,000 on PyPI

mock-alchemy

Provides mock helpers for SQLAlchemy sessions…

permissive · top 15,000 on PyPI

djangorestframework-bulk

Adds bulk create, update, and delete operations…

permissive · top 15,000 on PyPI

django-query-builder

Builds and executes complex SQL queries…

permissive · top 15,000 on PyPI

django-cacheops

Automatic and manual ORM query caching for…

permissive · top 5,000 on PyPI

django-zen-queries

Prevents unintended database queries in Django…

permissive · top 15,000 on PyPI

djangorestframework-filters

Extends Django REST Framework with advanced…

permissive · top 15,000 on PyPI

django-user-sessions

Replaces Django's opaque session backend with…

permissive · top 15,000 on PyPI

alchemy-mock

Provides mock helpers for SQLAlchemy sessions…

permissive · top 15,000 on PyPI

django-chunkator

Iterates over large Django QuerySets in…

permissive · top 15,000 on PyPI

django-sql-utils

Provides simplified APIs for Django querysets…

permissive · top 15,000 on PyPI