NLdoc OpenAPI Test Set Generator

A powerful CLI tool for generating comprehensive test sets from OpenAPI 3.1.0 specifications. This TypeScript library automatically creates valid and invalid test examples based on schema definitions, enabling robust API validation testing.
Overview
The OpenAPI Test Set Generator extracts examples from OpenAPI specifications and systematically creates test variants to validate API implementations. It processes schema definitions and generates:
- Valid examples: Test cases that conform to the API specification
- Invalid examples: Test cases that deliberately violate schema constraints for negative testing
- Property variants: Multiple variations of examples with modified properties based on configurable rules
This tool is essential for:
- Automated API testing and validation
- Schema compliance verification
- Test-driven API development
- Generating comprehensive test suites from API specifications
Getting Started
Prerequisites
- Node.js >= 22.0.0
- npm >= 10.0.0
Installation
Install the package globally:
npm install -g @nldoc/openapi-test-set-generator
Or as a development dependency:
npm install --save-dev @nldoc/openapi-test-set-generator
Basic Usage
Create a configuration file (e.g., test-generator.yaml):
input:
openapi: ./api/openapi.json
output:
valid: ./tests/examples.valid.json
invalid: ./tests/examples.invalid.json
variantsOnExamples:
property:
- where: {}
do: {}
valid: true
- where:
required: true
hasDefault: false
do:
remove: true
valid: false
Run the generator:
openapi-test-set-generator extract-examples --input test-generator.yaml
Or using npx:
npx @nldoc/openapi-test-set-generator extract-examples -i test-generator.yaml
Configuration
Configuration File Structure
The generator uses a YAML configuration file with the following structure:
input:
openapi: <path-to-openapi-spec>
output:
valid: <output-path-for-valid-examples>
invalid: <output-path-for-invalid-examples>
variantsOnExamples:
property:
- where: <conditions>
do: <action>
valid: <boolean>
Property Modification Rules
The generator supports sophisticated property selection and modification:
Where Conditions
Select properties based on their schema characteristics:
where:
type: 'string'
format: 'email'
required: true
hasDefault: false
minLength:
$gte: 1
maxLength:
$lte: 100
Modification Actions
do:
set: <value>
remove: true
truncate: 10
Nested Property Support
The generator automatically traverses nested object structures to find and modify properties at any depth:
variantsOnExamples:
property:
- where:
type: 'string'
format: 'email'
do:
set: 'invalid-email'
valid: false
- where:
required: false
do:
remove: true
valid: true
Development
Local Setup
Available Scripts
npm run build - Compile TypeScript to JavaScript
npm run build:check - Type-check without emitting files
npm test - Run test suite with coverage
npm run test:watch - Run tests in watch mode
npm run lint - Lint the codebase
npm run format - Format code with Prettier
npm run format:check - Check code formatting
npm run fix - Auto-fix linting and formatting issues
API Documentation
CLI Commands
Extract and generate test examples from an OpenAPI specification.
openapi-test-set-generator extract-examples --input <config-file>
Options:
-i, --input <file> - Path to the configuration file (required)
Examples:
openapi-test-set-generator extract-examples --input otsg.yaml
openapi-test-set-generator extract-examples -i otsg.yml
Programmatic Usage
import {
importOpenAPIFile,
extractSchemaObjectsWithExamples,
createExampleVariantsFromSchemasOnQueries
} from '@nldoc/openapi-test-set-generator';
const spec = await importOpenAPIFile('path/to/openapi.json');
const schemas = extractSchemaObjectsWithExamples(spec);
const { valid, invalid } = await createExampleVariantsFromSchemasOnQueries(
schemas,
variantQueries
);
Example Scenarios
Common Test Patterns
-
Required Field Validation
- where:
required: true
hasDefault: false
do:
remove: true
valid: false
-
String Format Validation
- where:
type: 'string'
format: 'uuid'
do:
set: 'invalid-uuid'
valid: false
-
Array Constraints
- where:
type: 'array'
required: true
hasDefault: false
do:
set: []
valid: false
-
Boolean Value Testing
- where:
type: 'boolean'
do:
set: true
valid: true
- where:
type: 'boolean'
do:
set: false
valid: true
Integration Examples
This generator can be integrated into any project that uses OpenAPI specifications. Common integration patterns include:
- Generate comprehensive test sets for API schemas
- Validate API implementation compliance
- Create edge cases for robust testing
- Ensure API specification accuracy
Example integration in package.json:
{
"scripts": {
"build:examples": "openapi-test-set-generator extract-examples -i ./example-generator.yaml"
}
}
Testing
Run the test suite:
npm test
Run tests with coverage:
npm run test:watch run
Contributing
We welcome contributions! Please ensure:
- All tests pass (
npm test)
- Code is properly formatted (
npm run format:check)
- Linting rules are followed (
npm run lint)
- TypeScript compilation succeeds (
npm run build:check)
License
This project is licensed under the European Union Public License 1.2 - see LICENSE for details.
Acknowledgements