Maritest
Maritest is an API testing framework that the purpose solely of simplifying assertion when doing testing in the API layer, it's an easy and convenient way to go iterative testing while keeping up the fast-paced development and being able to maintain all testing modules/scenarios without breaking change
Installation
To install maritest just simply using :
pip install maritest
Basic Usage
After you're done with installation, you can try to use this basic feature from Maritest for example
from maritest.assertion import Assert
request = Assert(
method="GET",
url="https://jsonplaceholder.typicode.com/todos/1",
headers={},
proxy={"http": "http://api.services.com"},
timeout=60,
)
request.assert_is_ok(message="Should be success")
request.assert_is_failed(message="Should be failed")
If the assertion for that request is successful and according to what we expected, then it will return with a custom message that you already set before based on the message argument. If not successful, then it will raise an AssertionError
with a formatted error message that is already available (without you needing to customize the error message again).
For now, Maritest already have prepared several kinds of useful assertions method which include a common assertion that is always used in testing for API scenario, those are :
from maritest.assertion import Assert
request = Assert(method="GET", url="https://jsonplaceholder.typicode.com/todos/1", headers={})
request.assert_is_ok(message="Response is OK")
request.assert_is_failed(message="Response is FAILED")
request.assert_is_headers(message="Response have headers")
request.assert_is_content_type(message="Response have content-type")
request.assert_content_type_to_equal(value="application/json", message="Value of content-type is equal")
request.assert_is_2xx_status(message="Response status in 2xx")
request.assert_is_3xx_status(message="Response status in 3xx")
request.assert_is_4xx_status(message="Response status in 4xx")
request.assert_is_5xx_status(message="Response status in 5xx")
request.assert_has_content(message="Response has content")
request.assert_has_json(message="Response has JSON")
request.assert_has_text(message="Response has text")
request.assert_status_code_in(status_code=[200, 201], message="Response status in 2xx")
request.assert_status_code_not_in(status_code=[400, 404], message="Response status not in 4xx")
request.assert_json_to_equal(obj={"this one json object"}, message="Response is equal to JSON")
request.assert_content_to_equal(obj={"multipart"}, message="Response is equal to multipart")
request.assert_text_to_equal(obj=b'this one is bytes object', message="Response is equal to byte")
request.assert_response_time(duration=200, message="Response should not exceed the duration")
request.assert_response_time_less(message="Response should not exceed the limit")
request.assert_expected_to_fail(message="Response expected to be failed")
request.assert_tls_secure()
request.assert_content_length()
Documentation
For further detail of extending and advanced usage when using Maritest, please read the documentation.