Examples
beforeScript
Here is an example of how you might use the beforeScript
element in a Tesmon test. In this example, a beforeScript
script named generateStoreId
is defined. This script uses the Python uuid
library to generate a unique store ID, which is then stored in the context object. The context object is a built-in Tesmon feature that allows you to store and retrieve data that can be used in your tests.
The createStore
task in the example then uses the context.store_id
value in the HTTP request body, ensuring that a unique store ID is used for each request. This allows you to generate dynamic data and use it in your tests, which can be useful for a variety of purposes.
scripts:
generateStoreId: |
import uuid
context["store_id"] = str(uuid.uuid4())
actions:
createStore:
type: HTTP::CLIENT
props:
method: POST
baseUrl: https://api.example.com
path: "/store"
headers:
"Content-Type": "application/json"
body: |
{
storeId: ${{context.store_id}}
}
tests:
createStore:
tasks:
- beforeScript: scripts.generateStoreId
action: actions.createStore
validate
Here is an example of how you might use the validate
element in a Tesmon test. In this example, a validate
script named validateSingleResultIsReturned
is defined. This script uses an assert statement to check that the last_output
property of the context object contains exactly one row of data.
The validateSingleResultIsReturned
script is then specified as the validate element for the validateResult
task in the createStore
test. This means that the script will be executed after the validateResult
task is executed, and it will be used to validate the output of the task. If the assert statement in the script evaluates to True, the validation will pass and the test will continue. If the assert statement evaluates to False, the validation will fail and the test will be terminated.
scripts:
validateSingleResultIsReturned: assert len(context["last_output"]["rows"]) == 1
tests:
createStore:
tasks:
- action: ...
validate: scripts.validateSingleResultIsReturned
afterScript
Here is an example of how you might use the afterScript
element in a Tesmon test. In this example, an afterScript
script named saveAccessToken
is defined. This script retrieves the access_token
value from the last_output
property of the context object, and then saves the value in the context object under the key access_token.
The authenticate
task in the createStore
test uses the actions.authenticateUser
action, which performs an HTTP POST request to the /authorize
endpoint of the API. After this task is executed, the saveAccessToken
script is executed, which saves the access_token
value in the context object.
The validateHttpGetResponse
task in the createStore
test then uses the context.access_token
value in the HTTP request header, ensuring that the correct access_token
is used in the request. This allows you to access and use data generated by previous tasks in your tests, which can be useful for a variety of purposes.
scripts:
saveAccessToken: |
token = context["last_output"]["body"]["access_token"]
context["access_token"] = token
actions:
authenticateUser:
type: HTTP::CLIENT
props:
method: POST
baseUrl: https://api.example.com
path: "/authorize"
headers:
"Content-Type": "application/json"
body: |
{
'username': 'username1'
'password': '*********'
}
getUserData:
type: HTTP::CLIENT
props:
method: GET
baseUrl: https://api.example.com
path: "/user/100"
headers:
"Content-Type": "application/json"
"Authorization": "Bearer ${{context.access_token}}"
tests:
createStore:
tasks:
- action: actions.authenticateUser
- action: actions.getUserData