With Akita's Django integration, you can build an Akita spec from your Django integration tests! Here's how it works.
-
You can use the Akita/Django test client (
akita_django.test.Client
) as a nearly-drop-in replacement for Django's test client (django.test.Client
). The Akita client behaves just like the Django client, but it also creates a HAR file logging the requests and responses your integration tests send and receive through the client. Note: You will need to callakita_django.test.Client.close()
, which is not necessarily withdjango.test.Client
. See Replace the Client below. -
Once you have a HAR file, you can use Akita's
apispec
command to turn it into a spec.
Why a Django integration?
The Django test client makes Django tests more efficient by exercising your service without the extra overhead of running it as a full-fledged HTTP server. However, this means that Akita can't build a spec by watching network traffic, because the Django test client executes requests directly against your service, without sending traffic over the network.
The Akita/Django test client extends the Django test client to capture your traffic and output it as a HAR file.
Install akita-django
akita-django
The first step is to install the PyPI package with the Akita/Django test client.
pip install akita-django
Replace the Client
Next, there are three changes to make to your integration tests:
- Import
akita_django.test.Client
in place ofdjango.test.Client
. - (Optional) Set the output location for the HAR file.
- Call
client.close()
.
Here's a sample integration test with comments showing how to add the Akita/Django client.
# STEP 1: Import akita_django.test.Client instead of django.test.Client.
# from django.test import Client, TestCase
#
from akita_django.test import Client
from django.test import TestCase
from rest_framework import status
from .models import User, File
from .serializers import UserSerializer, FileSerializer
class UserTest(TestCase):
"""Tests for user-related APIs."""
def setUp(self):
# STEP 2 (optional): Set an output HAR file to save your trace.
# If not specified, this defaults to
# `akita_trace_{timestamp}.har`.
self.client = Client(har_file_path='akita_trace.user.har')
def test_user_crud(self):
kent = {
"first_name": "Kent",
"last_name": "Bazemore",
"email": "[email protected]",
"phone": "415-111-2233",
}
# Create user
response = self.client.post('/users/', kent, content_type="application/json")
expected = copy.deepcopy(kent)
expected["id"] = response.data["id"]
def tearDown(self):
# STEP 3: Call `client.close()`. This is necessary to terminate
# the JSON object in the HAR file and close the file descriptor.
self.client.close()
This example comes from the Akibox Django Tutorial, a tutorial we created to showcase the Akita/Django integration.
Run your Integration Tests
After you've swapped in the Akita/Django client, run your tests! When they finish, you should see a HAR file in the location you specified when creating the client.
Next Steps
Now that you have a HAR file, you can use Akita's apispec
command to turn it into a spec. Take a look at Generate Specs from Traffic for details.
Updated 9 months ago
What's Next
Generate Specs from Traffic |