Generate OAS Specs from Tests:
Test Driven Documentation for Django

Test Driven Documentation

Your tests are already the source of truth for your API, now you can use them to generate OAS/Swagger every time you run them. This is great because:
  • Since tests are coupled to your code, the docs are updated on every commit
  • Tests are the best abstraction for defining an API contract
  • Tests document the actual behavior of your API, not how you think it works
  • You'll never write Swagger manually again

Install CLI

Optic is an open source project backed by YCombinator. It's great for documenting APIs and can output Swagger, API Clients or static documentation.
$ npm install @useoptic/cli -g 

Setup API

In order to learn your API spec, Optic needs to know:
  • How to run your API's integration tests
  • Which paths you want it to document
This configuration is defined in an optic.yml file placed in the root directory of your API repository. First navigate to your API repository:
$ cd path/to/repo
Then create an optic.yml manually or by running optic setup:init.
  id: your-api-name        # pick a slug for your API
  version: 0.1.0           # a semantic version for the API
  run_tests: npm run test  # the command that runs your tests
  paths:                   # the paths you want Optic to document
    - /self
    - /self/memberships/:teamId
    - /self/posts/:postId
Optic will only document paths that are listed in the optic.yml. Since it might take a few minutes to fill out the paths section of the file, we suggest picking 3 paths to start and adding the remaining ones at the end of the tutorial.

Using Optic with Django

Python APIs built with Django are easy to connect to Optic using our custom middleware.

Add the Middleware to your Project

Install the Optic Documenting Middleware using pip
pip3 install optic-document-django

Making the Middleware Run During Testing

Now add the middleware to the file that configures the Django API. You should put the middleware at the very bottom of the stack.

Verify Integration

Before we continue, let's take a moment to make sure everything is set up properly. You can run optic setup:tests to verify that:
  • Optic's documenting middleware has been included correctly
  • the optic.yml is configured to run your API tests properly
$ optic setup:tests

  Listening for API interactions in your tests... done

  Observed >= 1 API Interactions. Your test setup is valid
All setup? Nice work! Let's document your API now.

Document the API

Now that you've added Optic to your API, you can run the document command anytime to get the current contract of the API and generate a Swagger specification.
$ optic api:document --generate oas
The OAS Spec will be generated in .optic/oas.

That's it!

If this tutorial helped you check out Optic's Website and our GitHub to learn more about our other tools.