Authoring tests in code view

With YAML-based test authoring, you can define API tests in a structured and consistent way to validate API behavior under different conditions. This method allows you to check API responses, confirm data integrity, and validate business logic through clearly defined test cases.

YAML makes it easy to organize test configurations that support automation and repeatability. You can outline expected API behavior, provide input data, and define assertions to verify whether the API works as intended. This structure helps development teams maintain long-term API quality and quickly identify issues during updates or deployments.

You can write test files from scratch, structure them clearly, and link them to your API, assertion, and environment configurations. The following sections describe the components of a test file and how they work together.

What is a test file?

A test file in API Studio is a YAML file with kind:test. It includes a set of test cases for a specific API and specifies the following:

  • The API methods to call
  • The input data to send
  • The assertions to apply to the responses

The test file also references:

  • An environment file that defines test variables such as apiKey and petid.
  • An assertion file that describes how to validate responses, for example, checking that the status code is 200.

Test authoring considerations

  • Each test file maps to only one API.
  • You can configure multiple test cases for different methods, such as GET and POST, within the same file.
  • You can add multiple assertions for a single response model.
  • You can reference environment variables from another kind or add them directly as variables in the test file.
  • You can define API test cases using kind:test, specify assertions using kind:assertion, and configure environments using kind:environment.
kind:test configuration template
kind: test
metadata:
  name: TestName                # example: TestPayments
  version: Version              # example: 1.0.0
  tags:
    - Tag1                      # example: functional
    - Tag2
  namespace: Namespace          # example: default

spec:
  api:
    $ref: Namespace:APIName:APIVersion  # example: default:PaymentAPI:1.0.1

  environment:
    $ref: Namespace:EnvironmentName:EnvironmentVersion  # example: default:testEnvironment:1.0.0
    variables:
      - key: "VariableKey1"     # example: "name"
        value: "Value1"         # example: "Tom"
        isSecret: true/false    # example: false
      - key: "VariableKey2"     # example: "password"
        value: ${VariableReference} # Reference value (prompts if isSecret is true)
        isSecret: true/false    # example: true

  request:
    - method: HTTPMethod        # example: GET
      resource: ResourcePath    # example: /pet/${petid}
      headers:
        - key: "HeaderKey1"     # example: "Content-Type"
          value: "HeaderValue1" # example: "application/json"
        - key: "HeaderKey2"
          value: ${HeaderVariableReference}  # Reference to the value

      parameters:
        - key: "ParameterKey1"  # example: "petid"
          value: ${ParameterReference}  # Reference to the value

      auth:
        noauth: true/false      # example: true
        basicAuth:
          userName: "Username"  # if using basicAuth
          password: "Password"
        bearerToken: "Token"

      payload:
        json: |
          {
            "Key": "Value",     # example: "name": "Adam"
            "Key2": Boolean     # example: "notify": true
          }
      settings:
        sslVerification: true/false   # example: true
        encodeURL: true/false     # example: true
  assertions:
    $ref: Namespace:AssertionName:AssertionVersion

In the template:

  • Replace placeholder text, such as TestName, Version, Namespace, with specific values relevant to your test.
  • Replace reference configurations such as Namespace:EnvironmentName:EnvironmentVersion, Namespace:AssertionName:AssertionVersion with specific values relevant to your test.
Category Field Description
kind   Defines the type of configuration. In this case, it specifies that this configuration is for test.
metadata.

Identifies and organizes the test with essential tags and identifiers.

name Names the test, serving as its unique identifier.
version Specifies the version of the test configuration.
tags Labels the test for categorization.
namespace Group the test into a logical category to manage test environments and resources.
spec.

Configures the test case, specifying the API endpoint, environment setup, request parameters, payload types, and assertions.

api Links the test to the API under examination, pointing to an existing API definition. You can do this by referencing an existing API definition by using the $ref parameter or $endpoint parameter:
  1. Using $ref parameter:

    Use the $ref parameter to point to an API definition in the format:

    $ref: Namespace:APIName:APIVersion

    For example:

    $ref: default:PaymentAPI:1.0.1
  2. Using $endpoint parameter:

    Alternatively, you can provide a direct URL for the API by using the $endpoint parameter in the format:

    $endpoint: endpoint

    For example:

    $endpoint: https://petstore.swagger.io/v2

environment Provides the reference to the environment configuration file. This configuration includes environment-specific variables, such as apiKey, that the test can dynamically use. To reference the environment variables, use the $ref parameter in the following format:

$ref: Namespace:EnvironmentName:​EnvironmentVersion

For example: default:testEnvironment:1.0.0

request Defines the HTTP request details, including:
  • method. The HTTP method to use. For example:GET, POST.
  • resource. The specific API endpoint, which can include dynamic parameters. For example:/pet/${petid}.
  • headers. Defines request headers in a list of key-value pairs.
  • parameters. Defines query, form, or path parameters in a list of key-value pairs.
  • auth. Authentication options, including no-auth, basicAuth, and bearerToken.
  • payload. Specifies different payload types that the test sends. For example: formData, json, and xml. Only one payload type is typically used per request.
  • settings. Defines how API requests are processed in API Studio, including parameters for handling SSL verification and URL encoding:
    • sslVerification. Enables or disables SSL certificate verification for API requests. When set to true, the system verifies SSL certificates to secure the connection, making it ideal for production environments. Setting it to false bypasses this check, which can be helpful in testing or development environments where SSL certificates might be self-signed or not required.
    • encodeURL. Controls whether URLs are automatically encoded before request transmission. When true, URLs are encoded to maintain proper formatting and transmission. When false, URLs are sent as-is.
assertions Validates the API response based on specified criteria. To reference the test assertion, use the $ref parameter in the following format: Namespace:AssertionName:​AssertionVersion

For example: default:testAssertion:1.0.0

A sample configuration for test kind is as follows:

kind: test
metadata:
  name: TestPayments
  version: 1.0.0
  tags:
    - functional
  namespace: default
spec:
  api:
    $ref: default:PaymentAPI:1.0.1
  environment:
   $ref: default:testEnvironment:1.0.0
  request:
    - method: GET
      resource: /pet/${petid}
      headers:
        - key: "Content-Type"
          value: "application/json"
        - key: "X-Gateway-APIKey"
          value: ${apiKey}
      auth:
        noauth: true
      payload:
        json: |
         {
          'name':'Adam',
          'notify': true
         }
      settings:
        sslVerification: false
        encodeURL: true
      assertions:
         $ref: default:testAssertion:1.0.0

In this example:

  • Metadata uniquely identifies the test as TestPayments with version 1.0.0.
  • Request configuration sends a GET request to /pet/${petid} using headers and JSON payload.