API tests
Tests from a spec, run in a minute
This is what happens after you import an OpenAPI description: tests are written from it, checked against a mock of it, and every failure says why. Press run; no account needed.
1. The spec
A small Bookstore API in OpenAPI 3: books you can list, create, read, replace and delete, and authors you can read. It's imported into a mock at https://mock.testingapis.com/c1sw260j9g, which stores what you send.
Show the spec
{
"openapi": "3.0.3",
"info": {
"title": "Bookstore API",
"version": "1.2.0",
"description": "A sample bookstore: books, their authors and reviews."
},
"servers": [
{
"url": "https://bookstore.example.com/v1"
}
],
"security": [
{
"bearerAuth": []
}
],
"components": {
"securitySchemes": {
"bearerAuth": {
"type": "http",
"scheme": "bearer"
}
},
"schemas": {
"Book": {
"type": "object",
"required": [
"id",
"title",
"authorId",
"price"
],
"properties": {
"id": {
"type": "integer",
"example": 42
},
"title": {
"type": "string",
"example": "The Pragmatic Programmer"
},
"authorId": {
"type": "integer",
"example": 7
},
"price": {
"type": "number",
"minimum": 0,
"example": 39.95
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"publishedOn": {
"type": "string",
"format": "date",
"nullable": true
}
}
},
"NewBook": {
"type": "object",
"required": [
"title",
"authorId",
"price"
],
"properties": {
"title": {
"type": "string"
},
"authorId": {
"type": "integer"
},
"price": {
"type": "number",
"minimum": 0
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"Author": {
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"country": {
"type": "string"
}
}
},
"Review": {
"type": "object",
"required": [
"id",
"rating"
],
"properties": {
"id": {
"type": "integer"
},
"rating": {
"type": "integer",
"minimum": 1,
"maximum": 5
},
"text": {
"type": "string"
}
}
},
"Error": {
"type": "object",
"required": [
"message"
],
"properties": {
"message": {
"type": "string"
}
}
}
},
"responses": {
"Unauthorized": {
"description": "Missing or invalid token",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"NotFound": {
"description": "No such resource",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
},
"paths": {
"/books": {
"get": {
"tags": [
"Books"
],
"summary": "List books",
"parameters": [
{
"name": "limit",
"in": "query",
"schema": {
"type": "integer"
},
"example": 20
}
],
"responses": {
"200": {
"description": "Books",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Book"
}
}
}
}
},
"401": {
"$ref": "#/components/responses/Unauthorized"
}
}
},
"post": {
"tags": [
"Books"
],
"summary": "Add a book",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NewBook"
},
"example": {
"title": "Refactoring",
"authorId": 7,
"price": 47.5,
"tags": [
"code"
]
}
}
}
},
"responses": {
"201": {
"description": "Created",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Book"
}
}
}
},
"400": {
"description": "Invalid book",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
},
"401": {
"$ref": "#/components/responses/Unauthorized"
}
}
}
},
"/books/{bookId}": {
"parameters": [
{
"name": "bookId",
"in": "path",
"required": true,
"schema": {
"type": "integer"
},
"example": 42
}
],
"get": {
"tags": [
"Books"
],
"summary": "Get a book",
"responses": {
"200": {
"description": "The book",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Book"
}
}
}
},
"404": {
"$ref": "#/components/responses/NotFound"
}
}
},
"put": {
"tags": [
"Books"
],
"summary": "Update a book",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/NewBook"
},
"example": {
"title": "Refactoring, 2nd edition",
"authorId": 7,
"price": 49.9
}
}
}
},
"responses": {
"200": {
"description": "Updated",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Book"
}
}
}
},
"400": {
"description": "Invalid book"
},
"404": {
"$ref": "#/components/responses/NotFound"
}
}
},
"delete": {
"tags": [
"Books"
],
"summary": "Delete a book",
"responses": {
"204": {
"description": "Deleted"
},
"404": {
"$ref": "#/components/responses/NotFound"
}
}
}
},
"/books/{bookId}/reviews": {
"parameters": [
{
"name": "bookId",
"in": "path",
"required": true,
"schema": {
"type": "integer"
},
"example": 42
}
],
"get": {
"tags": [
"Reviews"
],
"summary": "List a book's reviews",
"responses": {
"200": {
"description": "Reviews",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Review"
}
}
}
}
},
"404": {
"$ref": "#/components/responses/NotFound"
}
}
}
},
"/authors/{authorId}": {
"get": {
"tags": [
"Authors"
],
"summary": "Get an author",
"parameters": [
{
"name": "authorId",
"in": "path",
"required": true,
"schema": {
"type": "integer"
},
"example": 7
}
],
"responses": {
"200": {
"description": "The author",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Author"
}
}
}
},
"404": {
"$ref": "#/components/responses/NotFound"
}
}
}
},
"/health": {
"get": {
"tags": [
"Ops"
],
"summary": "Health check",
"security": [],
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {}
}
}
}
}
}
}
}2. The tests
30 tests written from the spec by the rules engine: valid requests with schema checks, missing and wrong-typed fields, unknown ids, malformed bodies. Tests that create a book delete it when the run ends.
Requests go from your browser to the mock. Nothing is saved.