Skip to content

REST

qa_pytest_rest

__all__ = ['HttpMethod', 'RestConfiguration', 'RestSteps', 'RestTests'] module-attribute

HttpMethod

Bases: str, Enum

Enum representing HTTP methods for REST requests.

Source code in qa-pytest-rest/src/qa_pytest_rest/rest_steps.py
17
18
19
20
21
22
23
24
25
class HttpMethod(str, Enum):
    """
    Enum representing HTTP methods for REST requests.
    """
    GET = "GET"
    POST = "POST"
    PUT = "PUT"
    DELETE = "DELETE"
    PATCH = "PATCH"

DELETE = 'DELETE' class-attribute instance-attribute

GET = 'GET' class-attribute instance-attribute

PATCH = 'PATCH' class-attribute instance-attribute

POST = 'POST' class-attribute instance-attribute

PUT = 'PUT' class-attribute instance-attribute

RestConfiguration

Bases: BaseConfiguration

Configuration class for REST API endpoints.

Inherits from

BaseConfiguration

Source code in qa-pytest-rest/src/qa_pytest_rest/rest_configuration.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class RestConfiguration(BaseConfiguration):
    """
    Configuration class for REST API endpoints.

    Inherits from:
        BaseConfiguration
    """

    @final
    @cached_property
    def base_url(self) -> str:
        """
        Returns the base URL for the endpoint from the configuration parser.

        Returns:
            str: The base URL specified in the configuration under the 'rest/base_url' key.
        """
        return self.parser["rest"]["base_url"]

    def resource_uri(self, path: str = EMPTY_STRING) -> str:
        """
        Constructs and returns the full endpoint URL by joining the base endpoint URL with the specified path.

        Args:
            path (str, optional): The path to append to the base endpoint URL. Defaults to EMPTY_STRING.

        Returns:
            str: The complete URL formed by joining the base endpoint and the provided path.
        """
        return urljoin(self.base_url, path)

base_url cached property

Returns the base URL for the endpoint from the configuration parser.

Returns:

Name Type Description
str str

The base URL specified in the configuration under the 'rest/base_url' key.

resource_uri(path=EMPTY_STRING)

Constructs and returns the full endpoint URL by joining the base endpoint URL with the specified path.

Parameters:

Name Type Description Default
path str

The path to append to the base endpoint URL. Defaults to EMPTY_STRING.

EMPTY_STRING

Returns:

Name Type Description
str str

The complete URL formed by joining the base endpoint and the provided path.

Source code in qa-pytest-rest/src/qa_pytest_rest/rest_configuration.py
32
33
34
35
36
37
38
39
40
41
42
def resource_uri(self, path: str = EMPTY_STRING) -> str:
    """
    Constructs and returns the full endpoint URL by joining the base endpoint URL with the specified path.

    Args:
        path (str, optional): The path to append to the base endpoint URL. Defaults to EMPTY_STRING.

    Returns:
        str: The complete URL formed by joining the base endpoint and the provided path.
    """
    return urljoin(self.base_url, path)

RestSteps

Bases: GenericSteps[TConfiguration]

BDD-style step definitions for REST API operations.

Type Parameters

TConfiguration: The configuration type, must be a RestConfiguration.

Attributes:

Name Type Description
_rest_session Session

The HTTP session used for sending REST requests.

Source code in qa-pytest-rest/src/qa_pytest_rest/rest_steps.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class RestSteps[TConfiguration: RestConfiguration](
        GenericSteps[TConfiguration]):
    """
    BDD-style step definitions for REST API operations.

    Type Parameters:
        TConfiguration: The configuration type, must be a RestConfiguration.

    Attributes:
        _rest_session (requests.Session): The HTTP session used for sending REST requests.
    """
    _rest_session: requests.Session

    @final
    def _invoke(self, request: Request) -> Response:
        """
        Sends the given HTTP request using the configured session.

        Args:
            request (Request): The HTTP request to send.
        Returns:
            Response: The HTTP response.
        """
        return self._rest_session.send(
            self._rest_session.prepare_request(request))

    @Context.traced
    @final
    def invoking(self, request: Request) -> Self:
        """
        Send a REST request and assert that the response is OK.

        Args:
            request (Request): The HTTP request to send.
        Returns:
            Self: Enables method chaining.
        Raises:
            AssertionError: If the response is not OK.
        """
        return self.eventually_assert_that(
            lambda: self._invoke(request).ok, is_(True))

    @Context.traced
    @final
    def the_invocation(
            self, request: Request, by_rule: Matcher[Response]) -> Self:
        """
        Send a REST request and assert that the response matches the given matcher.

        Args:
            request (Request): The HTTP request to send.
            by_rule (Matcher[Response]): The matcher to apply to the response.
        Returns:
            Self: Enables method chaining.
        Raises:
            AssertionError: If the response does not match the rule.
        """
        return self.eventually_assert_that(
            lambda: self._invoke(request),
            by_rule)

invoking(request)

Send a REST request and assert that the response is OK.

Parameters:

Name Type Description Default
request Request

The HTTP request to send.

required

Returns: Self: Enables method chaining. Raises: AssertionError: If the response is not OK.

Source code in qa-pytest-rest/src/qa_pytest_rest/rest_steps.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
@Context.traced
@final
def invoking(self, request: Request) -> Self:
    """
    Send a REST request and assert that the response is OK.

    Args:
        request (Request): The HTTP request to send.
    Returns:
        Self: Enables method chaining.
    Raises:
        AssertionError: If the response is not OK.
    """
    return self.eventually_assert_that(
        lambda: self._invoke(request).ok, is_(True))

the_invocation(request, by_rule)

Send a REST request and assert that the response matches the given matcher.

Parameters:

Name Type Description Default
request Request

The HTTP request to send.

required
by_rule Matcher[Response]

The matcher to apply to the response.

required

Returns: Self: Enables method chaining. Raises: AssertionError: If the response does not match the rule.

Source code in qa-pytest-rest/src/qa_pytest_rest/rest_steps.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@Context.traced
@final
def the_invocation(
        self, request: Request, by_rule: Matcher[Response]) -> Self:
    """
    Send a REST request and assert that the response matches the given matcher.

    Args:
        request (Request): The HTTP request to send.
        by_rule (Matcher[Response]): The matcher to apply to the response.
    Returns:
        Self: Enables method chaining.
    Raises:
        AssertionError: If the response does not match the rule.
    """
    return self.eventually_assert_that(
        lambda: self._invoke(request),
        by_rule)

RestTests

Bases: AbstractTestsBase[TSteps, TConfiguration]

Base class for REST API test cases.

This class provides a reusable test base for REST API testing, managing a requests.Session for each test method. It is generic over the types of steps and configuration used.

Attributes:

Name Type Description
_rest_session Session

The HTTP session used for making REST requests. Not thread-safe.

Type Parameters

TSteps: The type of the steps class, typically derived from RestSteps. TConfiguration: The type of the configuration class, typically derived from RestConfiguration.

Source code in qa-pytest-rest/src/qa_pytest_rest/rest_tests.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class RestTests[
    TSteps: RestSteps[Any],
    TConfiguration: RestConfiguration
](AbstractTestsBase[TSteps, TConfiguration]):
    """
    Base class for REST API test cases.

    This class provides a reusable test base for REST API testing, managing a `requests.Session`
    for each test method. It is generic over the types of steps and configuration used.

    Attributes:
        _rest_session (requests.Session): The HTTP session used for making REST requests. Not thread-safe.

    Type Parameters:
        TSteps: The type of the steps class, typically derived from RestSteps.
        TConfiguration: The type of the configuration class, typically derived from RestConfiguration.
    """
    _rest_session: requests.Session  # not thread safe

    @property
    def rest_session(self) -> requests.Session:
        """
        Returns the HTTP session used for making REST requests.

        Returns:
            requests.Session: The HTTP session instance.
        """
        return self._rest_session

    @override
    def setup_method(self):
        """
        Initializes a new requests.Session before each test method.
        """
        super().setup_method()
        self._rest_session = requests.Session()

    @override
    def teardown_method(self):
        """
        Closes the requests.Session after each test method.
        """
        try:
            self._rest_session.close()
        finally:
            super().teardown_method()

rest_session property

Returns the HTTP session used for making REST requests.

Returns:

Type Description
Session

requests.Session: The HTTP session instance.

setup_method()

Initializes a new requests.Session before each test method.

Source code in qa-pytest-rest/src/qa_pytest_rest/rest_tests.py
42
43
44
45
46
47
48
@override
def setup_method(self):
    """
    Initializes a new requests.Session before each test method.
    """
    super().setup_method()
    self._rest_session = requests.Session()

teardown_method()

Closes the requests.Session after each test method.

Source code in qa-pytest-rest/src/qa_pytest_rest/rest_tests.py
50
51
52
53
54
55
56
57
58
@override
def teardown_method(self):
    """
    Closes the requests.Session after each test method.
    """
    try:
        self._rest_session.close()
    finally:
        super().teardown_method()