Listen to this blog |
Writing automated API tests can be done using any Python HTTP client library & assertions packages. If we get all the API testing capabilities in one framework, it would minimize script creation & maintenance time. In this article, you will learn how to automate API testing using Apiritif framework.
Apiritif is a Python based API testing framework which eases automated API tests creation and maintenance. It is developed by Blazemeter and has all necessary utilities and assertions methods for API testing.
HTTP Requests
from apiritif import http response = http.get("http://example.com") response.assert_ok() # will raise AssertionError if request wasn't successful
Assertions
response = http.get("http://example.com/") # assert that request succeeded (status code is 2xx or 3xx) response.assert_ok() # assert that request has failed response.assert_failed() # status code based assertions response.assert_2xx() response.assert_3xx() response.assert_4xx() response.assert_5xx() response.assert_status_code(code) response.assert_not_status_code(code) # content-based assertions # assert that response body contains a string response.assert_in_body(member) # assert that response body doesn't contain a string response.assert_not_in_body(member) # search (or match) response body with a regex response.assert_regex_in_body(regex, match=False) response.assert_regex_not_in_body(regex, match=False) # assert that response has header response.assert_has_header(header) # assert that response has header with given value response.assert_header_value(header, value) # assert that response's headers contains a string response.assert_in_headers(member) response.assert_not_in_headers(member) # search (or match) response body with a regex response.assert_regex_in_headers(member) response.assert_regex_not_in_headers(member) # assert that response body matches JSONPath query response.assert_jsonpath(jsonpath_query, expected_value=None) response.assert_not_jsonpath(jsonpath_query) # assert that response body matches XPath query response.assert_xpath(xpath_query, parser_type='html', validate=False) response.assert_not_xpath(xpath_query, parser_type='html', validate=False)
Comments(0)