From c2fef300224402ebb35c3c20243f06fbb67cddda Mon Sep 17 00:00:00 2001 From: Ben Khoo Date: Sat, 23 May 2015 21:37:45 +0800 Subject: [PATCH 1/2] Initial Version --- docs/topics/testing/advanced.txt | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/docs/topics/testing/advanced.txt b/docs/topics/testing/advanced.txt index 93583893a981f..73be4d2a87426 100644 --- a/docs/topics/testing/advanced.txt +++ b/docs/topics/testing/advanced.txt @@ -307,6 +307,53 @@ running your tests, you can define test-only models in its ``models.py`` file. +Test-only models +========================================================== + +Test-only models are models that are only created while running unit tests. This is particularly useful when writing :doc:`reusable applications ` that do not contain any models of their own. + +Consider the following structure:: + + polls/ + migrations/ + 0001_initial.py + __init__.py + tests/ + __init__.py + models.py + test_polls.py + ... + __init__.py + models.py + views.py + urls.py + ... + +The ``models.py`` file in the test module contains your test-only model definitions. It is important to leave the original ``models.py`` file in the app module even if it is empty. + +Create a migration for your test-models in the migrations module. You will need to adapt the migration to only execute during unit tests. You can do this by checking if the database name:: + + from django.db import migrations + from django.conf import settings + + + def is_test_db(): + return settings.DATABASES.get( + 'default', {}).get('NAME', '').startswith('test_') + + class Migration(migrations.Migration): + + dependencies = [] + + if is_test_db(): + operations = [ + migrations.CreateModel(...), + ... + ] + +That's it! The test-models will now be usable in your unit tests. + + .. _other-testing-frameworks: Using different testing frameworks From f29f95110619f146659c84e4dad6557949196f06 Mon Sep 17 00:00:00 2001 From: Ben Khoo Date: Sat, 23 May 2015 21:43:50 +0800 Subject: [PATCH 2/2] Fixed a typo. --- docs/topics/testing/advanced.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/topics/testing/advanced.txt b/docs/topics/testing/advanced.txt index 73be4d2a87426..5e44718df1d23 100644 --- a/docs/topics/testing/advanced.txt +++ b/docs/topics/testing/advanced.txt @@ -331,7 +331,7 @@ Consider the following structure:: The ``models.py`` file in the test module contains your test-only model definitions. It is important to leave the original ``models.py`` file in the app module even if it is empty. -Create a migration for your test-models in the migrations module. You will need to adapt the migration to only execute during unit tests. You can do this by checking if the database name:: +Create a migration for your test-models in the migrations module. You will need to adapt the migration to only execute during unit tests. You can do this by checking the database name:: from django.db import migrations from django.conf import settings