oatts-cypress
Version:
An Open API Cypress Test Template Generator
299 lines (253 loc) • 15.3 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Namespaces</h3><ul><li><a href="compilation.html">compilation</a><ul class='methods'><li data-type='method'><a href="compilation.html#compile">compile</a></li><li data-type='method'><a href="compilation.html#compileOperationLevel">compileOperationLevel</a></li><li data-type='method'><a href="compilation.html#compilePathLevel">compilePathLevel</a></li><li data-type='method'><a href="compilation.html#compileTransactionLevel">compileTransactionLevel</a></li><li data-type='method'><a href="compilation.html#prepareTemplate">prepareTemplate</a></li></ul></li><li><a href="processing.html">processing</a><ul class='methods'><li data-type='method'><a href="processing.html#determineQueryType">determineQueryType</a></li><li data-type='method'><a href="processing.html#isNumberType">isNumberType</a></li><li data-type='method'><a href="processing.html#lookupCustomQueryValue">lookupCustomQueryValue</a></li><li data-type='method'><a href="processing.html#lookupCustomValue">lookupCustomValue</a></li><li data-type='method'><a href="processing.html#pathify">pathify</a></li><li data-type='method'><a href="processing.html#process">process</a></li><li data-type='method'><a href="processing.html#processHeaders">processHeaders</a></li><li data-type='method'><a href="processing.html#processOperations">processOperations</a></li><li data-type='method'><a href="processing.html#processParams">processParams</a></li><li data-type='method'><a href="processing.html#processPaths">processPaths</a></li><li data-type='method'><a href="processing.html#processResponse">processResponse</a></li><li data-type='method'><a href="processing.html#processTransactions">processTransactions</a></li><li data-type='method'><a href="processing.html#replaceNewlines">replaceNewlines</a></li></ul></li><li><a href="templateHelpers.html">templateHelpers</a><ul class='methods'><li data-type='method'><a href="templateHelpers.html#isNotDefaultStatusCode">isNotDefaultStatusCode</a></li><li data-type='method'><a href="templateHelpers.html#json">json</a></li><li data-type='method'><a href="templateHelpers.html#notEmptyObject">notEmptyObject</a></li></ul></li></ul><h3>Global</h3><ul><li><a href="global.html">generate</a></li><li><a href="global.html">merge2</a></li></ul>
</nav>
<div id="main">
<section class="readme">
<article><p><a href="https://travis-ci.org/google/oatts"><img src="https://travis-ci.org/google/oatts.svg?branch=master" alt="Build Status"></a></p>
<h1>OpenAPI Test Templates (oatts)</h1>
<blockquote>
<p>Generate basic unit test scaffolding for your <a href="https://www.openapis.org/">OpenAPI specification</a>.</p>
</blockquote>
<h2>Disclaimer</h2>
<p>This is not an officially supported Google product.</p>
<p><code>oatts</code> is based off of the <a href="https://github.com/apigee-127/swagger-test-templates">swagger-test-templates</a> module and the lessons learned during its development.</p>
<p><em>This is a work in progress.</em></p>
<h2>Goal</h2>
<p>The goal of <code>oatts</code> is to provide a standalone module for generating Node.js unit test code scaffolding based on a given OpenAPI specification.</p>
<p>The hope is that by providing such a tool, API developers will be encouraged to test the contract between their spec and backend early, often and continuously as the project grows.</p>
<h2>Usage</h2>
<p>There are a couple ways to use <code>oatts</code>.</p>
<h3>Module</h3>
<p>Install via <code>npm</code></p>
<pre><code>npm install --save oatts
</code></pre>
<p>Then use it in code</p>
<pre class="prettyprint source lang-js"><code>var oatts = require('oatts');
var options = {
// see "Options" section below for available options
};
var tests = oatts.generate('/path/to/openapi.yaml', options);
console.log(tests)
</code></pre>
<h3>Command line interface</h3>
<p>Install globally via <code>npm</code></p>
<pre><code>npm install -g oatts
</code></pre>
<p>Then use in your command line</p>
<pre class="prettyprint source lang-sh"><code>> oatts generate --help
Usage: generate [options]
generate unit test scaffolding for a given OpenAPI/Swagger Spec
Options:
-h, --help output usage information
--host <host> target hostname to use in test generation
-p, --paths <paths> comma separated list of paths to generate tests for
-e, --samples generate sample response bodies rather than schema, if applicable
-s, --spec <spec> path to the target OpenAPI/Swagger spec document to consume
-w, --writeTo <writeTo> directory to write the generated tests to file
-c, --consumes <consumes> consumes/content-type to use in request when applicable to the API resource
-o, --produces <produces> produces/accept to use in request when applicable to the API resource
-u, --customValues <customValues> custom request values to be used in generation; takes precedent over a customValuesFile
--customValuesFile <customValuesFile> path to JSON file with custom request values to be used in generation
-m, --scheme <scheme> which scheme to use if multiple are present in spec
-t --templates <templateDir> path to direcotry of custom templates
-S, --status-codes <statusCodes> comma separated list of status codes to explicity generate tests for
> oatts generate -s ./path/to/openapi.yaml -w ./output/dir
> ls ./output/dir
pet-test.js pet-{petId}-uploadImage-test.js user-test.js
. . .
</code></pre>
<h3>Using the result</h3>
<p>The resulting test files are built using the <a href="https://mochajs.org/">mocha</a> testing framework and <a href="http://dareid.github.io/chakram/">chakram</a> API testing framework. Thus, you will need both of these dependencies installed in order to run your newly generated tests.</p>
<p>After installing these, you can run the tests with mocha:</p>
<pre class="prettyprint source"><code># start your API server to test against!!
> mocha --recursive <test directory>
tests for /goodbye
tests for get
✓ should respond 200 for "Success" (57ms)
tests for /hello
tests for get
✓ should respond 200 for "Success"
2 passing (82ms)
</code></pre>
<h3>Custom Values</h3>
<p>Custom values can be supplied through both the command line and a JSON file. The in-line, command line supplied JSON will take precedent.</p>
<p>An example custom values JSON file can be found <a href="./test/process/documents/customValuesTest.json">here</a>.</p>
<h3>Custom Templates</h3>
<p>Custom templates can be supplied via the <code>templates</code> option. The directory pointed to by the option must contain 4 <a href="http://handlebarsjs.com/">Handlebars</a> templates named the same way as those found in <code>./templates</code>.</p>
<ul>
<li><code>topLevel.handlebars</code>: the top level template for a single test file</li>
<li><code>pathLevel.handlebars</code>: the path level template, usually the beginning of a test suite for a specific path</li>
<li><code>operationLevel.handlebars</code>: the operation level template, for a single operation test suite</li>
<li><code>transactionLevel.handlebars</code>: the template for a single transaction, or a single response code's unit test</li>
</ul>
<p>The data available to be used in the templates is specified in the <code>ProcessedSpec</code> type.</p>
<p>There are also a few helpers available to be used in the Handlebars templates, which can be found in the <code>templateHelpers</code> documentation namespace. Use the default templates as examples of how to use them.</p>
<h2>Options</h2>
<p>The following options can be passed to the generation function, some/all are exposed in the accompanying CLI:</p>
<table>
<thead>
<tr>
<th>Name</th>
<th style="text-align:center">CLI Flag</th>
<th style="text-align:right">Default</th>
<th style="text-align:right">Required</th>
<th style="text-align:right">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>spec</code></td>
<td style="text-align:center"><code>--spec -s</code></td>
<td style="text-align:right">n/a</td>
<td style="text-align:right"><code>true</code></td>
<td style="text-align:right">Path to a <code>swagger.yaml</code> or <code>openapi.yaml</code></td>
</tr>
<tr>
<td><code>host</code></td>
<td style="text-align:center"><code>--host</code></td>
<td style="text-align:right"><code>spec.host</code></td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">Hostname to put in test requests; defaults to <code>host</code> in given spec</td>
</tr>
<tr>
<td><code>paths</code></td>
<td style="text-align:center"><code>--paths -p</code></td>
<td style="text-align:right"><code>spec.paths</code></td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">API paths to generate tests for; defaults to all paths in given spec</td>
</tr>
<tr>
<td><code>samples</code></td>
<td style="text-align:center"><code>--samples -e</code></td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">Toggle generating sample responses for assertion</td>
</tr>
<tr>
<td><code>writeTo</code></td>
<td style="text-align:center"><code>--writeTo -w</code></td>
<td style="text-align:right">n/a</td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">Directory to write generated tests to; will create the directory if it doesn't exist</td>
</tr>
<tr>
<td><code>consumes</code></td>
<td style="text-align:center"><code>--consumes -c</code></td>
<td style="text-align:right"><code>operation.consumes[0]</code> | | <code>spec.conumes[0]</code></td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">Consumes header to use in a request when applicable</td>
</tr>
<tr>
<td><code>produces</code></td>
<td style="text-align:center"><code>--produces -o</code></td>
<td style="text-align:right"><code>operation.produces[0]</code> | | <code>spec.produces[0]</code></td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">Produces header to use in a request when applicable</td>
</tr>
<tr>
<td><code>customValues</code></td>
<td style="text-align:center"><code>--customValues -u</code></td>
<td style="text-align:right">n/a</td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">Values to be populated in requests where specified; overrides <code>customValuesFile</code></td>
</tr>
<tr>
<td><code>customValuesFile</code></td>
<td style="text-align:center"><code>--customValuesFile</code></td>
<td style="text-align:right">n/a</td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">Path to a JSON file with values to populate in requests</td>
</tr>
<tr>
<td><code>scheme</code></td>
<td style="text-align:center"><code>--scheme -m</code></td>
<td style="text-align:right"><code>spec.schemes[0]</code></td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">Override for multiple scheme present in a spec</td>
</tr>
<tr>
<td><code>templates</code></td>
<td style="text-align:center"><code>--templates -t</code></td>
<td style="text-align:right"><code>'./templates'</code></td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">Path to directory containing custom templates</td>
</tr>
<tr>
<td><code>statusCodes</code></td>
<td style="text-align:center"><code>--status-codes -S</code></td>
<td style="text-align:right"><code>operation.responses</code></td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">comma separated list of status codes to explicity generate tests for</td>
</tr>
<tr>
<td><code>jsonRefs</code></td>
<td style="text-align:center"></td>
<td style="text-align:right">n/a</td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right"><em>(See <a href="https://github.com/whitlockjc/json-refs/blob/master/docs/API.md#module_JsonRefs..JsonRefsOptions">JsonRefs~JsonRefsOptions</a>)</em></td>
</tr>
<tr>
<td><code>customFormats</code></td>
<td style="text-align:center"></td>
<td style="text-align:right">n/a</td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">The key/value pair of custom formats <em>(The keys are the format name and the values are async functions. See <a href="https://github.com/zaggino/z-schema#register-a-custom-format">ZSchema Custom Formats</a>)</em></td>
</tr>
<tr>
<td><code>customFormatGenerators</code></td>
<td style="text-align:center"></td>
<td style="text-align:right">n/a</td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">The key/value pair of custom format generators <em>(The keys are the format name and the values are functions. See <a href="https://github.com/json-schema-faker/json-schema-faker#custom-formats">json-schema-mocker Custom Format</a>)</em></td>
</tr>
<tr>
<td><code>customValidators</code></td>
<td style="text-align:center"></td>
<td style="text-align:right">n/a</td>
<td style="text-align:right"><code>false</code></td>
<td style="text-align:right">The custom validators. See <a href="https://github.com/apigee-127/sway/blob/master/docs/API.md#module_sway.DocumentValidationFunction">DocumentValidationFunction</a></td>
</tr>
</tbody>
</table>
<h2>Testing</h2>
<p>To test this module simply use the <code>npm</code> script</p>
<pre><code>npm test
</code></pre>
<h2>Discussion</h2>
<p>If you have a question or a topic you'd like to discuss, please feel free to open
a discussion on our Google Group <a href="https://groups.google.com/forum/#!forum/oatts-users/">oatts-users</a>.</p>
<h2>Contributing</h2>
<p>Contributors are welcome! Please see <a href="CONTRIBUTING.md">CONTRIBUTING.md</a>.</p>
<h2>Copyright</h2>
<p>Copyright 2018, Google Inc.</p>
<h2>License</h2>
<p>See <a href="LICENSE">LICENSE</a> file.</p></article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Sat Aug 24 2019 12:31:35 GMT+0100 (Western European Summer Time) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>