Skip to main content

softest - Soft Assertions

Supports the soft assert style of testing,
where multiple assertions can fail within the same method,
while collecting and formatting those failures' stack traces
for reporting by a final assert_all call.

Such stack traces are enhanced
to include the call hierarchy
from within the test class.

Usage

import softest


class ExampleTest(softest.TestCase):

	def test_example(self):
		self.soft_assert(self.assertEqual, 'Worf', 'wharf', 'Klingon is not ship receptacle')
		self.soft_assert(self.assertTrue, True)
		self.soft_assert(self.assertTrue, False)

		self.assert_all()

	def test_example_with_only_one_failure(self):
		self.soft_assert(self.assertTrue, False)
		self.assert_all()

	# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
	class SomeException(Exception):

		def __init__(self, reason:str):
			super().__init__(self)
			self.reason = reason

		def __str__(self):
			return self.reason

	# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----

	def test_assert_raises(self):
		with self.soft_assert_raises(self.SomeException) as assertion_context:
			print('=)')
		print(assertion_context)

		with self.soft_assert_raises_regex(self.SomeException, 'reason') as another_context:
			raise self.SomeException('reazon')
		print(another_context)

		self.assert_all()


if __name__ == '__main__':
	softest.main()

Error Console Output

======================================================================
FAIL: "test_assert_raises" (ExampleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\test\softest_test.py", line 41, in test_assert_raises
    self.assert_all()
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\main\softest\case.py", line 140, in assert_all
    self.fail(''.join(failure_output))
AssertionError: ++++ soft assert failure details follow below ++++

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The following 2 failures were found in "test_assert_raises" (ExampleTest):
+----------------------+----------------------+----------------------+
 Failure 1 ("test_assert_raises" method)
+----------------------+----------------------+----------------------+
Traceback (most recent call last):
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\test\softest_test.py", line 32, in test_assert_raises
    print('=)')
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\main\softest\context.py", line 51, in __exit__
    "Context actions did not raise {}".format(exception_name))
  File "C:\Users\nicholas.umble\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 135, in _raiseFailure
    raise self.test_case.failureException(msg)
AssertionError: Context actions did not raise SomeException

-+ [1/2] +-

+----------------------+----------------------+----------------------+
 Failure 2 ("test_assert_raises" method)
+----------------------+----------------------+----------------------+
SomeException: reazon
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\test\softest_test.py", line 37, in test_assert_raises
    raise SomeException('reazon')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\main\softest\context.py", line 71, in __exit__
    self.expected_regex.pattern, str(exception_value)))
  File "C:\Users\nicholas.umble\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 135, in _raiseFailure
    raise self.test_case.failureException(msg)
AssertionError: "reason" does not match the supplied regular expression "reazon"

-+ [2/2] +-

======================================================================
FAIL: "test_example" (ExampleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\test\softest_test.py", line 24, in test_example
    self.assert_all()
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\main\softest\case.py", line 140, in assert_all
    self.fail(''.join(failure_output))
AssertionError: ++++ soft assert failure details follow below ++++

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The following 2 failures were found in "test_example" (ExampleTest):
+----------------------+----------------------+----------------------+
 Failure 1 ("test_example" method)
+----------------------+----------------------+----------------------+
Traceback (most recent call last):
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\test\softest_test.py", line 20, in test_example
    self.soft_assert(self.assertEqual, 'Worf', 'wharf', 'Klingon is not ship receptacle')
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\main\softest\case.py", line 64, in soft_assert
    assert_method(*arguments, **keywords)
  File "C:\Users\nicholas.umble\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 829, in assertEqual
    assertion_func(first, second, msg=msg)
  File "C:\Users\nicholas.umble\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 1203, in assertMultiLineEqual
    self.fail(self._formatMessage(msg, standardMsg))
  File "C:\Users\nicholas.umble\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 670, in fail
    raise self.failureException(msg)
AssertionError: 'Worf' != 'wharf'
- Worf
+ wharf
 : Klingon is not ship receptacle

-+ [1/2] +-

+----------------------+----------------------+----------------------+
 Failure 2 ("test_example" method)
+----------------------+----------------------+----------------------+
Traceback (most recent call last):
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\test\softest_test.py", line 22, in test_example
    self.soft_assert(self.assertTrue, False)
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\main\softest\case.py", line 64, in soft_assert
    assert_method(*arguments, **keywords)
  File "C:\Users\nicholas.umble\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 682, in assertTrue
    raise self.failureException(msg)
AssertionError: False is not true

-+ [2/2] +-

======================================================================
FAIL: "test_example_with_only_one_failure" (ExampleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\test\softest_test.py", line 28, in test_example_with_only_one_failure
    self.assert_all()
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\main\softest\case.py", line 140, in assert_all
    self.fail(''.join(failure_output))
AssertionError: ++++ soft assert failure details follow below ++++

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The following failure was found in "test_example_with_only_one_failure" (ExampleTest):
+----------------------+----------------------+----------------------+
Traceback (most recent call last):
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\test\softest_test.py", line 27, in test_example_with_only_one_failure
    self.soft_assert(self.assertTrue, False)
  File "C:\more-programs\automation\workspaces\Ivan\Softest\src\main\softest\case.py", line 64, in soft_assert
    assert_method(*arguments, **keywords)
  File "C:\Users\nicholas.umble\AppData\Local\Programs\Python\Python36-32\lib\unittest\case.py", line 682, in assertTrue
    raise self.failureException(msg)
AssertionError: False is not true

-+ [1/1] +-

----------------------------------------------------------------------
Ran 3 tests in 0.016s

FAILED (failures=3)

Versions

1.2.0.0

Added soft_assert_raises and soft_assert_raises_regex to support/replace assertRaises and assertRaisesRegex. Updated output to include a failure count at the end of each failure, such as -+ [1/2] +-.

Metadata

Release files for softest 1.2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for softest 1.2.0.0
File Size Uploaded
softest-1.2.0.0.tar.gz 6.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for softest 1.2.0.0
File Interpreter ABI Platform
softest-1.2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.9 kB

Release files / softest-1.2.0.0.tar.gz

Download URL softest-1.2.0.0.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
68ba7a928748c0014cd159abf61b65f515ca1b42de0807d5baa2f3abe144d7fd
BLAKE2b-256 checksum
How to use checksums
c0569a2a97805a987540a4d7efafd8a1469fd279d12de7dcc05bf901b18a4dbb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

Release files / softest-1.2.0.0-py3-none-any.whl

Download URL softest-1.2.0.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1a2960d1c3026f1ab9631fb8f9970847896f2c33bc68e8465465a8c2d47c28cb
BLAKE2b-256 checksum
How to use checksums
a4f6dca6271bd1610189ccdd34f2e039caf13739e2c9bf72c5237047e064e9ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.5

Release history Release notifications | RSS feed

This release

1.2.0.0 This release

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page