Writing Your First Test
Learn how to structure a Plyson test suite and add assertions.
Now that you understand the Project Structure, let's dive into writing your first test. Plyson tests are defined in JSON, which means they are purely declarative and easy to maintain.
Creating a Test Suite
You can manually create a JSON file or use the Plyson CLI to generate a boilerplate for you:
npx plyson generate suite usersThis will create a new file at suites/users.test.json.
Pro Tip: Plyson recursively discovers all files matching the
suites/**/*.test.jsonpattern. This means you can organize your tests into subfolders (e.g.,suites/auth/,suites/billing/) at any depth to keep your project clean.
A suite is a collection of Test Cases, and each case contains one or more Steps.
{
"title": "User Management API",
"description": "Tests for the user-related endpoints",
"testCases": [
{
"id": "USER_GET_001",
"title": "Should fetch a list of users",
"steps": [
{
"title": "List Users",
"request": {
"method": "GET",
"endpoint": "/users"
},
"response": {
"validations": {
"statusCode": 200
}
}
}
]
}
]
}Breakdown of the Test Step
title: A human-readable name for the step, shown in reports.request: Defines the HTTP call.method: GET, POST, PUT, DELETE, etc.endpoint: The path relative to your environment'sbaseUrl.
response: Defines what to do with the result.validations: A block for checks likestatusCodeor customassertions.
Adding Assertions
Simple status code checks are great, but usually, you want to validate the response body. Let's add an assertion to check if we received an array.
{
"title": "List Users",
"request": {
"method": "GET",
"endpoint": "/users"
},
"response": {
"validations": {
"statusCode": 200,
"assertions": [
{
"title": "Response is an array",
"from": "body",
"path": "$",
"operator": "isArray"
}
]
}
}
}Running the Test
Save your file and run your tests using the Plyson CLI:
npx plyson run --env devPro Tip: You can omit the
--envflag entirely if you have adefaultEnvspecified in yourproject.jsonfile. The CLI will automatically use that environment.
Plyson will:
- Load your
project.jsonanddev.env.json. - Construct the full URL using the
baseUrlfrom your environment. - Execute the request using Playwright.
- Validate the status code and your custom assertions.
- Generate a report of the results.