Run Django Tests

Django web applications can be run on almost any machine that can run the Python 3 programming language: Windows, macOS, Linux/Unix, Solaris, to name just a few. Almost any computer should have the necessary performance to run Django during development. In this article, we'll provide instructions for Windows, macOS, and Linux/Unix. To run only migrations tests, use -tag option: python mange.py test-tag = migrationtest # runs only migraion tests python mange.py test-exclude-tag = migrationtest # runs all except migraion tests Django Checks. Djangotestmigrations comes with 2 groups of Django's checks for: detecting migrations scripts automatically generated names. Since Django 1.6 you can run a complete test case, or single test, using the complete dot notation for the element you want to run. Automatic test discovery will now find tests in any file that starts with test under the working directory, so addressing the question you would have to rename your files, but you can now keep them inside the directory you want.

Latest version

Released:

Test django schema and data migrations, including ordering

Since Django 1.6 you can run a complete test case, or single test, using the complete dot notation for the element you want to run. Automatic test discovery will now find tests in any file that starts with test under the working directory, so addressing the question you would have to rename your files, but you can now keep them inside the directory you want.

Project description

Django Test Client

Features

  • Allows to test django schema and data migrations
  • Allows to test both forward and rollback migrations
  • Allows to test the migrations order
  • Allows to test migration names
  • Fully typed with annotations and checked with mypy, PEP561 compatible
  • Easy to start: has lots of docs, tests, and tutorials

Read the announcing post.See real-world usage example.

Installation

We support several django versions:

  • 1.11
  • 2.2
  • 3.0
  • 3.1

Other versions might work too, but they are not officially supported.

Testing django migrations

Testing migrations is not a frequent thing in django land.But, sometimes it is totally required. When?

When we do complex schema or data changesand what to be sure that existing data won't be corrupted.We might also want to be sure that all migrations can be safely rolled back.And as a final touch we want to be sure that migrationsare in the correct order and have correct dependencies.

Testing forward migrations

To test all migrations we have a Migrator class.

It has three methods to work with:

  • .apply_initial_migration() which takes app and migration names to generatea state before the actual migration happens. It creates the before stateby applying all migrations up to and including the ones passed as an argument.

  • .apply_tested_migration() which takes app and migration names to perform theactual migration

  • .reset() to clean everything up after we are done with testing

So, here's an example:

That was an example of a forward migration.

Backward migration

The thing is that you can also test backward migrations.Nothing really changes except migration names that you pass and your logic:

Testing migrations ordering

Sometimes we also want to be sure that our migrations are in the correct order.And all our dependecies = [...] are correct.

To achieve that we have plan.py module.

That's how it can be used:

This way you can be sure that migrationsand apps that depend on each other will be executed in the correct order.

Test framework integrations 🐍

Django run tests in parallel

We support several test frameworks as first-class citizens.That's a testing tool after all!

Note that the Django post_migrate signal's receiver list is cleared atthe start of tests and restored afterwards. If you need to test yourown post_migrate signals then attach/remove them during a test.

pytest

We ship django-test-migrations with a pytest pluginthat provides two convinient fixtures:

  • migrator_factory that gives you an opportunityto create Migrator classes for any database
  • migrator instance for the 'default' database

That's how it can be used:

unittest

We also ship an integration with the built-in unittest framework.

Here's how it can be used:

Choosing only migrations tests

In CI systems it is important to get instant feedback. Running tests thatapply database migration can slow down tests execution, so it is often a goodidea to run standard, fast, regular unit tests without migrations in parallelwith slower migrations tests.

pytest

django_test_migrations adds migration_test marker to each test usingmigrator_factory or migrator fixture.To run only migrations test, use -m option:

unittest

django_test_migrations adds migration_testtagto every MigratorTestCase subclass.To run only migrations tests, use --tag option:

Django Checks

django_test_migrations comes with 2 groups of Django's checks for:

  • detecting migrations scripts automatically generated names
  • validating some subset of database settings

Testing migration names

django generates migration names for you when you run makemigrations.And these names are bad (read more about why it is bad)!Just look at this: 0004_auto_20191119_2125.py

What does this migration do? What changes does it have?

One can also pass --name attribute when creating migrations, but it is easy to forget.

We offer an automated solution: django checkthat produces an error for each badly named migration.

Add our check into your INSTALLED_APPS:

Django Test Database

And then in your CI run:

This way you will be safe from wrong names in your migrations.

Run Django Tests In Docker

Do you have a migrations that cannot be renamed? Add them to the ignore list:

Run django tests with pytest

And we won't complain about them.

Or you can completely ignore entire app:

Database configuration

Add our check to INSTALLED_APPS:

Then just run check management command in your CI like listed in sectionabove.

Django transactiontestcase

Credits

This project is based on work of other awesome people:

License

MIT.

Release historyRelease notifications | RSS feed

1.1.0

1.0.0

0.3.0

0.2.0

0.1.0

Download files

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

Files for django-test-migrations, version 1.1.0
Filename, sizeFile typePython versionUpload dateHashes
Filename, size django_test_migrations-1.1.0-py3-none-any.whl (25.2 kB) File type Wheel Python version py3 Upload dateHashes
Filename, size django-test-migrations-1.1.0.tar.gz (21.5 kB) File type Source Python version None Upload dateHashes
Close

Hashes for django_test_migrations-1.1.0-py3-none-any.whl

Run Django Shell

Hashes for django_test_migrations-1.1.0-py3-none-any.whl
AlgorithmHash digest
SHA2567ea17dac1a0b0c8084681899c6563d85f4262832f2fbb0c6240b12e554333934
MD552e8d6d65a119e313a3f5489f5699b16
BLAKE2-256ee87170f1a51f6829cba1f5b5e864371d6ab5544b3bbff36203583ce7566ec33
Close

Run Django Tests Positive

Hashes for django-test-migrations-1.1.0.tar.gz

Run Django Tests For Kids

Hashes for django-test-migrations-1.1.0.tar.gz
AlgorithmHash digest
SHA25627c0127552920bbdc339a84de360f1792abc8c353e2c8d2b86af92dc1ade6703
MD577b7acfbdceb59b9ac7090afcc71fc98
BLAKE2-256ca3e11942a8769c47035dd55e26c8404d212015dc0503057263059b1ae8ecc33