Skip to main content

Test

A test represents one or multiple API request to a certain endpoint.

Methods

withSecuritySchemes

Allows to attach one or multiple security schemas to the test plan.

import { Testplan, APIKeyAuth } from 'oat'

const apiToken = new APIKeyAuth('Authorization', 'vollq-b82b312d-4d44-40a3-bb5a-02529417e2d7', 'header')

const plan = new Testplan(specification)
plan.runTest('delete', '/api/specifications/{id}')
.withSecuritySchemes([apiToken])

withPayloads

Allows to attach one or multiple payload schemas to the test plan.

import { Testplan, URLParam, BodyPayload } from 'oat'

const urlParam = new URLParam({ id: '39f07889-1072-48df-8ca6-9d6726b5e525' })
const tokenPayload = new BodyPayload({ name: 'foobar', expires: null })

const plan = new Testplan(specification as OpenAPIV3.Document)
plan.runTest('delete', '/api/specifications/{id}')
.withPayloads([urlParam, tokenPayload])

expect

Defines the expected HTTP status code and optionally allows custom assertions on the response.

Basic Status Code Assertion

import { Testplan } from 'oat'

const plan = new Testplan(specification)
plan.runTest('delete', '/api/specifications/{id}').expect(401)

Status Code with Custom Assertions

import { Testplan } from 'oat'
import { expect } from 'vitest'

interface UserResponse {
id: string
name: string
}

const plan = new Testplan(specification)
plan.runTest('get', '/user/{id}')
.withPayloads(new URLParam({ id: '39f07889-1872-48df-8ca6-9d6726b5e526' }))
.expect(200, async (response: Response) => {
const body = await response.json() as UserResponse
expect(body.id).toBe('39f07889-1872-48df-8ca6-9d6726b5e526')
expect(body.name).toBe('Alice')
})