module documentation

Undocumented

Function setup​_databases Create the test databases.
Function setup​_test​_environment Perform global pre-test setup, such as installing the instrumented template renderer and setting the email backend to the locmem email backend.
Function teardown​_databases Destroy all the non-mirror databases.
Function teardown​_test​_environment Perform any global post-test teardown, such as restoring the original template renderer and restoring the email sending functions.
Constant TZ​_SUPPORT Undocumented
Variable jinja2 Undocumented
Variable requires​_tz​_support Undocumented
Class _​Test​State Undocumented
Class ​Approximate Undocumented
Class ​Capture​Queries​Context Context manager that captures queries executed by the specified connection.
Class ​Context​List A wrapper that provides direct key access to context items contained in a list of context objects.
Class ignore​_warnings Undocumented
Class isolate​_apps Act as either a decorator or a context manager to register models defined in its wrapped context to an isolated registry.
Class ​Logging​Capture​Mixin Capture the output from the 'django' logger and store it on the class's logger_output attribute.
Class modify​_settings Like override_settings, but makes it possible to append, prepend, or remove items instead of redefining the entire list.
Class ​Null​Time​Keeper Undocumented
Class override​_script​_prefix Decorator or context manager to temporary override the script prefix.
Class override​_settings No summary
Class override​_system​_checks Act as a decorator. Override list of registered system checks. Useful when you override INSTALLED_APPS, e.g. if you exclude auth app, you also need to exclude its system checks.
Class ​Test​Context​Decorator A base class that can either be used as a context manager during tests or as a test function or unittest.TestCase subclass decorator to perform temporary alterations.
Class ​Time​Keeper Undocumented
Function captured​_output Return a context manager used by captured_stdout/stdin/stderr that temporarily replaces the sys stream stream_name with a StringIO.
Function captured​_stderr Capture the output of sys.stderr:
Function captured​_stdin Capture the input to sys.stdin:
Function captured​_stdout Capture the output of sys.stdout:
Function compare​_xml No summary
Function dependency​_ordered Reorder test_databases into an order that honors the dependencies described in TEST[DEPENDENCIES].
Function extend​_sys​_path Context manager to temporarily add paths to sys.path.
Function freeze​_time No summary
Function get​_runner Undocumented
Function get​_unique​_databases​_and​_mirrors Figure out which databases actually need to be created.
Function instrumented​_test​_render An instrumented Template render method, providing a signal that can be intercepted by the test Client.
Function isolate​_lru​_cache Clear the cache of an LRU cache object on entering and exiting.
Function iter​_test​_cases Return an iterator over a test suite's unittest.TestCase objects.
Function register​_lookup Context manager to temporarily register lookups on a model field using lookup_name (or the lookup's lookup_name if not provided).
Function require​_jinja2 Decorator to enable a Jinja2 template engine in addition to the regular Django template engine for a test or skip it if Jinja2 isn't available.
Function tag Decorator to add tags to a test class or method.
def setup_databases(verbosity, interactive, *, time_keeper=None, keepdb=False, debug_sql=False, parallel=0, aliases=None, serialized_aliases=None, **kwargs):
Create the test databases.
def setup_test_environment(debug=None):
Perform global pre-test setup, such as installing the instrumented template renderer and setting the email backend to the locmem email backend.
def teardown_databases(old_config, verbosity, parallel=0, keepdb=False):
Destroy all the non-mirror databases.
def teardown_test_environment():
Perform any global post-test teardown, such as restoring the original template renderer and restoring the email sending functions.
TZ_SUPPORT =

Undocumented

Value
hasattr(time, 'tzset')
jinja2 =

Undocumented

requires_tz_support =

Undocumented

@contextmanager
def captured_output(stream_name):

Return a context manager used by captured_stdout/stdin/stderr that temporarily replaces the sys stream stream_name with a StringIO.

Note: This function and the following captured_std* are copied
from CPython's test.support module.
def captured_stderr():

Capture the output of sys.stderr:

with captured_stderr() as stderr:
print("hello", file=sys.stderr)

self.assertEqual(stderr.getvalue(), "hello

")

def captured_stdin():

Capture the input to sys.stdin:

with captured_stdin() as stdin:
stdin.write('hello
')
stdin.seek(0) # call test code that consumes from sys.stdin captured = input()

self.assertEqual(captured, "hello")

def captured_stdout():

Capture the output of sys.stdout:

with captured_stdout() as stdout:
print("hello")

self.assertEqual(stdout.getvalue(), "hello

")

def compare_xml(want, got):

Try to do a 'xml-comparison' of want and got. Plain string comparison doesn't always work because, for example, attribute ordering should not be important. Ignore comment nodes, processing instructions, document type node, and leading and trailing whitespaces.

Based on https://github.com/lxml/lxml/blob/master/src/lxml/doctestcompare.py

def dependency_ordered(test_databases, dependencies):
Reorder test_databases into an order that honors the dependencies described in TEST[DEPENDENCIES].
@contextmanager
def extend_sys_path(*paths):
Context manager to temporarily add paths to sys.path.
@contextmanager
def freeze_time(t):
Context manager to temporarily freeze time.time(). This temporarily modifies the time function of the time module. Modules which import the time function directly (e.g. from time import time) won't be affected This isn't meant as a public API, but helps reduce some repetitive code in Django's test suite.
def get_runner(settings, test_runner_class=None):

Undocumented

def get_unique_databases_and_mirrors(aliases=None):

Figure out which databases actually need to be created.

Deduplicate entries in DATABASES that correspond the same database or are configured as test mirrors.

Return two values: - test_databases: ordered mapping of signatures to (name, list of aliases)

where all aliases share the same underlying database.
  • mirrored_aliases: mapping of mirror aliases to original aliases.
def instrumented_test_render(self, context):
An instrumented Template render method, providing a signal that can be intercepted by the test Client.
@contextmanager
def isolate_lru_cache(lru_cache_object):
Clear the cache of an LRU cache object on entering and exiting.
def iter_test_cases(tests):

Return an iterator over a test suite's unittest.TestCase objects.

The tests argument can also be an iterable of TestCase objects.

@contextmanager
def register_lookup(field, *lookups, lookup_name=None):
Context manager to temporarily register lookups on a model field using lookup_name (or the lookup's lookup_name if not provided).
def require_jinja2(test_func):
Decorator to enable a Jinja2 template engine in addition to the regular Django template engine for a test or skip it if Jinja2 isn't available.
def tag(*tags):
Decorator to add tags to a test class or method.