Rest API tester
Project description
Python REST API Tester
Installation
pip install rest-api-tester
- This is available for Python3.6+
About
rest-api-tester
makes it easy to create REST API tests for your APIs.
Though this tool is written in Python, your APIs need not be!
Tutorial
- Create a test cases file named
__data__/test_something.json
{
"create_something": {
"url": "/something",
"method": "POST",
"status": 200,
"request": {
"name": "something"
},
"response": {
"id": 1,
"name": "something"
}
}
}
- Create a unit test class
from rest_api_tester.test import TestCase
class TestSomething(TestCase):
...
- Set up your test case runner
import os
from rest_api_tester.client import TestClient
from rest_api_tester.test import TestCase
from rest_api_tester.runner import TestCaseRunner
class TestSomething(TestCase):
def setUp(self) -> None:
test_client = TestClient(base_url='https://myapp.com:5000')
path_to_data = os.path.join(os.path.dirname(__file__), '__data__')
self.runner = TestCaseRunner(
client=test_client,
path_to_data=path_to_data,
default_content_type='application/json'
)
- Create your first API test
class TestSomething(TestCase):
...
def test_create_something(self) -> None:
result = self.runner.run(
path_to_test_cases='test_something.json',
test_name='create_something'
)
self.verify_test_result(result=result)
Other Details
- There is a JSON file parser provided out of the box that can be used to parse JSON test case files
- If needed, you can write your own parser for another format... this is quite trivial
- Your custom parser function should be provided as an argument to
TestCaseRunner.run
- Requests and responses can be referenced as an external file as shown here
- The file path is relative to
TestCaseRunner.path_to_data
- The file path is relative to
- Test case data can be augmented dynamically before tests are run and also before test results are verified
TestCaseRunner.run
returns aTestResult
object. You can then useTestCase.verify_test_result
to verify the result is as expected- Before
TestCase.verify_test_result
is called, you can modify theTestResult
object, if needed - If you need to modify the test case data before the test is run, you can use the
test_data_modifier
argument ofTestCaseRunner.run
- This is a convenient place to programmatically add authentication data to your test case requests
TestCaseRunner
needs aclient
provided so that it can make requests to your API- There is a
TestClient
class provided out of the box, which makes simple HTTP requests to your API using therequests
library - If you need something more custom, you can create your own class that extends from
BaseTestClient
- There is a
TestCase.verify_test_result
uses a simple verifier by default- If you need something more custom, the verifier can be overridden by using the
verifier
argument
- If you need something more custom, the verifier can be overridden by using the
Not Supported
- File uploads
- SOAP
- Websockets
Examples
- You can find more advanced API test examples here.
- Note: The examples provided are run against a FastAPI server, and thus need Python3.7+ to run
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
rest_api_tester-0.1.1.tar.gz
(6.0 kB
view hashes)