Delayed/soft assertions for python
Project description
Python-Delayed-Assert
Delayed aka. Soft asserts for python
Few features:
- No Dependenices on any other framework/library.
- Should work with any testing framework.
- Can be use as decorator or context manager.
Installation
Install via pip
pip install delayed-assert
Install from master
pip install git+https://github.com/pr4bh4sh/delayed-assert
Uses
See example_unittest.py for usage.
Using assertion library with lambda
Pass the assertion call as
expect(lambda: self.assertListEqual([4,5,6,2,5],[7,8]))
While I've tested only with unittest asserttion,It should be able to use any assertion library.
Keep in mind that, Python does not support statement inside lambda, so
expect(lambda: assert 1 == 1)
won't work as it is not a valid lambda expression in python
Current possible uses
def testSomething(self):
delayed_assert.expect(1 == 1) # will succeed
delayed_assert.expect(1 == 2) # will fail but won't stop execution
delayed_assert.expect(3 == 2, "Value don't match") # will fail but won't stop execution
delayed_assert.expect(3 == 3) # will succeed
# will stop execution and show the stack trace of 2nd assertion
delayed_assert.assert_expectations()
def testLambdas(self):
expect(lambda: self.assertEqual(3,4)) # will fail but won't stop execution
expect(lambda: self.assertListEqual([4,5,6,2,5],[7,8])) # will fail but won't stop execution
assert_expectations()
@delayed_assert.assert_all()
def testDecorator(self):
expect('five' == 'Six', 'String do not match')
expect([5,2] == [3,4], 'List item do not match')
expect([3,4] == [3,4], 'This message wont be printed')
# No need to call delayed_assert.assert_expectations() when decorator is used
def testContextManeger(self):
with delayed_assert.assert_all():
expect('four' == 'Six', 'String do not match')
expect([5,2] == [3,4], 'List item do not match')
expect([3,4] == [3,4], 'This message wont be printed')
# No need to call delayed_assert.assert_expectations() when using context maneger is used
Color Output Control
The library supports toggling colorized output on/off via environment variable or programmatically.
Disable Colors via Environment Variable
# Disable colors
DELAYED_ASSERT_ENABLE_COLOR=0 python -m unittest example_unittest.py
# Enable colors (default)
DELAYED_ASSERT_ENABLE_COLOR=1 python -m unittest example_unittest.py
Values that disable colors: 0, false, no, off (case-insensitive)
Disable Colors Programmatically
from delayed_assert.delayed_assert import set_color_enabled, get_color_enabled
# Disable colors
set_color_enabled(False)
# Check status
if get_color_enabled():
print("Colors are enabled")
This is useful for:
- CI/CD environments where ANSI color codes may not be supported
- Log files where color codes create noise
- Environments with accessibility requirements
Caller Stack Verification
By default, the library verifies that expect() is called from a method named test*.
You can disable this check if you need to use expect() in helper methods or custom test runners.
Disable via Environment Variable:
DELAYED_ASSERT_CHECK_CALLER=0 python -m unittest example_unittest.py
Disable Programmatically:
from delayed_assert.delayed_assert import set_check_caller
set_check_caller(False)
Using the @test_case Decorator:
Alternatively, you can mark any function as a test case using the @test_case decorator. This allows expect() to identify the function correctly without disabling verificaton globally.
from delayed_assert import expect, assert_expectations, test_case
@test_case
def verify_custom_scenario():
expect(1 == 1, "Should pass")
def test_runner():
verify_custom_scenario()
assert_expectations()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file delayed_assert-0.4.2.tar.gz.
File metadata
- Download URL: delayed_assert-0.4.2.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ec7e3ab65ca674c241ebe66510104b318b3688a520d02a9da08a1abfd127d36
|
|
| MD5 |
4f44d6c7ea25c355fa8bcad5ba3233d2
|
|
| BLAKE2b-256 |
eeaaa8f07e0f7f6279106bd7e41ad1cf6fea48bb1f3e0e6dbf8ae77d842db1dc
|
Provenance
The following attestation bundles were made for delayed_assert-0.4.2.tar.gz:
Publisher:
publish.yml on pr4bh4sh/delayed-assert
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
delayed_assert-0.4.2.tar.gz -
Subject digest:
3ec7e3ab65ca674c241ebe66510104b318b3688a520d02a9da08a1abfd127d36 - Sigstore transparency entry: 748140850
- Sigstore integration time:
-
Permalink:
pr4bh4sh/delayed-assert@95346a2e8990f76b377a168e4e857e28da9d4244 -
Branch / Tag:
refs/tags/0.4.2 - Owner: https://github.com/pr4bh4sh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@95346a2e8990f76b377a168e4e857e28da9d4244 -
Trigger Event:
release
-
Statement type:
File details
Details for the file delayed_assert-0.4.2-py3-none-any.whl.
File metadata
- Download URL: delayed_assert-0.4.2-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4520f5d8ab9cdfe98de22568de93bf1e6f21e2a81efa8ec58dc0c1b48d424c8a
|
|
| MD5 |
7908a00a971aeecab79742f84e78524a
|
|
| BLAKE2b-256 |
d46b50fe1c03685f1ef67d0795fd57b37864e862f1a35e92f0b2fa0bf44703d9
|
Provenance
The following attestation bundles were made for delayed_assert-0.4.2-py3-none-any.whl:
Publisher:
publish.yml on pr4bh4sh/delayed-assert
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
delayed_assert-0.4.2-py3-none-any.whl -
Subject digest:
4520f5d8ab9cdfe98de22568de93bf1e6f21e2a81efa8ec58dc0c1b48d424c8a - Sigstore transparency entry: 748140851
- Sigstore integration time:
-
Permalink:
pr4bh4sh/delayed-assert@95346a2e8990f76b377a168e4e857e28da9d4244 -
Branch / Tag:
refs/tags/0.4.2 - Owner: https://github.com/pr4bh4sh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@95346a2e8990f76b377a168e4e857e28da9d4244 -
Trigger Event:
release
-
Statement type: