Getting started with bepatient is quick and easy. The library comes equipped with a range of default checkers, comparers, and a convenient Waiter object for your use.
In most cases, using one of the two methods will suffice:
wait_for_value_in_request: in case of checking 1 condition
wait_for_values_in_request: in case of multiple conditions
We can also use the RequestsWaiter
object. However, it’s important to remember that
the default methods only allow the use of built-in checkers and comparers.
wait_for_value_in_request
Waits for a specified value in response.
(PreparedRequest | Response)
: The request or response to monitor for the
expected value.(int, optional)
: The expected HTTP status code. Defaults to 200.(COMPARATORS | None, optional)
: The comparer function or operator used for
value comparison. Defaults to None.(Any, optional)
: The value to be compared against the response data.
Defaults to None.(CHECKERS, optional)
: The type of checker to use.(Session | None, optional)
: The requests session to use for sending
requests. Defaults to None.(str | None, optional)
: The dot-separated path to the value in the
response data. Defaults to None.(str | None, optional)
: A search query to use to find the value in the
response data. Defaults to None.(int, optional)
: The number of retries to perform. Defaults to 60.(int, optional)
: The delay between retries in seconds. Defaults to 1.Response
: the final response containing the expected value.WaiterConditionWasNotMet
: if the condition is not met within the specified number
of attempts.from requests import get
from bepatient import wait_for_value_in_request
response = wait_for_value_in_request(
request=get("https://example.com/api"),
comparer="contain",
expected_value="string"
)
assert response.status_code == 200
wait_for_values_in_request
Wait for multiple specified values in a response using different checkers.
(PreparedRequest | Response)
: The request or response to monitor for the
expected values.(list[dict[str, Any]])
: A list of dictionaries, where each dictionary
contains information about a checker to apply.
Each dictionary must have keys:
(CHECKERS)
: type of checker to use.(COMPARATORS)
: comparer function or operator used for value comparison.(Any)
: the value to be compared against the response data.(str | None, optional)
: The dot-separated path to the value in the
response data. Defaults to None.(str | None, optional)
: A search query to use to find the value in
the response data. Defaults to None.(int, optional)
: The expected HTTP status code. Defaults to 200.(Session | None, optional)
: The requests session to use for sending
requests. Defaults to None.(int, optional)
: The number of retries to perform. Defaults to 60.(int, optional)
: The delay between retries in seconds. Defaults to 1.Response
: the final response containing the expected value.WaiterConditionWasNotMet
: if the condition is not met within the specified number
of attempts.from requests import get
from bepatient import wait_for_value_in_request
response = wait_for_value_in_request(
request=get("https://example.com/api"),
comparer="contain",
expected_value="string"
)
assert response.status_code == 200
Both of the above methods use the RequestsWaiter
object.
The RequestsWaiter
class is a utility class designed to facilitate the setup and
monitoring of requests to ensure expected values are met.
When creating a RequestsWaiter object, one attribute is required:
(PreparedRequest | Response)
: The request or response to monitor for the
expected values.Additionally, the user can also specify:
(int, optional)
: The expected HTTP status code. Defaults to 200.(Session | None, optional)
: The requests session to use for sending
requests. Defaults to None.Add a response checker to the waiter.
(Any)
: The value to be compared against the response data.(COMPARATORS)
: The comparer function or operator used for value comparison.(CHECKERS, optional)
: The type of checker to use. Defaults to “json_checker”.(str | None, optional)
: The dot-separated path to the value in the
response data. Defaults to None.(str | None, optional)
: A search query to use to find the value in the
response data. Defaults to None.self
: updated RequestsWaiter
instance.Run the waiter and monitor the specified request or response.
(int, optional)
: The number of retries to perform. Defaults to 60
.(int, optional)
: The delay between retries in seconds. Defaults to 1
.self
: updated RequestsWaiter
instance.WaiterConditionWasNotMet
: if the condition is not met within the specified number
of attempts.Get the final response containing the expected values.
Response
: final response containing the expected values.from requests import get
from bepatient import RequestsWaiter
# Create a RequestsWaiter instance with a request and expected status code
waiter = RequestsWaiter(request=get("https://example.com/api"), status_code=200)
# Add a checker to monitor for an expected value
waiter.add_checker(
expected_value=0,
comparer="have_len_greater",
checker="json_checker",
dict_path="data"
)
# Run the waiter and monitor the response
response = waiter.run(retries=5, delay=2).get_result()
# Access the final response containing the expected values
print(response)
When working with bepatient, you’ll find that each checking object should inherit from
the Checker
class or one of its derived classes.
At the moment, bepatient exclusively supports response checkers:
checkers:
- json_checker
- headers_checker
Furthermore, it’s important to note that RequestsExecutor
requires the status_code
attribute. This is because, prior to evaluating other checkers, it employs the
StatusCodeChecker
, a subclass of ResponseChecker
.
For an extensive list of available checkers, refer to:
from bepatient import CHECKERS
print(CHECKERS)
Comparers serve as straightforward functions designed to compare two objects and return a boolean value. bepatient offers a comprehensive range of default comparers, all designed to suit your testing needs. They can be accessed here:
from bepatient import COMPARATORS
print(COMPARATORS)
The basic comparers include:
comparators:
- is_equal
- is_not_equal
- is_greater_than
- is_lesser_than
- is_greater_than_or_equal
- is_lesser_than_or_equal
- not_contain
- contain_all
- contain_any
- have_len_equal
- have_len_greater
- have_len_lesser