Package: nrt-pytest-soft-asserts
Pytest soft asserts.Supported asserts:AssertDescriptionExampleReturnassert_true(condition, message=None)Verify that condition is Truesoft_asserts.assert_true(a == b)True if assertion passes, False if assertion failsassert_false(condition, message=None)Verify that condition is Falsesoft_asserts.assert_false(a == b)True if assertion passes, False if assertion failsassert_equal(first, second, message=None)Verify that first is equal to secondsoft_asserts.assert_equal(a, b)True if assertion passes, False if assertion failsassert_not_equal(first, second, message=None)Verify that first is not equal to secondsoft_asserts.assert_not_equal(a, b)True if assertion passes, False if assertion failsassert_is(first, second, message=None)Verify that first and second are the same objectsoft_asserts.assert_is(a, b)True if assertion passes, False if assertion failsassert_is_not(first, second, message=None)Verify that first and second are not the same objectsoft_asserts.assert_is_not(a, b)True if assertion passes, False if assertion failsassert_is_none(obj, message=None)Verify that obj is Nonesoft_asserts.assert_is_none(a)True if assertion passes, False if assertion failsassert_is_not_none(obj, message=None)Verify that obj is not Nonesoft_asserts.assert_is_not_none(a)True if assertion passes, False if assertion failsassert_in(obj, container, message=None)Verify that obj is in containersoft_asserts.assert_in(a, [a, b, c])True if assertion passes, False if assertion failsassert_not_in(obj, container, message=None)Verify that obj is not in containersoft_asserts.assert_not_in(a, [b, c])True if assertion passes, False if assertion failsassert_is_instance(obj, cls, message=None)Verify that obj is instance of clssoft_asserts.assert_is_instance(a, A)True if assertion passes, False if assertion failsassert_is_not_instance(obj, cls, message=None)Verify that obj is not instance of clssoft_asserts.assert_is_not_instance(a, B)True if assertion passes, False if assertion failsassert_almost_equal(first, second, delta, message=None)Verify that first is almost equal to secondand the different is equal or less to deltasoft_asserts.assert_almost_equal(1.001, 1.002, 0.1)True if assertion passes, False if assertion failsassert_not_almost_equal(first, second, delta, message=None)Verify that first is not almost equal to secondand the different is more than deltasoft_asserts.assert_not_almost_equal(1.001, 1.002, 0.00001)True if assertion passes, False if assertion failsassert_raises(exception, method: Callable, *args, **kwargs)Verify that method execution raise exceptionsoft_asserts.assert_raises(TypeError, sum, 'a', 2)True if assertion passes, False if assertion failsassert_raises_with(exception, message=None)Verify that execution in 'with' block raise exceptionwith soft_asserts.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, callsoft_asserts.assert_all().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 skippedif it is depended on the failed step.Example:To make test be skipped if step failed, a custom marker should be created.This is an example of such custom marker, but user can create its own custom marker.In conftest.py file:importpytest@pytest.fixture(autouse=True)defrun_before_test(request):markers=request.node.own_markersformarkerinmarkers:ifmarker.name=='soft_asserts':marker_params=marker.kwargssoft_asserts=marker_params['soft_asserts']skip_steps=marker_params['skip_steps']forstepinskip_steps:ifsoft_asserts.is_step_in_failure_steps(step):pytest.skip(f'Skipped because [{step}] failed.')importpytestfromnrt_pytest_soft_asserts.soft_assertsimportSoftAssertssoft_asserts=SoftAsserts()STEP_1='step_1'STEP_2='step_2'deftest_assert_with_steps():soft_asserts.set_step(STEP_1)# result is Falseresult=soft_asserts.assert_true(False)# print Falseprint(result)soft_asserts.set_step(STEP_2)soft_asserts.assert_true(False)# From this code section steps will not be attached to failure assertssoft_asserts.unset_step()soft_asserts.assert_true(False)soft_asserts.assert_all()@pytest.mark.soft_asserts(soft_asserts=soft_asserts,skip_steps=[STEP_1])deftest_skip_if_step_1_fail():soft_asserts.assert_true(True)@pytest.mark.soft_asserts(soft_asserts=soft_asserts,skip_steps=[STEP_2])deftest_skip_if_step_2_fail():soft_asserts.assert_true(True)Print error on each failed assertEach assertion failure can be printed.This can be done by adding logger or by adding a print method.In case a logger will be added to soft asserts, then logger.error(message) will be used.In case a print method will be added to soft asserts, then print_method(message) will be used.logger and print method cannot be added together.logger example:importloggingfromnrt_pytest_soft_asserts.soft_assertsimportSoftAssertslogger=logging.getLogger('test')soft_asserts=SoftAsserts()# logger will be used to print message after each assert fail.soft_asserts.set_logger(logger)deftest_assert_true_fail():i=1j=2# logger.error() will print messages to console for each assert that failssoft_asserts.assert_true(i+j==5)# f'{i} is different from {j}' will be printed by logger.error() after assert will failsoft_asserts.assert_equal(i,j,f'{i}is different from{j}')soft_asserts.assert_all()print method example:fromnrt_pytest_soft_asserts.soft_assertsimportSoftAssertsdefprint_method(message):print(message)soft_asserts=SoftAsserts()# print_method will be used to print message after each assert fail.soft_asserts.set_print_method(print_method)deftest_assert_true_fail():i=1j=2# print_method will print messages to console for each assert that failssoft_asserts.assert_true(i+j==5)# f'{i} is different from {j}' will be printed by print_method after assert will failsoft_asserts.assert_equal(i,j,f'{i}is different from{j}')soft_asserts.assert_all()Wiki:https://github.com/etuzon/python-nrt-pytest-soft-asserts/wiki