### Example Log File Output Source: https://github.com/okken/pytest-check/blob/main/README.md Illustrates the expected format of the 'session.log' file when using the `call_on_fail` with logging setup, showing timestamps and failure details. ```text $ cat session.log --- 2026-02-26 09:46:39.822 --- --------- Starting test run --------- --- 2026-02-26 09:46:39.831 --- FAILURE: check 1 == 2 test_log2.py:4 in test_one() -> check.equal(1, 2) --- 2026-02-26 09:46:39.832 --- FAILURE: check 5 == 6 test_log2.py:7 in test_two() -> check.equal(5, 6) ``` -------------------------------- ### Install pytest-check Source: https://context7.com/okken/pytest-check/llms.txt Install the pytest-check plugin using pip or conda. ```bash pip install pytest-check ``` ```bash conda install -c conda-forge pytest-check ``` -------------------------------- ### Configure Logging for Check Failures Source: https://github.com/okken/pytest-check/blob/main/README.md Set up a pytest fixture to configure logging and register a custom failure handler that logs all check failures to a file. This example demonstrates session-scoped autouse fixture for global logging setup. ```python import logging import pytest from pytest_check import check @pytest.fixture(scope='session', autouse=True) def setup_logging(): # logging config log = logging.getLogger(__name__) log.setLevel(logging.DEBUG) fh = logging.FileHandler('session.log') fh.setLevel(logging.DEBUG) fh.setFormatter(logging.Formatter('--- %(asctime)s.%(msecs)03d --- %(message)s', datefmt='%Y-%m-%d %H:%M:%S')) log.addHandler(fh) # log start of tests log.info("---------\nStarting test run\n---------") # have check failures log to file def log_failure(message): log.error(message) check.call_on_fail(log_failure) ``` -------------------------------- ### Using `check` Helper Functions Source: https://github.com/okken/pytest-check/blob/main/README.md Rewrite the previous example using `check`'s built-in helper functions for assertions. These functions do not require a `with check:` block and can be called directly. This approach can make tests more readable. ```python def test_httpx_get_with_helpers(): r = httpx.get('https://www.example.org/') assert r.status_code == 200 check.is_false(r.is_redirect) check.equal(r.encoding, 'utf-8') check.is_in('Example Domain', r.text) ``` -------------------------------- ### Basic Usage with `check` Context Manager Source: https://github.com/okken/pytest-check/blob/main/README.md Use the `check` context manager to allow multiple assertions within a test function to be evaluated even if earlier ones fail. This example demonstrates checking HTTP response properties after an initial status code assertion. ```python import httpx from pytest_check import check def test_httpx_get(): r = httpx.get('https://www.example.org/') # bail if bad status code assert r.status_code == 200 # but if we get to here # then check everything else without stopping with check: assert r.is_redirect is False with check: assert r.encoding == 'utf-8' with check: assert 'Example Domain' in r.text ``` -------------------------------- ### Use xfail with Check Context Managers Source: https://context7.com/okken/pytest-check/llms.txt When using the `with check:` context manager, you can combine it with `@pytest.mark.xfail`. This setup ensures that check failures within the context manager are treated as xfailed if they match the `raises` argument of the xfail marker. ```python @pytest.mark.xfail(raises=AssertionError) def test_xfail_with_context_manager(): "Marked xfail; check failures report AssertionError which matches raises=" with check: assert 1 == 2 # logged as xfailed with check: assert 2 == 3 # also xfailed ``` -------------------------------- ### Test with Multiple Failures Source: https://github.com/okken/pytest-check/blob/main/README.md Example test function demonstrating multiple check failures within a single test case. This showcases the core functionality of pytest-check. ```python def test_example(): a = 1 b = 2 c = [2, 4, 6] check.greater(a, b) check.less_equal(b, a) check.is_in(a, c, "Is 1 in the list") check.is_not_in(b, c, "make sure 2 isn't in list") ``` -------------------------------- ### Configure Max Traceback Lines Source: https://github.com/okken/pytest-check/blob/main/README.md Shows how to combine `--check-max-tb` and `--check-max-tb-line` to control the verbosity of failure tracebacks. This allows for different levels of detail for early vs. later failures. ```bash $ pytest test_check.py --check-max-tb=1 --check-max-tb-line=4 ``` -------------------------------- ### Configure Max Pseudo-Tracebacks Source: https://github.com/okken/pytest-check/blob/main/README.md Demonstrates how to configure the maximum number of pseudo-tracebacks displayed using the `--check-max-tb` pytest option. This allows for more detailed failure output when needed. ```bash (.venv) $ pytest test_check.py --check-max-tb=5 ``` -------------------------------- ### Use `check.not_equal()`, `check.is_()`, `check.is_not()` Source: https://context7.com/okken/pytest-check/llms.txt Perform non-blocking inequality and identity checks. These functions provide soft versions of `!=`, `is`, and `is not` comparisons. ```python from pytest_check import check SENTINEL = object() def test_identity_checks(): a = [1, 2, 3] b = a # same object c = [1, 2, 3] # equal but not same object check.is_(a, b, "a and b should be the same object") check.is_not(a, c, "a and c should NOT be the same object") check.not_equal(a, [1, 2, 4], "lists should differ") check.is_not(SENTINEL, None) ``` -------------------------------- ### Control Failure Reporting with CLI Options Source: https://context7.com/okken/pytest-check/llms.txt Use these CLI options to control the verbosity and performance of failure reporting. Adjust traceback depth, line format, and the number of failures reported or that cause a test to abort. ```bash pytest --check-max-tb=5 ``` ```bash pytest --check-max-tb=1 --check-max-tb-line=10 ``` ```bash pytest --check-max-report=10 ``` ```bash pytest --check-max-fail=20 ``` ```bash pytest --check-max-tb=1 --check-max-report=10 ``` ```bash pytest --check-max-tb=0 ``` ```bash pytest -x ``` ```bash pytest --tb=no ``` ```bash pytest --color=no ``` ```bash pytest -l ``` -------------------------------- ### Set Local Max Tracebacks Source: https://github.com/okken/pytest-check/blob/main/README.md Configure the maximum number of pseudo-tracebacks to display per test locally. Useful for controlling output verbosity when many failures occur. ```python def test_max_tb(): check.set_max_tb(2) for i in range(1, 11): check.equal(i, 100) ``` -------------------------------- ### Using `check` as a Fixture Source: https://github.com/okken/pytest-check/blob/main/README.md Alternatively, inject `check` as a fixture into your test function to use its context manager without needing an explicit import. This is a matter of personal preference. ```python def test_httpx_get(check): r = httpx.get('https://www.example.org/') ... with check: assert r.is_redirect == False ... ``` -------------------------------- ### Conditional Checks with any_failures() Source: https://github.com/okken/pytest-check/blob/main/README.md Illustrates using `check.any_failures()` to conditionally execute blocks of checks. This is useful for creating dependent check groups where later checks should only run if previous ones passed. ```python from pytest_check import check def test_with_groups_of_checks(): # always check these check.equal(1, 1) check.equal(2, 3) if not check.any_failures(): # only check these if the above passed check.equal(1, 2) check.equal(2, 2) ``` -------------------------------- ### Comparison Checks: check.greater(a, b) / check.greater_equal(a, b) / check.less(a, b) / check.less_equal(a, b) Source: https://context7.com/okken/pytest-check/llms.txt Non-blocking comparison checks for greater than, greater than or equal to, less than, and less than or equal to. These are useful for verifying numerical ranges or ordering. ```APIDOC ## `check.greater(a, b)` / `check.greater_equal(a, b)` / `check.less(a, b)` / `check.less_equal(a, b)` — Comparison Checks Non-blocking `>`, `>=`, `<`, `<=` comparisons. ### Usage Example ```python from pytest_check import check check.greater(value, threshold, "Value must be greater than threshold") check.less_equal(measurement, max_value, "Measurement must be less than or equal to max") ``` ``` -------------------------------- ### check.set_max_tb, check.set_max_report, check.set_max_fail Source: https://context7.com/okken/pytest-check/llms.txt Allows overriding global CLI speedup flags for a single test function. These settings reset automatically after each test. ```APIDOC ## `check.set_max_tb(n)` / `check.set_max_report(n)` / `check.set_max_fail(n)` — Per-test Limits Override the global CLI speedup flags for a single test function. Resets automatically after each test. ```python from pytest_check import check def test_large_dataset_validation(dataset): # Only show pseudo-tracebacks for first 2 failures check.set_max_tb(2) # Stop reporting after 10 failures check.set_max_report(10) # Abort the test after 50 check failures check.set_max_fail(50) for i, record in enumerate(dataset): check.is_not_none(record.get("id"), f"record[{i}].id must not be None") check.is_instance(record.get("value"), (int, float), f"record[{i}].value must be numeric") check.greater_equal(record.get("value", -1), 0, f"record[{i}].value must be >= 0") ``` ``` -------------------------------- ### None Checks: check.is_none(x) / check.is_not_none(x) Source: https://context7.com/okken/pytest-check/llms.txt Verify if a value `x` is None or not None. These are commonly used for checking optional fields or ensuring that certain values are always present. ```APIDOC ## `check.is_none(x)` / `check.is_not_none(x)` — None Checks Checks if `x` is `None` or not `None`. ### Usage Example ```python from pytest_check import check check.is_not_none(value, "Value must not be None") check.is_none(optional_value, "Optional value should be None") ``` ``` -------------------------------- ### Conditional Check Gating with check.any_failures Source: https://context7.com/okken/pytest-check/llms.txt Use `check.any_failures()` to return `True` if any checks have failed so far. This allows skipping dependent checks when prerequisite checks have already failed. ```python from pytest_check import check def test_database_record(record): # Stage 1: validate the record exists and has the right shape check.is_not_none(record, "record must not be None") check.is_instance(record, dict, "record must be a dict") check.is_in("id", record) check.is_in("data", record) # Stage 2: only validate contents if Stage 1 passed if not check.any_failures(): check.is_instance(record["id"], int, "id must be int") check.greater(record["id"], 0, "id must be positive") check.is_instance(record["data"], dict, "data must be a dict") check.is_in("payload", record["data"]) ``` -------------------------------- ### Use `check.equal()` for Equality Checks Source: https://context7.com/okken/pytest-check/llms.txt Perform non-blocking equality checks using `check.equal(a, b, msg='')`. It returns `True` on pass and `False` on failure, logging the failure. The return value can control subsequent checks. ```python from pytest_check import check def test_calculations(): result = {"sum": 10, "product": 24, "diff": 2} check.equal(result["sum"], 10, "sum should be 10") check.equal(result["product"], 24, "product should be 24") check.equal(result["diff"], 3, "diff should be 3") # fails # Return value can gate further checks if check.equal(len(result), 3, "should have 3 keys"): check.is_in("sum", result) check.is_in("product", result) ``` ```text # Output: # FAILURE: check 2 == 3: diff should be 3 # test_calc.py:6 in test_calculations() -> check.equal(result["diff"], 3, ...) ``` -------------------------------- ### Comparison Checks: check.greater / check.less Source: https://context7.com/okken/pytest-check/llms.txt Performs non-blocking greater than, less than, greater than or equal to, and less than or equal to comparisons. Logs failures instead of raising exceptions. ```python from pytest_check import check def test_performance_metrics(metrics): check.greater(metrics.throughput, 1000, "throughput must exceed 1000 req/s") check.less(metrics.latency_p99_ms, 200, "P99 latency must be under 200ms") check.less_equal(metrics.error_rate, 0.01, "error rate must be <= 1%") check.greater_equal(metrics.uptime_pct, 99.9, "uptime must be >= 99.9%") ``` -------------------------------- ### Per-test Limits with check.set_max_tb, check.set_max_report, check.set_max_fail Source: https://context7.com/okken/pytest-check/llms.txt Override global CLI speedup flags for a single test function using `check.set_max_tb(n)`, `check.set_max_report(n)`, and `check.set_max_fail(n)`. These settings reset automatically after each test. ```python from pytest_check import check def test_large_dataset_validation(dataset): # Only show pseudo-tracebacks for first 2 failures check.set_max_tb(2) # Stop reporting after 10 failures check.set_max_report(10) # Abort the test after 50 check failures check.set_max_fail(50) for i, record in enumerate(dataset): check.is_not_none(record.get("id"), f"record[{i}].id must not be None") check.is_instance(record.get("value"), (int, float), f"record[{i}].value must be numeric") check.greater_equal(record.get("value", -1), 0, f"record[{i}].value must be >= 0") ``` -------------------------------- ### Mark Individual Checks as Known Failures (xfail) Source: https://context7.com/okken/pytest-check/llms.txt Use the `xfail` keyword argument in check functions to mark specific assertions as known issues. If such a check fails, the entire test is reported as xfailed. Unrelated checks will still run and be reported normally. ```python import pytest from pytest_check import check def test_known_issues(api_response): # This check failing will mark the whole test as xfailed check.equal(api_response["status"], "ok", xfail="bug #42: status field broken") check.is_in("result", api_response, xfail="bug #43: result key missing") # Fully unrelated checks still run and pass independently check.is_instance(api_response, dict) ``` -------------------------------- ### Custom Failure Message with raises Source: https://github.com/okken/pytest-check/blob/main/README.md Provide a custom failure message to `check.raises` using the `msg` argument. This message will be reported if the expected exception is not raised, independent of the actual exception's string representation. ```python def test_raises_and_custom_fail_message(): with check.raises(ValueError, msg="custom"): x = 1 / 0 # division by zero error, NOT ValueError assert x == 0 ``` -------------------------------- ### Failure Callback with check.call_on_fail Source: https://context7.com/okken/pytest-check/llms.txt Register a callback function using `check.call_on_fail(func)` that is invoked with the failure message string on every check failure. This is useful for logging failures to external systems. ```python import logging import pytest from pytest_check import check # conftest.py @pytest.fixture(scope="session", autouse=True) def setup_failure_logging(): log = logging.getLogger("check_failures") log.setLevel(logging.ERROR) fh = logging.FileHandler("failures.log") fh.setFormatter(logging.Formatter("%(asctime)s %(message)s")) log.addHandler(fh) def log_failure(message: str) -> None: log.error(message) check.call_on_fail(log_failure) ``` -------------------------------- ### check.raises Source: https://context7.com/okken/pytest-check/llms.txt Used as a context manager to verify an exception is raised without stopping the test if it isn't. Supports inspecting the exception value and marking expected failures. ```APIDOC ## `check.raises(expected_exception, msg=None, xfail=None)` — Non-blocking Exception Check Used as a context manager (like `pytest.raises`) to verify an exception is raised, without stopping the test if it isn't. The exception value is accessible via `.value`. Supports `xfail` for known bugs. ```python from pytest_check import check def test_error_handling(): # Basic usage: check that ValueError is raised with check.raises(ValueError): int("not-a-number") # Inspect the exception value with check.raises(TypeError) as exc_info: result = "hello" + 42 # exc_info.value holds the exception after the block check.is_not_none(exc_info.value) # Custom failure message with check.raises(KeyError, msg="missing key should raise KeyError"): d = {} _ = d["nonexistent"] # xfail: known bug — logs as xfailed instead of failed with check.raises(ValueError, xfail="known issue #123"): pass # no exception raised — would normally fail, logged as xfail ``` ``` -------------------------------- ### Truthiness Checks: check.is_true(x) / check.is_false(x) Source: https://context7.com/okken/pytest-check/llms.txt These methods check if the boolean evaluation of `x` is True or False, respectively. They are useful for verifying conditions like feature flags or boolean states. ```APIDOC ## `check.is_true(x)` / `check.is_false(x)` — Truthiness Checks Checks `bool(x) is True` or `bool(x) is False`. ### Usage Example ```python from pytest_check import check check.is_true(some_condition, "Condition should be true") check.is_false(another_condition, "Condition should be false") ``` ``` -------------------------------- ### Using `xfail` Reason with Check Functions Source: https://github.com/okken/pytest-check/blob/main/README.md Pass an `xfail` argument to `check` helper functions to mark a failing check as expected to fail (xfailed). This does not xfail the entire test unless it's already marked with `@pytest.mark.xfail`. ```python check.equal(actual, expected, xfail="known issue #123") ``` -------------------------------- ### Non-blocking Exception Check with check.raises Source: https://context7.com/okken/pytest-check/llms.txt Use `check.raises` as a context manager to verify exceptions without stopping the test. The exception value is accessible via `.value`. Supports `xfail` for known bugs. ```python from pytest_check import check def test_error_handling(): # Basic usage: check that ValueError is raised with check.raises(ValueError): int("not-a-number") # Inspect the exception value with check.raises(TypeError) as exc_info: result = "hello" + 42 # exc_info.value holds the exception after the block check.is_not_none(exc_info.value) # Custom failure message with check.raises(KeyError, msg="missing key should raise KeyError"): d = {} _ = d["nonexistent"] # xfail: known bug — logs as xfailed instead of failed with check.raises(ValueError, xfail="known issue #123"): pass # no exception raised — would normally fail, logged as xfail ``` -------------------------------- ### Type Checks: check.is_instance(a, b) / check.is_not_instance(a, b) Source: https://context7.com/okken/pytest-check/llms.txt Verify if an object `a` is an instance of a specific class `b`, or not. Useful for validating data types returned by functions or parsed from external sources. ```APIDOC ## `check.is_instance(a, b)` / `check.is_not_instance(a, b)` — Type Checks Checks `isinstance(a, b)` or `not isinstance(a, b)`. ### Usage Example ```python from pytest_check import check check.is_instance(data, expected_type, "Data must be of expected type") check.is_not_instance(value, wrong_type, "Value should not be of wrong type") ``` ``` -------------------------------- ### None Checks: check.is_none / check.is_not_none Source: https://context7.com/okken/pytest-check/llms.txt Use to verify if a value is None or not None. Ideal for checking optional fields or ensuring required fields are present. ```python from pytest_check import check def test_optional_fields(record): check.is_not_none(record.id, "id must always be set") check.is_not_none(record.created_at, "created_at must always be set") check.is_none(record.deleted_at, "deleted_at should be None for active records") ``` -------------------------------- ### Truthiness Checks: check.is_true / check.is_false Source: https://context7.com/okken/pytest-check/llms.txt Use for checking if a value is True or False. Useful for feature flags or boolean configurations. ```python from pytest_check import check def test_feature_flags(config): check.is_true(config.feature_enabled, "feature flag should be on") check.is_false(config.debug_mode, "debug mode should be off in prod") check.is_true(config.retries > 0, "retries must be configured") ``` -------------------------------- ### Use `check(msg)` with Custom Messages Source: https://context7.com/okken/pytest-check/llms.txt Provide a custom failure message by calling `check` as a function before the `with check(msg):` block. The message is appended to the failure output. ```python from pytest_check import check def test_user_profile(user): with check("username must be non-empty"): assert user.username != "" with check("email must contain @"): assert "@" in user.email with check("age must be positive"): assert user.age > 0 ``` ```text # Failure output: # FAILURE: AssertionError, age must be positive # test_profile.py:7 in test_user_profile() -> assert user.age > 0 ``` -------------------------------- ### Conditional Logic with Check Return Values Source: https://github.com/okken/pytest-check/blob/main/README.md Check functions return a boolean indicating success or failure. Use this return value to perform actions conditionally based on the check's outcome. ```python from pytest_check import check def test_something() ... if check.equal(a, b): # they are equal ... else # they are not equal # and a failure was registered by the check method ... ``` -------------------------------- ### Type Checks: check.is_instance / check.is_not_instance Source: https://context7.com/okken/pytest-check/llms.txt Verifies if a variable is an instance of a specific type or not. Useful for ensuring correct data types in parsed outputs. ```python from pytest_check import check def test_return_types(parser_output): check.is_instance(parser_output.items, list, "items must be a list") check.is_instance(parser_output.count, int, "count must be an int") check.is_instance(parser_output.metadata, dict, "metadata must be a dict") check.is_not_instance(parser_output.count, float, "count should not be float") ``` -------------------------------- ### Approximate Equality: check.almost_equal(a, b, rel=None, abs=None) / check.not_almost_equal(...) Source: https://context7.com/okken/pytest-check/llms.txt Perform approximate equality checks using `pytest.approx` semantics. Allows specifying relative (`rel`) and absolute (`abs`) tolerances for floating-point comparisons. ```APIDOC ## `check.almost_equal(a, b, rel=None, abs=None)` / `check.not_almost_equal(...)` — Approximate Equality Uses `pytest.approx` internally. Accepts `rel` (relative tolerance) and `abs` (absolute tolerance). ### Usage Example ```python from pytest_check import check check.almost_equal(actual, expected, abs=0.01, msg="Values should be close") check.not_almost_equal(value, 0.0, abs=1e-6, msg="Value should not be close to zero") ``` ``` -------------------------------- ### Register Custom Failure Handler Source: https://github.com/okken/pytest-check/blob/main/README.md Register a function to be called for every check failure. The function receives the failure message as an argument, enabling custom actions like logging. ```python from pytest_check import check def my_func(msg): ... check.call_on_fail(my_func) ... ``` -------------------------------- ### Approximate Equality: check.almost_equal / check.not_almost_equal Source: https://context7.com/okken/pytest-check/llms.txt Compares floating-point numbers within a specified relative or absolute tolerance. Uses pytest.approx internally. ```python from pytest_check import check def test_floating_point_results(model_output): check.almost_equal(model_output.loss, 0.342, abs=1e-3, msg="loss within tolerance") check.almost_equal(model_output.accuracy, 0.95, rel=0.01, msg="accuracy within 1%") check.not_almost_equal(model_output.loss, 0.0, abs=1e-6, msg="loss should not be zero") ``` -------------------------------- ### Using raises as a Context Manager Source: https://github.com/okken/pytest-check/blob/main/README.md The `check.raises` context manager asserts that a specific exception is raised within its block. Unlike `pytest.raises`, a failure to raise the expected exception will not stop the test execution. ```python from pytest_check import check def test_raises(): with check.raises(AssertionError): x = 3 assert 1 < x < 4 ``` ```python from pytest_check import check def test_raises_exception_value(): with check.raises(ValueError) as e: raise ValueError("This is a ValueError") check.equal(str(e.value) == "This is a ValueError") ``` -------------------------------- ### Range Checks: check.between(b, a, c, ge=False, le=False) / check.between_equal(b, a, c) Source: https://context7.com/okken/pytest-check/llms.txt Check if a value `b` falls within a specified range defined by `a` and `c`. Supports strict (`a < b < c`) and inclusive (`a <= b <= c`) bounds. ```APIDOC ## `check.between(b, a, c, ge=False, le=False)` / `check.between_equal(b, a, c)` — Range Checks Checks that `b` falls strictly between `a` and `c` (`a < b < c`). Use `ge=True`/`le=True` for inclusive bounds, or use `between_equal` as a shortcut for `a <= b <= c`. ### Usage Example ```python from pytest_check import check check.between(value, min_val, max_val, msg="Value must be between min and max") check.between_equal(score, min_score, max_score, msg="Score must be within inclusive range") ``` ``` -------------------------------- ### Set Local Max Reported Failures Source: https://github.com/okken/pytest-check/blob/main/README.md Limit the number of failures reported per test locally. This helps manage output when numerous checks fail within a single test function. ```python def test_max_report(): check.set_max_report(5) for i in range(1, 11): check.equal(i, 100) ``` -------------------------------- ### check.fail Source: https://context7.com/okken/pytest-check/llms.txt Directly logs a failure with a message. Useful for building custom check helper functions without using the `@check_func` decorator. ```APIDOC ## `check.fail(msg, xfail=None)` — Explicit Failure Logging Directly log a failure with a message. Useful for building custom check helper functions without using the `@check_func` decorator. ```python from pytest_check import check def assert_positive(value, name="value"): """Custom non-blocking check helper.""" __tracebackhide__ = True if value > 0: return True check.fail(f"{name} must be positive, got {value}") return False def test_all_positive(measurements): for key, val in measurements.items(): assert_positive(val, name=key) # Output: # FAILURE: temperature must be positive, got -3 # FAILURE: pressure must be positive, got -1 ``` ``` -------------------------------- ### Use `with check:` for Soft Assertions Source: https://context7.com/okken/pytest-check/llms.txt Wrap standard assert statements within a `with check:` block to collect failures without stopping the test. This can be imported directly or used as a pytest fixture. ```python import httpx from pytest_check import check def test_api_response(): r = httpx.get("https://www.example.org/") # Hard assert: bail early if status is wrong assert r.status_code == 200 # Soft checks: collect all failures, don't stop with check: assert r.is_redirect is False with check: assert r.encoding == "utf-8" with check: assert "Example Domain" in r.text with check: assert r.headers["content-type"].startswith("text/html") ``` ```python # Equivalent using the check fixture (no import needed) def test_api_response_fixture(check): r = httpx.get("https://www.example.org/") assert r.status_code == 200 with check: assert r.encoding == "utf-8" with check: assert "Example Domain" in r.text ``` ```text # Output when checks fail: # FAILURE: assert False # test_api.py:10 in test_api_response() -> assert r.is_redirect is False # # FAILURE: AssertionError # FAILURE: AssertionError # ------------------------------------------------------------ # Failed Checks: 3 ``` -------------------------------- ### Range Checks: check.between / check.between_equal Source: https://context7.com/okken/pytest-check/llms.txt Checks if a value falls within a specified range, either strictly or inclusively. Useful for validating sensor readings or version numbers. ```python from pytest_check import check def test_value_ranges(sensor_readings): # Strict: 0 < reading < 100 check.between(sensor_readings.temperature, 0, 100, msg="temp must be 0–100") # Inclusive lower bound: 1 <= version <= 3 check.between(sensor_readings.version, 1, 3, ge=True, le=True) # Shortcut for fully inclusive range check.between_equal(sensor_readings.humidity, 20.0, 80.0, msg="humidity 20–80%") ``` -------------------------------- ### check.any_failures Source: https://context7.com/okken/pytest-check/llms.txt Returns True if any checks have failed so far in the current test. Useful for skipping dependent checks when prerequisites have already failed. ```APIDOC ## `check.any_failures()` — Conditional Check Gating Returns `True` if any checks have failed so far in the current test. Use it to skip a dependent block of checks when prerequisite checks have already failed. ```python from pytest_check import check def test_database_record(record): # Stage 1: validate the record exists and has the right shape check.is_not_none(record, "record must not be None") check.is_instance(record, dict, "record must be a dict") check.is_in("id", record) check.is_in("data", record) # Stage 2: only validate contents if Stage 1 passed if not check.any_failures(): check.is_instance(record["id"], int, "id must be int") check.greater(record["id"], 0, "id must be positive") check.is_instance(record["data"], dict, "data must be a dict") check.is_in("payload", record["data"]) ``` ``` -------------------------------- ### Explicit Failure Logging with check.fail Source: https://context7.com/okken/pytest-check/llms.txt Directly log a failure with a message using `check.fail`. Useful for building custom check helper functions without using the `@check_func` decorator. The function returns `True` on pass and `False` on failure. ```python from pytest_check import check def assert_positive(value, name="value"): """Custom non-blocking check helper.""" __tracebackhide__ = True if value > 0: return True check.fail(f"{name} must be positive, got {value}") return False def test_all_positive(measurements): for key, val in measurements.items(): assert_positive(val, name=key) # Output: # FAILURE: temperature must be positive, got -3 # FAILURE: pressure must be positive, got -1 ``` -------------------------------- ### check.call_on_fail Source: https://context7.com/okken/pytest-check/llms.txt Registers a callback function that is invoked with the failure message string on every check failure. Useful for logging failures to external systems. ```APIDOC ## `check.call_on_fail(func)` — Failure Callback Registers a callback function that is called with the failure message string on every check failure. Useful for logging failures to a file or external system. ```python import logging import pytest from pytest_check import check # conftest.py @pytest.fixture(scope="session", autouse=True) def setup_failure_logging(): log = logging.getLogger("check_failures") log.setLevel(logging.ERROR) fh = logging.FileHandler("failures.log") fh.setFormatter(logging.Formatter("%(asctime)s %(message)s")) log.addHandler(fh) def log_failure(message: str) -> None: log.error(message) check.call_on_fail(log_failure) ``` ``` -------------------------------- ### Defining Custom Check Functions with `@check.check_func` Source: https://github.com/okken/pytest-check/blob/main/README.md Decorate any function containing an `assert` statement with `@check.check_func` to transform it into a non-blocking check. This allows you to create reusable, custom assertion logic that integrates seamlessly with `pytest-check`. ```python from pytest_check import check @check.check_func def is_four(a): assert a == 4 def test_all_four(): is_four(1) is_four(2) is_four(3) is_four(4) ``` -------------------------------- ### Set Local Max Failures to Stop Source: https://github.com/okken/pytest-check/blob/main/README.md Define the maximum number of check failures after which a test will stop executing. Useful for preventing excessive test runs on slow code when many failures are expected. ```python def test_max_fail(): check.set_max_fail(5) for i in range(1, 11): check.equal(i, 100) ``` -------------------------------- ### Membership Checks: check.is_in / check.is_not_in Source: https://context7.com/okken/pytest-check/llms.txt Checks if an item is present in a collection (list, string, dict, set) or not. Useful for validating response structures or data integrity. ```python from pytest_check import check def test_response_structure(response_json): required_keys = ["id", "name", "email", "created_at"] for key in required_keys: check.is_in(key, response_json, f"'{key}' must be present in response") check.is_not_in("password", response_json, "password must not be exposed") check.is_not_in("secret_token", response_json, "secret_token must not be exposed") ``` -------------------------------- ### @check.check_func Source: https://context7.com/okken/pytest-check/llms.txt Decorator that wraps functions containing assert statements, logging failures non-blockingly instead of raising them. The decorated function returns True on pass and False on failure. ```APIDOC ## `@check.check_func` — Decorator for Custom Check Functions Wraps any function containing `assert` statements so that failures are logged non-blocking instead of raising. The decorated function returns `True` on pass and `False` on failure. ```python from pytest_check import check @check.check_func def is_valid_email(email: str): assert "@" in email, f"'{email}' missing @" assert "." in email.split("@")[-1], f"'{email}' missing domain dot" @check.check_func def is_valid_user(user: dict): assert "id" in user assert isinstance(user["id"], int) assert "name" in user assert len(user["name"]) > 0 def test_bulk_users(users): for user in users: is_valid_user(user) is_valid_email(user.get("email", "")) # All failures across all users are collected and reported together. ``` ``` -------------------------------- ### Membership Checks: check.is_in(a, b) / check.is_not_in(a, b) Source: https://context7.com/okken/pytest-check/llms.txt Assert that element `a` is present in or absent from collection `b`. This works with various collection types like lists, strings, dictionaries, and sets. ```APIDOC ## `check.is_in(a, b)` / `check.is_not_in(a, b)` — Membership Checks Checks `a in b` or `a not in b`. Works with lists, strings, dicts, sets, etc. ### Usage Example ```python from pytest_check import check check.is_in(item, collection, "Item must be in collection") check.is_not_in(sensitive_data, response, "Sensitive data must not be in response") ``` ``` -------------------------------- ### Handling Incorrect Exception Types with raises Source: https://github.com/okken/pytest-check/blob/main/README.md When the exception raised within the `check.raises` block is not of the expected type, the test failure message will detail the actual exception encountered. ```python def test_raises_fail(): with check.raises(ValueError): x = 1 / 0 # division by zero error, NOT ValueError assert x == 0 ``` -------------------------------- ### Decorator for Custom Check Functions with @check.check_func Source: https://context7.com/okken/pytest-check/llms.txt The `@check.check_func` decorator wraps functions containing `assert` statements, logging failures non-blockingly instead of raising. The decorated function returns `True` on pass and `False` on failure. ```python from pytest_check import check @check.check_func def is_valid_email(email: str): assert "@" in email, f"'{email}' missing @" assert "." in email.split("@")[-1], f"'{email}' missing domain dot" @check.check_func def is_valid_user(user: dict): assert "id" in user assert isinstance(user["id"], int) assert "name" in user assert len(user["name"]) > 0 def test_bulk_users(users): for user in users: is_valid_user(user) is_valid_email(user.get("email", "")) # All failures across all users are collected and reported together. ``` -------------------------------- ### NaN Checks: check.is_nan(x) / check.is_not_nan(x) Source: https://context7.com/okken/pytest-check/llms.txt Check if a value `x` is Not a Number (NaN) or not NaN. Useful for validating results of numerical computations. ```APIDOC ## `check.is_nan(x)` / `check.is_not_nan(x)` — NaN Checks Checks `math.isnan(x)` or `not math.isnan(x)`. ### Usage Example ```python from pytest_check import check check.is_not_nan(result, "Result must not be NaN") check.is_nan(error_value, "Error value should be NaN") ``` ``` -------------------------------- ### XFAIL Reason with raises Source: https://github.com/okken/pytest-check/blob/main/README.md The `xfail` argument in `check.raises` allows specifying a reason for an expected failure. If the `raises` check fails, the test will be marked as xfailed. This does not cause an xpass unless the test is already marked with `@pytest.mark.xfail`. ```python def test_raises_and_xfail(): with check.raises(ValueError, xfail="known issue #123"): x = 1 / 0 # division by zero error, NOT ValueError assert x == 0 ``` -------------------------------- ### Efficient Failure Assertion with check.fail() Source: https://github.com/okken/pytest-check/blob/main/README.md For performance-critical loops, use `check.fail()` to register failures. This avoids the overhead of `@check.check_func` in passing cases. The function should return `False` after calling `check.fail()`. ```python from pytest_check import check def is_four(a): __tracebackhide__ = True if a == 4: return True else: check.fail(f"check {a} == 4") return False def test_all_four(): is_four(1) is_four(2) is_four(3) is_four(4) ``` -------------------------------- ### NaN Checks: check.is_nan / check.is_not_nan Source: https://context7.com/okken/pytest-check/llms.txt Checks if a value is Not a Number (NaN) or not NaN. Essential for numerical stability checks in scientific or data processing applications. ```python from pytest_check import check def test_numerical_stability(results): for i, val in enumerate(results.outputs): check.is_not_nan(val, f"output[{i}] must not be NaN") check.is_not_nan(results.final_score, "final score must not be NaN") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.