Package: nrt-unittest-soft-asserts
Unittest soft asserts.Supported asserts:AssertDescriptionExampleReturnsoft_assert_true(condition, message=None)Verify that condition is Trueself.soft_assert_true(a == b)True if assertion passes, False if assertion failssoft_assert_false(condition, message=None)Verify that condition is Falseself.soft_assert_false(a == b)True if assertion passes, False if assertion failssoft_assert_equal(first, second, message=None)Verify that first is equal to secondself.soft_assert_equal(a, b)True if assertion passes, False if assertion failssoft_assert_not_equal(first, second, message=None)Verify that first is not equal to secondself.soft_assert_not_equal(a, b)True if assertion passes, False if assertion failssoft_assert_is(first, second, message=None)Verify that first and second are the same objectself.soft_assert_is(a, b)True if assertion passes, False if assertion failssoft_assert_is_not(first, second, message=None)Verify that first and second are not the same objectself.soft_assert_is_not(a, b)True if assertion passes, False if assertion failssoft_assert_is_none(obj, message=None)Verify that obj is Noneself.soft_assert_is_none(a)True if assertion passes, False if assertion failssoft_assert_is_not_none(obj, message=None)Verify that obj is not Noneself.soft_assert_is_not_none(a)True if assertion passes, False if assertion failssoft_assert_in(obj, container, message=None)Verify that obj is in containerself.soft_assert_in(a, [a, b, c])True if assertion passes, False if assertion failssoft_assert_not_in(obj, container, message=None)Verify that obj is not in containerself.soft_assert_not_in(a, [b, c])True if assertion passes, False if assertion failssoft_assert_is_instance(obj, cls, message=None)Verify that obj is instance of clsself.soft_assert_is_instance(a, A)True if assertion passes, False if assertion failssoft_assert_is_not_instance(obj, cls, message=None)Verify that obj is not instance of clsself.soft_assert_is_not_instance(a, B)True if assertion passes, False if assertion failssoft_assert_almost_equal(first, second, delta, message=None)Verify that first is almost equal to secondand the different is equal or less to deltaself.soft_assert_almost_equal(1.001, 1.002, 0.1)True if assertion passes, False if assertion failssoft_assert_not_almost_equal(first, second, delta, message=None)Verify that first is not almost equal to secondand the different is more than deltaself.soft_assert_not_almost_equal(1.001, 1.002, 0.0001)True if assertion passes, False if assertion failssoft_assert_raises(exception, method: Callable, *args, **kwargs)Verify that method execution raise exceptionself.soft_assert_raises(TypeError, sum, 'a', 2)True if assertion passes, False if assertion failssoft_assert_raises_with(exception, message=None)Verify that execution in 'with' block raise exceptionwith self.soft_assert_raised_with(ValueError):raise ValueError(ERROR_MESSAGE_1)In the end of each test, the soft asserts will be verified and the test will be marked as failed if any of the asserts failed.To verify the soft asserts in the middle of the test, callself.soft_assert_all().soft_assert_all() will raiseAssertionErrorif any of the asserts failed.StepsEach testing section can be divided to steps. The meaning of this is that if one of the asserts in a step failed,then the step will be entered to list of failure steps and next test can be skipped if it is depended on the failed step.Example:To make test be skipped if step failed, a custom decorator should be created.This is an example of such custom decorator, but user can create its own custom decorator.importfunctoolsfromnrt_unittest_soft_asserts.soft_assertsimportSoftAsserts# Custom decorator to skip test if one of the steps failed.defskip_steps(skip_steps_list:list[str]):defdecorator(test_method):@functools.wraps(test_method)defwrapper(self,*args,**kwargs):forstepinskip_steps_list:ifself.is_step_in_failure_steps(step):self.skipTest(f'Skipped because step [{step}] failed.')returntest_method(self,*args,**kwargs)returnwrapperreturndecoratorclassSoftAssertsExamplesTests(SoftAsserts):STEP_1='step 1'STEP_2='step 2'STEP_3='step 3'deftest_01_assert_with_steps_test_will_fail(self):self.set_step(self.STEP_1)# result is Falseresult=self.soft_assert_true(False)self.set_step(self.STEP_2)self.soft_assert_true(False)# From this code section steps will not be attached to failure assertsself.unset_step()self.soft_assert_true(False)self.soft_assert_all()@skip_steps([STEP_1])deftest_02_skip_if_step_1_fail(self):self.soft_assert_true(False)self.soft_assert_all()@skip_steps([STEP_2])deftest_03_skip_if_step_2_fail(self):self.soft_assert_true(False)self.soft_assert_all()@skip_steps([STEP_1,STEP_2])deftest_04_skip_if_step_1_or_step2_fail(self):self.soft_assert_true(False)self.soft_assert_all()@skip_steps([STEP_3])deftest_05_skip_if_step_3_fail_will_not_be_skipped(self):"""Test should not be skipped because {STEP_3} is not in failure steps."""# result is Trueresult=self.soft_assert_true(True)self.soft_assert_all()Print error on each failed assertEach assert failure can be printed.This can be done by adding logger or by adding a print method.In case a logger will be added, then logger.error(message) will be used.In case a print method will be added, then print_method(message) will be used.logger and print method cannot be added together.logger example:importloggingfromnrt_unittest_soft_asserts.soft_assertsimportSoftAssertsclassSoftAssertsWithLoggerTests(SoftAsserts):logger=logging.getLogger('test')@classmethoddefsetUpClass(cls):super().setUpClass()cls.set_logger(cls.logger)deftest_assert_true_fail(self):i=1j=2# logger.error() will print messages to console for each assert that failsself.soft_assert_true(i+j==5)self.soft_assert_equal(i,j,f'{i}is different from{j}')self.soft_assert_all()print method example:fromnrt_unittest_soft_asserts.soft_assertsimportSoftAssertsclassSoftAssertsWithPrintMethodTests(SoftAsserts):defsetUp(self):super().setUp()self.set_print_method(self.__print_message)deftest_assert_true_fail(self):i=1j=2# self.__print_message() will print messages# to console for each assert that failsself.soft_assert_true(i+j==5)self.soft_assert_equal(i,j,f'{i}is different from{j}')self.soft_assert_all()def__print_message(self,msg):print()Wiki:https://github.com/etuzon/python-nrt-unittest-soft-asserts/wiki