
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
A library for creating fluent, readable, and composable checks for your tests or application logic.
A Python library for creating fluent, readable, and composable checks for your tests or application logic.
Install the package using pip:
pip install fluent-checks
The core of the library is the Check
class. A Check
is a simple wrapper around a function that returns a boolean (a Condition
).
from fluent_checks import Check
# Create a check from a lambda
is_even = Check(lambda: 2 % 2 == 0)
# Evaluate the check
if is_even:
print("It's even!")
# Or more explicitly
assert bool(is_even) is True
Checks can be combined using logical operators to create more complex conditions.
a = Check(lambda: True)
b = Check(lambda: False)
assert bool(a & b) is False # And
assert bool(a | b) is True # Or
assert bool(~b) is True # Not
You can wait for a condition to become true.
import time
start_time = time.time()
flaky_check = Check(lambda: time.time() - start_time > 2)
# as_waiting will block until the check is true, or the timeout is reached.
assert flaky_check.as_waiting(timeout=3) is True
assert flaky_check.as_waiting(timeout=1) is False
You can enforce time limits on checks.
from datetime import datetime, timedelta
# This check will raise a TimeoutException if it takes longer than 1 second
slow_check = Check(lambda: time.sleep(2) or True)
failing_check = slow_check.with_timeout(1)
try:
bool(failing_check)
except TimeoutException:
print("Caught expected timeout!")
# You can also use a specific deadline
deadline = datetime.now() + timedelta(seconds=1)
check_with_deadline = slow_check.with_deadline(deadline)
You can verify that a condition holds true multiple times.
# succeeds_in_attempts: True if the check passes at least once in 5 attempts
flaky_check = Check(lambda: random.random() > 0.5)
assert flaky_check.succeeds_in_attempts(5)
# is_consistent_for: True if the check passes 5 times in a row
stable_check = Check(lambda: True)
assert stable_check.is_consistent_for(5)
The sometimes
and always
checks will run in a background thread until they are explicitly stopped.
# The 'sometimes' check will pass if the condition is met at least once.
check = Check(lambda: random.random() > 0.8).sometimes()
time.sleep(1) # Give it some time to run
assert bool(check) is True
check.stop()
# The 'always' check will only pass if the condition is met every single time.
check = Check(lambda: random.random() > 0.2).always()
time.sleep(1) # Give it some time to run
assert bool(check) is False
check.stop()
You can check that a piece of code raises a specific exception.
def might_fail():
raise ValueError("Something went wrong")
check = Check(might_fail).raises(ValueError)
assert bool(check) is True
You can add callbacks to your checks, that will be executed on success or on failure.
a = Check(lambda: True)
a.on_success(lambda: print("I've passed"))
a.on_failure(lambda: print("I've failed"))
bool(a) # This will print "I've passed"
You can check for files and directories.
# Check if a file exists
assert FileExistsCheck("path/to/file")
# Check if a directory exists
assert DirectoryExistsCheck("path/to/dir")
# Check if a file contains specific content
assert FileContainsCheck("path/to/file", "content")
You can compare values.
# Equality
assert IsEqualCheck(1, 1)
assert IsNotEqualCheck(1, 2)
# Comparison
assert IsGreaterThanCheck(2, 1)
assert IsLessThanCheck(1, 2)
# Containment
assert IsInCheck(1, [1, 2, 3])
# Instance
assert IsInstanceOfCheck(1, int)
Class/Method | Description |
---|---|
Check(condition: Callable[[], bool]) | The base class for all checks. |
& , | , ~ | Operators for AND, OR, and NOT logic. |
bool(check) , check.check() | Evaluates the check and returns the boolean result. |
Class/Method | Description |
---|---|
as_waiting(timeout: float) -> WaitingCheck | Blocks until the check is True or the timeout expires. |
with_timeout(timeout: float) -> TimeoutCheck | Returns a new check that will raise a TimeoutException if it doesn't complete within the timeout. |
with_deadline(deadline: datetime) -> DeadlineCheck | Similar to with_timeout but uses an absolute deadline. |
succeeds_within(timeout: float) -> SucceedsWithinCheck | Checks if the condition is met at least once within a time limit. eventually is an alias for this. |
fails_within(timeout: float) -> FailsWithinCheck | Checks if the condition fails at least once within a time limit. |
Class/Method | Description |
---|---|
succeeds_in_attempts(times: int) -> RepeatingOrCheck | Checks if the condition is met at least once within a number of tries. |
is_consistent_for(times: int) -> RepeatingAndCheck | Checks if the condition is met consecutively for a number of tries. |
sometimes() -> LoopingOrCheck | Runs the check in a background thread and succeeds if the condition is met at least once. Remember to call stop() . |
always() -> LoopingAndCheck | Runs the check in a background thread and succeeds only if the condition is always met. Remember to call stop() . |
Class/Method | Description |
---|---|
raises(exception: type[Exception]) -> RaisesCheck | Checks if the condition raises a specific exception. |
on_success(callback: Callable[[], None]) -> Check | Registers a callback to be executed when the check succeeds. |
on_failure(callback: Callable[[], None]) -> Check | Registers a callback to be executed when the check fails. |
Class/Method | Description |
---|---|
FileExistsCheck(path: str) | Checks if a file exists at the given path. |
DirectoryExistsCheck(path: str) | Checks if a directory exists at the given path. |
FileContainsCheck(path: str, content: str) | Checks if a file at the given path contains the given content. |
Class/Method | Description |
---|---|
IsEqualCheck(left, right) | Checks if left == right . |
IsNotEqualCheck(left, right) | Checks if left != right . |
IsGreaterThanCheck(left, right) | Checks if left > right . |
IsLessThanCheck(left, right) | Checks if left < right . |
IsInCheck(member, container) | Checks if member in container . |
IsInstanceOfCheck(obj, class_or_tuple) | Checks if isinstance(obj, class_or_tuple) . |
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
FAQs
A library for creating fluent, readable, and composable checks for your tests or application logic.
We found that fluent-checks demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.