Skip to content

Commit af0bfa3

Browse files
committed
doc: improve documentation
explain argument and how those can be used
1 parent 47590f5 commit af0bfa3

File tree

2 files changed

+72
-23
lines changed

2 files changed

+72
-23
lines changed

atest/library/all_cases.robot

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ Wrapper
1919
VAR &{headers} =
2020
END
2121
${r} = Call And Validate ${case} headers=${headers}
22-
Log ${r.json()}
22+
VAR ${body} = ${r.json()}
23+
Should Be True ${body}

src/SchemathesisLibrary/__init__.py

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from typing import TYPE_CHECKING, Any
1515

1616
from DataDriver import DataDriver # type: ignore
17-
from requests.auth import HTTPDigestAuth
1817
from robot.api import logger
1918
from robot.api.deco import keyword
2019
from robot.result.model import TestCase as ResultTestCase # type: ignore
@@ -35,18 +34,18 @@
3534

3635

3736
class SchemathesisLibrary(DynamicCore):
38-
"""SchemathesisLibrary is a library for validating API cases using Schemathesis.
37+
"""SchemathesisLibrary is a library for validating API schema using Schemathesis.
3938
4039
%TOC%
4140
4241
Example usage of the library and the `Call And Validate` keyword
4342
44-
Library must be imported with the `url` or `path` argument to specify the
43+
Library must be imported with the ``url`` or ``path`` argument to specify the
4544
OpenAPI schema. The library uses
4645
[https://github.com/Snooz82/robotframework-datadriver|DataDriver] to generate
4746
test cases from the OpenAPI schema by using
4847
[https://github.com/schemathesis/schemathesis|Schemathesis]. The library
49-
creates test cases that takes one argument, `${case}`, which is a
48+
creates test cases that takes one argument, ``${case}``, which is a
5049
Schemathesis
5150
[https://schemathesis.readthedocs.io/en/stable/reference/python/#schemathesis.Case|Case]
5251
object. The `Call And Validate` keyword can be used to call and validate
@@ -83,25 +82,25 @@ def __init__(
8382
) -> None:
8483
"""The SchemathesisLibrary can be initialized with the following arguments:
8584
86-
| =Argument= | =Description= |
87-
| `headers` | Optional HTTP headers to be used when schema is downloaded from `url`. |
88-
| `max_examples` | Maximum number of examples to generate for each operation. Default is 5. |
89-
| `path` | Path to the OpenAPI schema file. Using either `path` or `url` is mandatory. |
90-
| `url` | URL where the OpenAPI schema can be downloaded. |
91-
| `auth` | Optional authentication class to be used passed for Schemathesis authentication when test cases are executed. |
85+
| =Argument= | =Description= |
86+
| ``headers`` | Optional HTTP headers to be used when schema is downloaded from ``url``. |
87+
| ``max_examples`` | Maximum number of examples to generate for each operation. Default is 5. |
88+
| ``path`` | Path to the OpenAPI schema file. Using either ``path`` or ``url`` is mandatory. |
89+
| ``url`` | URL where the OpenAPI schema can be downloaded. |
90+
| ``auth`` | Optional authentication class to be used passed for Schemathesis authentication when test cases are executed. |
9291
93-
The `headers` argument is only needed when the schema is downloaded from a URL and there is need to pass example
94-
authentication headers to the endpoint. `headers` is not used the API calls are made during test execution.
92+
The ``headers`` argument is only needed when the schema is downloaded from a URL and there is need to pass example
93+
authentication headers to the endpoint. The ``headers`` is not used in the API calls are made during test execution.
9594
96-
`path` and `url` are mutually exclusive, only one of them should be used to specify the OpenAPI schema location.
95+
``path`` and ``url`` are mutually exclusive, only one of them should be used to specify the OpenAPI schema location.
9796
98-
`auth` can be used create Schemathesis
97+
``auth`` can be used create Schemathesis
9998
[https://schemathesis.readthedocs.io/en/stable/guides/auth/#dynamic-token-authentication|dynamic token]
10099
authentication for the test cases. The dynamic token generation class should
101-
follow the Schemathesis documentation. The only addition is the import. Importing
100+
follow the Schemathesis documentation. The only addition is the import and importing
102101
the class must follow the Robot Framework library
103102
[https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#specifying-library-to-import|import rules]
104-
, example if importing with filename, filename much match to the class name.
103+
. Example if importing with filename, filename much match to the class name.
105104
See example from
106105
[https://github.com/aaltat/robotframework-schemathesis?tab=readme-ov-file##dynamic-token-authentication|README.md]
107106
file
@@ -126,12 +125,31 @@ def call_and_validate(
126125
*,
127126
base_url: str | None = None,
128127
headers: dict[str, Any] | None = None,
129-
auth: tuple[str, str] | HTTPDigestAuth | None = None,
128+
auth: tuple[str, str] | Any | None = None,
130129
) -> Response:
131130
"""Call and validate a Schemathesis case.
132131
132+
| Argument | Description |
133+
| ``case`` | The Schemathesis [https://schemathesis.readthedocs.io/en/stable/reference/python/#schemathesis.Case|Case] to be called and validated. |
134+
| ``base_url`` | Optional base URL to override the one defined in the schema. Need mostly when schema is read from a file. |
135+
| ``headers`` | Optional HTTP headers to be added when calling the API endpoint. |
136+
| ``auth`` | Optional authentication to be used when calling the case. |
137+
138+
``auth`` can be any authentication supported by
139+
[https://www.python-httpx.org/advanced/authentication/|httpx authentication].
140+
Example a tuple containing username and password for basic authentication or
141+
and instance of
142+
[https://www.python-httpx.org/advanced/authentication/#digest-authentication|Digest authentication]
143+
144+
Returns a
145+
[https://schemathesis.readthedocs.io/en/stable/reference/python/#schemathesis.Response|Schemathesis Response]
146+
object. Can be used to further validate the response, example check specific headers
147+
or response body.
148+
133149
Example:
134-
| ${response} = Call And Validate Case ${case}
150+
| ${response} = `Call And Validate` ${case}
151+
| VAR ${body} ${response.json()}
152+
| Should Be True ${body} # Or whatever validation is needed by users
135153
"""
136154
logger.info(f"auth: {auth} {type(auth)}")
137155
headers = self._dot_dict_to_dict(headers) if headers else None
@@ -149,16 +167,34 @@ def call(
149167
*,
150168
base_url: str | None = None,
151169
headers: dict[str, Any] | None = None,
152-
auth: tuple[str, str] | HTTPDigestAuth | None = None,
170+
auth: tuple[str, str] | Any | None = None,
153171
) -> Response:
154172
"""Call a Schemathesis case without validation.
155173
174+
| =Argument= | =Description= |
175+
| ``case`` | The Schemathesis [https://schemathesis.readthedocs.io/en/stable/reference/python/#schemathesis.Case|Case] to be called and validated. |
176+
| ``base_url`` | Optional base URL to override the one defined in the schema. Need mostly when schema is read from a file. |
177+
| ``headers`` | Optional HTTP headers to be added when calling the API endpoint. |
178+
| ``auth`` | Optional authentication to be used when calling the case. |
179+
180+
``auth`` can be any authentication supported by
181+
[https://www.python-httpx.org/advanced/authentication/|httpx authentication].
182+
Example a tuple containing username and password for basic authentication or
183+
and instance of
184+
[https://www.python-httpx.org/advanced/authentication/#digest-authentication|Digest authentication]
185+
186+
Returns a
187+
[https://schemathesis.readthedocs.io/en/stable/reference/python/#schemathesis.Response|Schemathesis Response]
188+
object. Can be used to further validate the response, example check specific headers
189+
or response body.
190+
156191
The `Call` and `Validate Response` keywords can be used together
157192
to call a case and validate the response.
158193
159194
Example:
160-
| ${response} = Call Case ${case}
161-
| Validate Response ${case} ${response}
195+
| ${response} = `Call` ${case}
196+
| `Validate Response` ${case} ${response}
197+
162198
"""
163199
headers = self._dot_dict_to_dict(headers) if headers else None
164200
self.info(f"Calling case: {case.path} | {case.method} | {case.path_parameters}")
@@ -173,10 +209,22 @@ def validate_response(
173209
case: Case,
174210
response: Response,
175211
headers: dict[str, Any] | None = None,
176-
auth: tuple[str, str] | HTTPDigestAuth | None = None,
212+
auth: tuple[str, str] | Any | None = None,
177213
) -> None:
178214
"""Validate a Schemathesis response.
179215
216+
| Argument | Description |
217+
| ``case`` | The Schemathesis case to be validated against. |
218+
| ``response`` | The Schemathesis response to be validated. Returned by ``Call`` keyword. |
219+
| ``headers`` | Optional HTTP headers to be added when calling the API endpoint. |
220+
| ``auth`` | Optional authentication to be used when calling the case. |
221+
222+
``auth`` can be any authentication supported by
223+
[https://www.python-httpx.org/advanced/authentication/|httpx authentication].
224+
Example a tuple containing username and password for basic authentication or
225+
and instance of
226+
[https://www.python-httpx.org/advanced/authentication/#digest-authentication|Digest authentication]
227+
180228
The response is validated against the case's expectations.
181229
The `Call` and `Validate Response` keywords can be used together
182230
to call a case and validate the response. See the example from

0 commit comments

Comments
 (0)