Async & eventual assertions¶
Poll a callable until an assertion passes or the timeout expires. Start with eventually() on a
callable value, chain the assertion you expect to eventually hold, and await the result - or use
eventually_sync() for the same polling without an event loop. See
Testing for usage.
eventually ¶
eventually(
*,
timeout: float = 5.0,
interval: float = 0.5,
ignoring: type[Exception]
| tuple[type[Exception], ...] = (),
trace: bool = True,
) -> AsyncAssertionBuilder
Switch to async polling mode for eventual-consistency assertions.
The current val must be a callable (sync or async). Returns an
AsyncAssertionBuilder whose assertion
methods are coroutines that poll val() until the assertion passes or
timeout expires.
By default only a failing assertion is retried: any exception raised by val() itself
propagates immediately. A probe that signals "not ready yet" by raising (a connection refused
while a service boots, a record not yet visible) can be retried too by listing those exception
types in ignoring.
Polling itself is always strict - retrying requires hard failures - but the final timeout
failure honors the builder's mode: inside
soft_assertions() it is collected instead of raised,
and under assert_warn() it is logged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
maximum seconds to keep retrying (default |
5.0
|
interval
|
float
|
seconds between retries (default |
0.5
|
ignoring
|
type[Exception] | tuple[type[Exception], ...]
|
an |
()
|
trace
|
bool
|
record a |
True
|
Examples:
Usage:
import asyncio
from assertpy2 import assert_that
counter = {"n": 0}
def get_count():
counter["n"] += 1
return counter["n"]
asyncio.run(
assert_that(get_count).eventually(timeout=2).is_equal_to(3)
)
Retry a probe that raises while the system under test is not ready yet:
await assert_that(get_order).eventually(timeout=10, ignoring=ConnectionError).has_status("PAID")
# or configure fluently on the returned builder
await assert_that(get_order).eventually().within(10).ignoring(ConnectionError).has_status("PAID")
Returns:
| Name | Type | Description |
|---|---|---|
AsyncAssertionBuilder |
AsyncAssertionBuilder
|
an async builder whose assertion methods are awaitable |
Raises:
| Type | Description |
|---|---|
TypeError
|
if |
Source code in assertpy2/assertpy.py
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 | |
eventually_sync ¶
eventually_sync(
*,
timeout: float = 5.0,
interval: float = 0.5,
ignoring: type[Exception]
| tuple[type[Exception], ...] = (),
trace: bool = True,
) -> SyncAssertionBuilder
Switch to blocking polling mode for eventual-consistency assertions, without asyncio.
The synchronous sibling of eventually():
the current val must be a sync callable, and the returned
SyncAssertionBuilder exposes assertion methods
that block the calling thread (via time.sleep) while polling val() until the
assertion passes or timeout expires - no event loop and no await needed. A probe
that returns an awaitable raises TypeError; poll async probes with eventually().
Retry, failure-mode, and diagnostics semantics are identical to eventually(): only a
failing assertion (or an exception type listed in ignoring) is retried, the final
timeout failure honors the builder's soft/warn mode, and it carries the same
PollTrace flight recorder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
maximum seconds to keep retrying (default |
5.0
|
interval
|
float
|
seconds between retries (default |
0.5
|
ignoring
|
type[Exception] | tuple[type[Exception], ...]
|
an |
()
|
trace
|
bool
|
record a |
True
|
Examples:
Usage:
from assertpy2 import assert_that
counter = {"n": 0}
def get_count():
counter["n"] += 1
return counter["n"]
assert_that(get_count).eventually_sync(timeout=2, interval=0.1).is_equal_to(3)
Retry a probe that raises while the system under test is not ready yet:
assert_that(get_order).eventually_sync(timeout=10, ignoring=ConnectionError).has_status("PAID")
# or configure fluently on the returned builder
assert_that(get_order).eventually_sync().within(10).ignoring(ConnectionError).has_status("PAID")
Returns:
| Name | Type | Description |
|---|---|---|
SyncAssertionBuilder |
SyncAssertionBuilder
|
a blocking builder whose assertion methods poll on call |
Raises:
| Type | Description |
|---|---|
TypeError
|
if |
Source code in assertpy2/assertpy.py
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 | |
Async assertion builder that polls a callable until an assertion passes or timeout expires.
Do not instantiate directly; use eventually() instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
a sync or async callable that produces the value to test |
required |
builder_func
|
Callable
|
factory function to create assertion builders (receives |
required |
description
|
str
|
optional error description forwarded to the builder |
''
|
timeout
|
float
|
maximum seconds to keep retrying |
5.0
|
interval
|
float
|
seconds between retries |
0.5
|
ignoring
|
tuple[type[Exception], ...]
|
exception types the polling loop retries instead of propagating |
()
|
kind
|
str | None
|
the failure mode of the final timeout failure ( |
None
|
logger
|
object
|
the logger for |
None
|
trace
|
bool
|
record a |
True
|
Source code in assertpy2/async_assertions.py
within ¶
every ¶
ignoring ¶
Replace the exception types the polling loop retries instead of propagating.
Examples:
Usage:
await assert_that(get_order).eventually().within(10).ignoring(ConnectionError).has_status("PAID")
Raises:
| Type | Description |
|---|---|
TypeError
|
if any argument is not an |
Source code in assertpy2/async_assertions.py
Blocking assertion builder that polls a sync callable until an assertion passes or timeout expires.
Do not instantiate directly; use
eventually_sync() instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
a sync callable that produces the value to test (an async probe raises |
required |
builder_func
|
Callable
|
factory function to create assertion builders (receives |
required |
description
|
str
|
optional error description forwarded to the builder |
''
|
timeout
|
float
|
maximum seconds to keep retrying |
5.0
|
interval
|
float
|
seconds between retries |
0.5
|
ignoring
|
tuple[type[Exception], ...]
|
exception types the polling loop retries instead of propagating |
()
|
kind
|
str | None
|
the failure mode of the final timeout failure ( |
None
|
logger
|
object
|
the logger for |
None
|
trace
|
bool
|
record a |
True
|
Source code in assertpy2/async_assertions.py
val
property
¶
The value the last passing poll saw.
Declared on the class rather than left to __getattr__, which answers every other name with a
polling call: reading .val off a chain would otherwise poll once and hand back a function.
Before anything has passed there is no such value, and __getattr__ says so. A raise here
would not: Python falls back to __getattr__ whenever an attribute lookup ends in
AttributeError, property included, so the message would have been swallowed and answered with
a polling call all the same.
within ¶
every ¶
ignoring ¶
Replace the exception types the polling loop retries instead of propagating.
Examples:
Usage:
assert_that(get_order).eventually_sync().within(10).ignoring(ConnectionError).has_status("PAID")
Raises:
| Type | Description |
|---|---|
TypeError
|
if any argument is not an |