Robot Framework SchemathesisLibrary to automatically create test cases from API specifications.
Project description
Robot Framework SchemathesisLibrary
Robot Framework SchemathesisLibrary is a library build top of the Schemathesis. Schemathesis automatically generates thousands of test cases from your OpenAPI or GraphQL schema and finds edge cases that break your API.
SchemathesisLibrary uses DataDriver to create test cases from the Schemathesis Case object.
Installation
Install with pip, uv or any package manager that supports PyPi
pip install robotframework-schemathesislibrary
Keyword documentation
See keyword documentation for more details. A link older keyword documentation can be found from versions page
Usage
Test are automatically generated based your API specification, SchemathesisLibrary uses DataDriver internally, but you need to create template suite, so that DataDriver is able to create needed test for your test suite.
SchemathesisLibrary must be imported by url or path argument, which tell where
the API specification can obtained. As like with Datadriver, there must be
Test Template
setting defined. The template keyword must take one argument, usually referred as
${case} and the template keyword must call Call And Validate keyword with the
${case} argument.
Example test suite:
*** Settings ***
Library SchemathesisLibrary url=http://127.0.0.1/openapi.json
Test Template Wrapper
*** Test Cases ***
All Tests # This test is deleted by DataDriver
Wrapper test_case_1
*** Keywords ***
Wrapper
[Arguments] ${case}
Call And Validate ${case}
Authentication
Dynamic token authentication
Library currently supports Schemathesis
dynamic token
authentication by the library import auth argument. The dynamic token generation
class should follow the Schemathesis documentation. The only addition is the import.
Importing the class must follow the Robot Framework library
import rules
, example if importing with filename, filename much match to the class name. Example
if test case looks like:
*** Settings ***
Library SchemathesisLibrary url=http://127.0.0.1/openapi.json auth=${CURDIR}/AuthExtension.py
Test Template Wrapper
*** Test Cases ***
All Tests
Wrapper test_case_1
*** Keywords ***
Wrapper
[Arguments] ${case}
Call And Validate ${case}
And AuthExtension.py looks like
from base64 import b64encode
import schemathesis
from robot.api import logger
@schemathesis.auth()
class AuthExtension:
def get(self, case, ctx):
# Instead of hard coding secrets to class, it is better to get them dynamically.
# Jenkins or GitHub secrets, Azure keyvault, or from somewhere which is appropriate
# for your needs.
return b64encode("joulu:pukki".encode("utf-8")).decode("ascii")
def set(self, case, data, ctx):
case.headers = case.headers or {}
case.headers["Authorization"] = f"Basic {data}"
logger.debug(f"Updated headers for case: {case.operation.method} {case.operation.path}")
Then with all API calls, will have basic auth set in the headers for all calls made to your API endpoint.
Schemathesis hook support
Library supports extending Schemathesis by defining hooks. Hooks allows users to customize how Schemathesis generates test data, validates responses, and handles requests through hooks, custom checks, and data generation strategies. For more details about Schemathesis hooks, refer to Schemathesis extending documentation: https://schemathesis.readthedocs.io/en/stable/guides/extending/
Example if there need to add custom header in each request, then it is possible to import library with hook:
*** Settings ***
Variables authentication.py
Library SchemathesisLibrary
... url=http://127.0.0.1/openapi.json
... hook=${CURDIR}/hook_filter.py
Test Template Wrapper
*** Test Cases ***
All Tests
Wrapper test_case_1
*** Keywords ***
Wrapper
[Arguments] ${case}
Call And Validate ${case} auth=${BASIC_AUTH_TUPLE}
And when hook_filter.py looks like:
import schemathesis
global_count_count = 0
@schemathesis.hook
def filter_query(ctx, query) -> bool:
method = ctx.operation.method.lower().strip()
if method == "put":
global global_count_count
global_count_count += 1
if global_count_count > 2:
return False
return True
Then only two test with PUT request are generated.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file robotframework_schemathesislibrary-2.3.0.tar.gz.
File metadata
- Download URL: robotframework_schemathesislibrary-2.3.0.tar.gz
- Upload date:
- Size: 14.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa67a2813438d8b77071ca7804c0feb11e0049288ac80e03f96ecc1d4df65101
|
|
| MD5 |
85cd914ad141b8b2024abc0173dd95ce
|
|
| BLAKE2b-256 |
822d74227f5daa8a27826afdb0d118be67c8512584513cd670948e6aa6ea2242
|
File details
Details for the file robotframework_schemathesislibrary-2.3.0-py3-none-any.whl.
File metadata
- Download URL: robotframework_schemathesislibrary-2.3.0-py3-none-any.whl
- Upload date:
- Size: 14.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6ce987cb626403f77118dc48ae953628fc225929ada198d093031eaa7560a69
|
|
| MD5 |
037124a768188a1b8a453702d29927e2
|
|
| BLAKE2b-256 |
a83d935b8ad8254b06f880eb374428a947ce70cc066102fe53978a2e51dafc5b
|