@@ -28,82 +28,82 @@ dynamically in a hook or fixture.
28
28
29
29
.. decorator :: pytest.mark.django_db([transaction=False, reset_sequences=False, databases=None, serialized_rollback=False, available_apps=None])
30
30
31
- This is used to mark a test function as requiring the database. It
32
- will ensure the database is set up correctly for the test. Each test
33
- will run in its own transaction which will be rolled back at the end
34
- of the test. This behavior is the same as Django's standard
35
- :class: `~django.test.TestCase ` class.
36
-
37
- In order for a test to have access to the database it must either be marked
38
- using the :func: `~pytest.mark.django_db ` mark or request one of the :fixture: `db `,
39
- :fixture: `transactional_db ` or :fixture: `django_db_reset_sequences ` fixtures.
40
- Otherwise the test will fail when trying to access the database.
41
-
42
- :type transaction: bool
43
- :param transaction:
44
- The ``transaction `` argument will allow the test to use real transactions.
45
- With ``transaction=False `` (the default when not specified), transaction
46
- operations are noops during the test. This is the same behavior that
47
- :class: `django.test.TestCase ` uses. When ``transaction=True ``, the behavior
48
- will be the same as :class: `django.test.TransactionTestCase `.
49
-
50
-
51
- :type reset_sequences: bool
52
- :param reset_sequences:
53
- The ``reset_sequences `` argument will ask to reset auto increment sequence
54
- values (e.g. primary keys) before running the test. Defaults to
55
- ``False ``. Must be used together with ``transaction=True `` to have an
56
- effect. Please be aware that not all databases support this feature.
57
- For details see :attr: `django.test.TransactionTestCase.reset_sequences `.
58
-
59
-
60
- :type databases: Iterable[str] | str | None
61
- :param databases:
62
-
63
- The ``databases `` argument defines which databases in a multi-database
64
- configuration will be set up and may be used by the test. Defaults to
65
- only the ``default `` database. The special value ``"__all__" `` may be used
66
- to specify all configured databases.
67
- For details see :attr: `django.test.TransactionTestCase.databases ` and
68
- :attr: `django.test.TestCase.databases `.
69
-
70
- :type serialized_rollback: bool
71
- :param serialized_rollback:
72
- The ``serialized_rollback `` argument enables :ref: `rollback emulation
73
- <test-case-serialized-rollback>`. After a transactional test (or any test
74
- using a database backend which doesn't support transactions) runs, the
75
- database is flushed, destroying data created in data migrations. Setting
76
- ``serialized_rollback=True `` tells Django to serialize the database content
77
- during setup, and restore it during teardown.
78
-
79
- Note that this will slow down that test suite by approximately 3x.
80
-
81
- :type available_apps: Iterable[str] | None
82
- :param available_apps:
83
- .. caution ::
84
-
85
- This argument is **experimental ** and is subject to change without
86
- deprecation.
87
-
88
- The ``available_apps `` argument defines a subset of apps that are enabled
89
- for a specific set of tests. Setting ``available_apps `` configures models
90
- for which types/permissions will be created before each test, and which
91
- model tables will be emptied after each test (this truncation may cascade
92
- to unavailable apps models).
93
-
94
- For details see :attr: `django.test.TransactionTestCase.available_apps `
31
+ This is used to mark a test function as requiring the database. It
32
+ will ensure the database is set up correctly for the test. Each test
33
+ will run in its own transaction which will be rolled back at the end
34
+ of the test. This behavior is the same as Django's standard
35
+ :class: `~django.test.TestCase ` class.
36
+
37
+ In order for a test to have access to the database it must either be marked
38
+ using the :func: `~pytest.mark.django_db ` mark or request one of the :fixture: `db `,
39
+ :fixture: `transactional_db ` or :fixture: `django_db_reset_sequences ` fixtures.
40
+ Otherwise the test will fail when trying to access the database.
41
+
42
+ :type transaction: bool
43
+ :param transaction:
44
+ The ``transaction `` argument will allow the test to use real transactions.
45
+ With ``transaction=False `` (the default when not specified), transaction
46
+ operations are noops during the test. This is the same behavior that
47
+ :class: `django.test.TestCase ` uses. When ``transaction=True ``, the behavior
48
+ will be the same as :class: `django.test.TransactionTestCase `.
49
+
50
+
51
+ :type reset_sequences: bool
52
+ :param reset_sequences:
53
+ The ``reset_sequences `` argument will ask to reset auto increment sequence
54
+ values (e.g. primary keys) before running the test. Defaults to
55
+ ``False ``. Must be used together with ``transaction=True `` to have an
56
+ effect. Please be aware that not all databases support this feature.
57
+ For details see :attr: `django.test.TransactionTestCase.reset_sequences `.
58
+
59
+
60
+ :type databases: Iterable[str] | str | None
61
+ :param databases:
62
+
63
+ The ``databases `` argument defines which databases in a multi-database
64
+ configuration will be set up and may be used by the test. Defaults to
65
+ only the ``default `` database. The special value ``"__all__" `` may be used
66
+ to specify all configured databases.
67
+ For details see :attr: `django.test.TransactionTestCase.databases ` and
68
+ :attr: `django.test.TestCase.databases `.
69
+
70
+ :type serialized_rollback: bool
71
+ :param serialized_rollback:
72
+ The ``serialized_rollback `` argument enables :ref: `rollback emulation
73
+ <test-case-serialized-rollback>`. After a transactional test (or any test
74
+ using a database backend which doesn't support transactions) runs, the
75
+ database is flushed, destroying data created in data migrations. Setting
76
+ ``serialized_rollback=True `` tells Django to serialize the database content
77
+ during setup, and restore it during teardown.
78
+
79
+ Note that this will slow down that test suite by approximately 3x.
80
+
81
+ :type available_apps: Iterable[str] | None
82
+ :param available_apps:
83
+ .. caution ::
84
+
85
+ This argument is **experimental ** and is subject to change without
86
+ deprecation.
87
+
88
+ The ``available_apps `` argument defines a subset of apps that are enabled
89
+ for a specific set of tests. Setting ``available_apps `` configures models
90
+ for which types/permissions will be created before each test, and which
91
+ model tables will be emptied after each test (this truncation may cascade
92
+ to unavailable apps models).
93
+
94
+ For details see :attr: `django.test.TransactionTestCase.available_apps `
95
95
96
96
97
97
.. note ::
98
98
99
- If you want access to the Django database inside a *fixture *, this marker may
100
- or may not help even if the function requesting your fixture has this marker
101
- applied, depending on pytest's fixture execution order. To access the database
102
- in a fixture, it is recommended that the fixture explicitly request one of the
103
- :fixture: `db `, :fixture: `transactional_db `,
104
- :fixture: `django_db_reset_sequences ` or
105
- :fixture: `django_db_serialized_rollback ` fixtures. See below for a description
106
- of them.
99
+ If you want access to the Django database inside a *fixture *, this marker may
100
+ or may not help even if the function requesting your fixture has this marker
101
+ applied, depending on pytest's fixture execution order. To access the database
102
+ in a fixture, it is recommended that the fixture explicitly request one of the
103
+ :fixture: `db `, :fixture: `transactional_db `,
104
+ :fixture: `django_db_reset_sequences ` or
105
+ :fixture: `django_db_serialized_rollback ` fixtures. See below for a description
106
+ of them.
107
107
108
108
.. note :: Automatic usage with ``django.test.TestCase``.
109
109
@@ -118,36 +118,36 @@ dynamically in a hook or fixture.
118
118
119
119
.. decorator :: pytest.mark.urls(urls)
120
120
121
- Specify a different ``settings.ROOT_URLCONF `` module for the marked tests.
121
+ Specify a different ``settings.ROOT_URLCONF `` module for the marked tests.
122
122
123
- :type urls: str
124
- :param urls:
125
- The urlconf module to use for the test, e.g. ``myapp.test_urls ``. This is
126
- similar to Django's ``TestCase.urls `` attribute.
123
+ :type urls: str
124
+ :param urls:
125
+ The urlconf module to use for the test, e.g. ``myapp.test_urls ``. This is
126
+ similar to Django's ``TestCase.urls `` attribute.
127
127
128
- Example usage::
128
+ Example usage::
129
129
130
- @pytest.mark.urls('myapp.test_urls')
131
- def test_something(client):
132
- assert b'Success!' in client.get('/some_url_defined_in_test_urls/').content
130
+ @pytest.mark.urls('myapp.test_urls')
131
+ def test_something(client):
132
+ assert b'Success!' in client.get('/some_url_defined_in_test_urls/').content
133
133
134
134
135
135
``pytest.mark.ignore_template_errors `` - ignore invalid template variables
136
136
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
137
137
138
138
.. decorator :: pytest.mark.ignore_template_errors
139
139
140
- Ignore errors when using the ``--fail-on-template-vars `` option, i.e.
141
- do not cause tests to fail if your templates contain invalid variables.
140
+ Ignore errors when using the ``--fail-on-template-vars `` option, i.e.
141
+ do not cause tests to fail if your templates contain invalid variables.
142
142
143
- This marker sets the ``string_if_invalid `` template option.
144
- See :ref: `django:invalid-template-variables `.
143
+ This marker sets the ``string_if_invalid `` template option.
144
+ See :ref: `django:invalid-template-variables `.
145
145
146
- Example usage::
146
+ Example usage::
147
147
148
- @pytest.mark.ignore_template_errors
149
- def test_something(client):
150
- client('some-url-with-invalid-template-vars')
148
+ @pytest.mark.ignore_template_errors
149
+ def test_something(client):
150
+ client('some-url-with-invalid-template-vars')
151
151
152
152
153
153
Fixtures
@@ -383,15 +383,15 @@ created in data migrations, you should add the
383
383
384
384
.. note :: Combining database access fixtures.
385
385
386
- When using multiple database fixtures together, only one of them is
387
- used. Their order of precedence is as follows (the last one wins):
386
+ When using multiple database fixtures together, only one of them is
387
+ used. Their order of precedence is as follows (the last one wins):
388
388
389
- * ``db ``
390
- * ``transactional_db ``
389
+ * ``db ``
390
+ * ``transactional_db ``
391
391
392
- In addition, using ``live_server `` or ``django_db_reset_sequences `` will also
393
- trigger transactional database access, and ``django_db_serialized_rollback ``
394
- regular database access, if not specified.
392
+ In addition, using ``live_server `` or ``django_db_reset_sequences `` will also
393
+ trigger transactional database access, and ``django_db_serialized_rollback ``
394
+ regular database access, if not specified.
395
395
396
396
.. fixture :: settings
397
397
@@ -419,10 +419,10 @@ Example
419
419
420
420
.. py :function :: django_assert_num_queries(num, connection = None , info = None , * , using = None )
421
421
422
- :param num: expected number of queries
423
- :param connection: optional database connection
424
- :param str info: optional info message to display on failure
425
- :param str using: optional database alias
422
+ :param num: expected number of queries
423
+ :param connection: optional database connection
424
+ :param str info: optional info message to display on failure
425
+ :param str using: optional database alias
426
426
427
427
This fixture allows to check for an expected number of DB queries.
428
428
@@ -459,10 +459,10 @@ If you use type annotations, you can annotate the fixture like this::
459
459
460
460
.. py :function :: django_assert_max_num_queries(num, connection = None , info = None , * , using = None )
461
461
462
- :param num: expected maximum number of queries
463
- :param connection: optional database connection
464
- :param str info: optional info message to display on failure
465
- :param str using: optional database alias
462
+ :param num: expected maximum number of queries
463
+ :param connection: optional database connection
464
+ :param str info: optional info message to display on failure
465
+ :param str using: optional database alias
466
466
467
467
This fixture allows to check for an expected maximum number of DB queries.
468
468
@@ -492,12 +492,12 @@ If you use type annotations, you can annotate the fixture like this::
492
492
493
493
.. py :function :: django_capture_on_commit_callbacks(* , using = DEFAULT_DB_ALIAS , execute = False )
494
494
495
- :param using:
496
- The alias of the database connection to capture callbacks for.
497
- :param execute:
498
- If True, all the callbacks will be called as the context manager exits, if
499
- no exception occurred. This emulates a commit after the wrapped block of
500
- code.
495
+ :param using:
496
+ The alias of the database connection to capture callbacks for.
497
+ :param execute:
498
+ If True, all the callbacks will be called as the context manager exits, if
499
+ no exception occurred. This emulates a commit after the wrapped block of
500
+ code.
501
501
502
502
.. versionadded :: 4.4
503
503
0 commit comments