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 anyAssertionError
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): ...
- 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 typePreconditionError
.
- 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
- 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 asunittest.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
- 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 asunittest.TestCase.subTest()
.- Parameters:
msg (str) – Optional message used in case of failure.
**params – Additional information or context for the subtest.