The following example shows how to reuse an external YAML file (or an external YAML step) as re-usable content in Zerocode test cases, using the new ${YAML.FILE:<path>} token.
This mirrors the already existing ${JSON.FILE:<path>} token(see: External JSON file content (opens in a new tab)) but lets you author the re-usable content(or even a whole step) in YAML instead of JSON.
Raised via Issue #492 (opens in a new tab) and delivered via PR #505 (opens in a new tab).
Dependency:
<dependency>
<groupId>org.jsmart</groupId>
<artifactId>zerocode-tdd</artifactId>
<version>check-latest-in-readme-github</version>
</dependency>Scenario This Feature Can Be Used
- You already have (or prefer to write) a step, a request body, or an expected response body in YAML, and want to reuse it across one or more test scenarios(JSON or YAML) instead of duplicating it.
- You are gradually moving your test suite from JSON to YAML and want both formats to interoperate - a JSON scenario can pull in a YAML step, and vice versa.
Solution Example - Reusing a whole Step written in YAML
Main scenario file - scenario_get_api_step_test.yml
---
scenarioName: "GIVEN-the GitHub REST end point, WHEN-I invoke GET, THEN-I will receive the 200 status with body"
steps:
- stepFile: ${YAML.FILE:unit_test_files/yaml/scenario_get_api_step.yml}Re-usable step file - scenario_get_api_step.yml
name: "get_user_details"
url: "/users/googlebot"
operation: "GET"
request:
-
verify:
status: 200
body:
login: "googlebot"
type: "User"
verifyabove is just an alias ofassertions- both are supported, use whichever reads better for your team.
Reusing just a request/response body from a YAML file
The same ${YAML.FILE:<path>} token also works for any leaf node inside a step, e.g. to pull in a re-usable request or expected-response body from a separate .yaml/.yml file - exactly the way ${JSON.FILE:<path>} already works:
{
"scenarioName": "POST API - Yaml file as request/response content - Reuse body",
"steps": [
{
"name": "create_emp",
"url": "/api/v1/employees",
"operation": "POST",
"request": {
"body" : "${YAML.FILE:reusable_content/request/request_body.yaml}"
},
"assertions": {
"status": 201
}
}
]
}where request_body.yaml:
name: "Emma"
surName: "Norton"Run Log / Where to find the tests
This feature is covered by the below unit tests in the Zerocode repo itself:
- Test runner:
YamlUnitTest.java(opens in a new tab) - Main scenario file:
scenario_get_api_step_test.yml(opens in a new tab) - Re-usable step file:
scenario_get_api_step.yml(opens in a new tab)
Available since
V 1.4.x - see the releases page (opens in a new tab) for the exact version that includes PR #505.