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
andpetid
. - 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 usingkind:assertion
, and configure environments usingkind:environment
.
kind:test
. Defines test cases for the API.kind:environment
. Configures environment variables and request parameters. For more information, see Defining an environment configuration in code view.kind:assertion
. Defines conditions to validate API responses. For more information, see Defining validation criteria using assertions in code view.
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.
- Replace placeholder text, such as
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:
|
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:
For example:
|
|
request |
Defines the HTTP request details, including:
|
|
assertions |
Validates the API response based on specified criteria. To reference the test assertion, use
the $ref parameter in the following format:
Namespace:AssertionName:AssertionVersionFor example:
|
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 asTestPayments
with version1.0.0
.Request
configuration sends aGET
request to/pet/${petid}
using headers and JSON payload.