bepatient

Usage

Quickstart

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.

Basic methods

wait_for_value_in_request

Waits for a specified value in response.

Args:
Returns:
Raises:
Example:
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.

Args:
Returns:
Raises:
Example:
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.

RequestsWaiter

The RequestsWaiter class is a utility class designed to facilitate the setup and monitoring of requests to ensure expected values are met.

Args

When creating a RequestsWaiter object, one attribute is required:

Additionally, the user can also specify:

Methods

add_checker

Add a response checker to the waiter.

Args:
Returns:
run

Run the waiter and monitor the specified request or response.

Args:
Returns:
Raises:
get_result

Get the final response containing the expected values.

Returns:

Example

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)

Default checkers

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)

Default comparers

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