API

anfema_django_testutils.testcases

exception anfema_django_testutils.testcases.PreconditionError

Exception to indicate a precondition failure.

class anfema_django_testutils.testcases.PreconditionContext

Context manager to handle precondition failures.

This context tags every raised AssertionError as precondition error.

with PreconditionContext():
     do_something()
anfema_django_testutils.testcases.precondition(func)

Decorator to define a callable as precondition.

Wraps a callable into a PreconditionContext, so that any AssertionError raised by the callable will lead to a precondition failure rather than a failure of the test.

from anfema_django_testutils.testcases import TestCase, precondition


class CustomTestCase(TestCase):

    @precondition
    def custom_precondition(self):
        do_something()
class anfema_django_testutils.testcases.repeat(repetitions, *, fail_fast=False)

Repeats a test method a given number of times.

The decorated test method will be executed within a subtest for each repetition, and the result of each failed repetition will be displayed in the test report.

Use this decorator when you have a test method that you want to repeat multiple times to ensure its stability and reliability. This can be particularly useful when dealing with non-deterministic tests or to catch intermittent failures that might not appear in a single test run.

from anfema_django_testutils.testcases import TestCase, repeat


class CustomTestCase(TestCase):

    @repeat(repetitions=100, fail_fast=True)
    def test_to_be_repeated_100_times(self):
        ...
Parameters:
  • repetitions (int) – The number of times the test method should be repeated.

  • fail_fast (bool) – If True, stop repeating the test method after the first failure.

class anfema_django_testutils.testcases.SimpleTestCase(*args, **kwargs)

Extends the django.test.SimpleTestCase class with a precondition failure status.

assertNotRaises(*unexpected_exception, atomic=False, msg=None)

Fail if an exception of class unexpected_exception is raised by the callable.

Any other exception type will not be caught, and the test case will be deemed to have suffered an error, or, if the exception is from type AssertionError a failure respectively a precondition error if its from type PreconditionError.

Parameters:
  • *unexpected_exception (type[Exception]) – Exception classes expected to not be raised.

  • atomic (bool) – If set to True, the context will be wrapped inside a transaction. Default is False.

  • msg (str) – Optional message to use on failure.

Return type:

Iterator[None]

assertPrecondition()

Context manager to handle precondition failures.

Any AssertionError or subclass of it, which is raised within the context will indicate the test has been finished with precondition failure rather than failure. Any other exception type will not be caught, and the test case will be deemed to have suffered an error, exactly as for an unexpected exception.

with self.assertPrecondition():
     do_something()
fail_precondition(msg=None)

Fail immediately with PreconditionError.

Call this method from a test case if you want to indicate it has been finished with precondition failure rather than failure.

Parameters:

msg (str) – Optional message to use.

preconditionFailureException

alias of PreconditionError

class anfema_django_testutils.testcases.TransactionTestCase(*args, **kwargs)

Extends the django.test.TransactionTestCase class with a precondition failure status.

See also

SimpleTestCase

transactionSubTest(msg=None, **params)

Like unittest.TestCase.subTest(), but runs within a transaction and ensures this transaction is rolled back afterward.

On database backends with no transaction support, transactionSubTest() behaves as unittest.TestCase.subTest().

Parameters:
  • msg (str) – Optional message used in case of failure.

  • **params – Additional information or context for the subtest.

class anfema_django_testutils.testcases.TestCase(*args, **kwargs)

Extends the django.test.TestCase class with a precondition failure status.

See also

SimpleTestCase

transactionSubTest(msg=None, **params)

Like unittest.TestCase.subTest(), but runs within a transaction and ensures this transaction is rolled back afterward.

On database backends with no transaction support, transactionSubTest() behaves as unittest.TestCase.subTest().

Parameters:
  • msg (str) – Optional message used in case of failure.

  • **params – Additional information or context for the subtest.

anfema_django_testutils.tags

This module provides test case tags related utilities.

class anfema_django_testutils.tags.TestCaseTag(*tags)

Decorator base class to create predefined test case tags.

New in version 0.2.

With this class you can create re-usable test case tags for your Django project:

class CustomTestCaseTag(TestCaseTag):
    """A custom testcase tag"""
    tags = ['VeryImportantTest', 'VerySlowTest']

@CustomTestCaseTag('Tag1', 'Tag2')
class CustomTestCase(TestCase):
    ...

This is pretty much the same as tagging a test class like:

@tag('CustomTestCaseTag', 'VeryImportantTest', 'VerySlowTest', 'Tag1', 'Tag2')
class CustomTestCase(TestCase):
        ...

Hint

If you have several tags to define you can generically use the TestCaseTag in a separate module, for instance:

# custom_tags.py

__all__ = ('AdminTest', 'ModelTest', 'FormTest')

import sys
from anfema_django_testutils.tags import TestCaseTag

for tag_name in __all__:
    setattr(sys.modules[__name__], tag_name, type(tag_name, (TestCaseTag,), {}),)
tags = []

A list with predefined extra tags.