OpenAPI Test Templates Generator

1.6.0 · maintenance · verified Wed Apr 22

OATTS (OpenAPI Test Templates) is a utility designed to generate basic Node.js unit test scaffolding directly from an OpenAPI specification document. Currently at version 1.6.0, it provides both a command-line interface and a module API to automate the creation of boilerplate tests for your API endpoints. The generated tests are structured for the Mocha testing framework and utilize Chakram for API assertions, encouraging developers to maintain a consistent contract between their API specification and its implementation. While providing valuable scaffolding for early and continuous API contract testing, it's important to note that OATTS is described as a 'work in progress' and 'not an officially supported Google product', suggesting a less frequent release cadence and potentially limited long-term support compared to core Google projects. Its primary differentiator is its focus on generating runnable Mocha/Chakram tests specifically for Node.js environments, building on lessons learned from `swagger-test-templates`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically use `oatts` to generate unit test scaffolding for a simple OpenAPI specification, writing them to a specified directory. It includes cleanup for the dummy spec.

const oatts = require('oatts');
const path = require('path');
const fs = require('fs');

// Create a dummy OpenAPI spec for demonstration
const dummyOpenApiSpecPath = path.join(__dirname, 'dummy-openapi.yaml');
const dummyOpenApiSpecContent = `
openapi: 3.0.0
info:
  title: Dummy API
  version: 1.0.0
servers:
  - url: http://localhost:3000
paths:
  /hello:
    get:
      summary: Greet the user
      operationId: getHello
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
  /goodbye/{name}:
    parameters:
      - name: name
        in: path
        required: true
        schema:
          type: string
    get:
      summary: Bid farewell
      operationId: getGoodbye
      responses:
        '200':
          description: Successful farewell
          content:
            application/json:
              schema:
                type: object
                properties:
                  farewellMessage:
                    type: string
`;

fs.writeFileSync(dummyOpenApiSpecPath, dummyOpenApiSpecContent);

// Options for test generation
const options = {
  host: 'http://localhost:3000', // Target API host for generated tests
  writeTo: path.join(__dirname, 'generated-tests'), // Directory to write tests
  // You can specify specific paths: paths: '/hello,/goodbye/{name}'
};

console.log('Generating tests for dummy OpenAPI spec...');

// Generate the tests
oatts.generate(dummyOpenApiSpecPath, options)
  .then((generatedFiles) => {
    console.log('Tests generated successfully:');
    generatedFiles.forEach(file => console.log(`- ${file}`));
    console.log(`
To run these tests, you'll need mocha and chakram installed:
  npm install --save-dev mocha chakram
Then, start your API server (if applicable) and run:
  mocha --recursive ${options.writeTo}`);
  })
  .catch((error) => {
    console.error('Error generating tests:', error);
  })
  .finally(() => {
    // Clean up the dummy spec file and generated test directory
    fs.unlinkSync(dummyOpenApiSpecPath);
    // Note: Deleting the generated test directory is more complex and left out for quickstart clarity
    // You might want to remove this for actual usage to inspect generated files.
  });

view raw JSON →