diff options
| author | Christopher Adams <christopher.r.adams@gmail.com> | 2022-06-25 17:15:55 -0400 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-06-27 11:29:21 +0200 |
| commit | 90d2f9f41671ef01c8e8e7b5648f95c9bf512aae (patch) | |
| tree | dddbdfa82bbd4f9ddbfbe9bb06a5bc04302b88fa /docs/topics/testing/tools.txt | |
| parent | b2eff16806057095c7dd3daa9402ad615e51627f (diff) | |
Fixed #33422 -- Improved docs about isolating apps.
Diffstat (limited to 'docs/topics/testing/tools.txt')
| -rw-r--r-- | docs/topics/testing/tools.txt | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt index bec54d7568..9e3d2c4956 100644 --- a/docs/topics/testing/tools.txt +++ b/docs/topics/testing/tools.txt @@ -1398,6 +1398,69 @@ LOCALE_PATHS, LANGUAGE_CODE Default translation and loaded translations MEDIA_ROOT, DEFAULT_FILE_STORAGE Default file storage ================================ ======================== +Isolating apps +-------------- + +.. function:: utils.isolate_apps(*app_labels, attr_name=None, kwarg_name=None) + + Registers the models defined within a wrapped context into their own + isolated :attr:`~django.apps.apps` registry. This functionality is useful + when creating model classes for tests, as the classes will be cleanly + deleted afterward, and there is no risk of name collisions. + + The app labels which the isolated registry should contain must be passed as + individual arguments. You can use ``isolate_apps()`` as a decorator or a + context manager. For example:: + + from django.db import models + from django.test import SimpleTestCase + from django.test.utils import isolate_apps + + class MyModelTests(SimpleTestCase): + + @isolate_apps("app_label") + def test_model_definition(self): + class TestModel(models.Model): + pass + ... + + … or:: + + with isolate_apps("app_label"): + class TestModel(models.Model): + pass + ... + + The decorator form can also be applied to classes. + + Two optional keyword arguments can be specified: + + * ``attr_name``: attribute assigned the isolated registry if used as a + class decorator. + * ``kwarg_name``: keyword argument passing the isolated registry if used as + a function decorator. + + The temporary ``Apps`` instance used to isolate model registration can be + retrieved as an attribute when used as a class decorator by using the + ``attr_name`` parameter:: + + @isolate_apps("app_label", attr_name="apps") + class TestModelDefinition(SimpleTestCase): + def test_model_definition(self): + class TestModel(models.Model): + pass + self.assertIs(self.apps.get_model("app_label", "TestModel"), TestModel) + + … or alternatively as an argument on the test method when used as a method + decorator by using the ``kwarg_name`` parameter:: + + class TestModelDefinition(SimpleTestCase): + @isolate_apps("app_label", kwarg_name="apps") + def test_model_definition(self, apps): + class TestModel(models.Model): + pass + self.assertIs(apps.get_model("app_label", "TestModel"), TestModel) + .. _emptying-test-outbox: Emptying the test outbox |
