Teskube Test Scripts
Test scripts are single executor oriented tests. The script can have different types, which depends on the types of executors installed in your cluster.
Testkube includes postman/collection
, cypress/project
and curl/test
script types that are auto registered during the Testkube install by default. // provide examples for cypress and curl
As Testkube was designed with flexibility in mind - executors may be added for handling additional script types.
Script Test Source
Scripts can currently be created from two sources:
- A simple file with test content. For postman collections, we're exporting collection as JSON file. For curl executor, we're passing JSON file with configured curl command.
Git
- we can passrepository
,path
andbranch
where our test is. This is used in Cypress executor - as Cypress tests are more like npm-based projects which can have a lot of files. Sparse checkouts are handled that are fast even in the case of huge mono-repos.
Create a Script
Create Your First Script from a File (Postman Collection Test)
To create your first postman collection in Testkube, export your collection into a file.
- Right click on your collection name:
- Click the Export button:
- Save in a convenient location (We're using
~/Downloads/TODO.postman_collection.json
path.):
kubectl testkube create test --file ~/Downloads/TODO.postman_collection.json --name test
Output:
Detected test script type postman/collection
Script created test 🥇
Your reusable script is created!
Updating Scripts
To update your script after a change in Postman, re-export to a file and run the update command:
kubectl testkube update test --file ~/Downloads/TODO.postman_collection.json --name my-test
Output:
Detected test script type postman/collection
Script updated my-test 🥇
Testkube will override all script settings and content with update method.
Checking Script Content
Let's see what has been created:
kubectl get tests -n testkube
Output:
NAME AGE
my-test 32s
$ kubectl testkube get test my-test
name: my-test
type_: postman/collection
content: |-
{
"info": {
"_postman_id": "b40de9fe-9201-4b03-8ca2-3064d9027dd6",
"name": "TODO",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Create TODO",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 201 CREATED\", function () {",
" pm.response.to.have.status(201);",
"});",
"",
"",
"pm.test(\"Check if todo item craeted successfully\", function() {",
" var json = pm.response.json();",
" pm.environment.set(\"item\", json.url);",
" pm.sendRequest(json.url, function (err, response) {",
" var json = pm.response.json();",
" pm.expect(json.title).to.eq(\"Create video for conference\");",
"",
" });",
" console.log(\"creating\", pm.environment.get(\"item\"))",
"})",
"",
""
],
"type": "text/javascript"
}
},
{
"listen": "prerequest",
"script": {
"exec": [
""
],
"type": "text/javascript"
}
}
],
"protocolProfileBehavior": {
"disabledSystemHeaders": {}
},
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\"title\":\"Create video for conference\",\"order\":1,\"completed\":false}"
},
"url": {
"raw": "{{uri}}",
"host": [
"{{uri}}"
]
}
},
"response": []
},
{
"name": "Complete TODO item",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
"console.log(\"completing\", pm.environment.get(\"item\"))"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "PATCH",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\"completed\": true}"
},
"url": {
"raw": "{{item}}",
"host": [
"{{item}}"
]
}
},
"response": []
},
{
"name": "Delete TODO item",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
"console.log(\"deleting\", pm.environment.get(\"item\"))"
],
"type": "text/javascript"
}
},
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status code is 204 no content\", function () {",
" pm.response.to.have.status(204);",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "DELETE",
"header": [],
"url": {
"raw": "{{item}}",
"host": [
"{{item}}"
]
}
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
],
"variable": [
{
"key": "uri",
"value": "http://34.74.127.60:8080/todos"
},
{
"key": "item",
"value": null
}
]
}
We can see that the script resource was created with Postman collection JSON content.
You can also check scripts with the standard kubectl
command, which will list the Scripts Custom Resource.
kubectl get tests -ntestkube my-test -oyaml
Create a Script from Git
Some executors can handle files and some can handle only Git resources. Consult the executor readme file to determine which scripts types are handled.
Let's assume that a Cypress project is created in a Git repository and one has been created in https://github.com/kubeshop/testkube-executor-cypress/tree/main/examples/tree/main/examples.
Where examples is a test directory in the https://github.com/kubeshop/testkube-executor-cypress.git
repository.
Now we can create our Cypress based script (in Git based scripts we need to pass the script type)
kubectl testkube create test --git-uri https://github.com/kubeshop/testkube-executor-cypress.git --git-branch main --git-path examples --name kubeshop-cypress --type cypress/project
Output:
Script created kubeshop-cypress 🥇
The created Testkube test script is defined on the cluster:
$ kubectl get tests -n testkube kubeshop-cypress -o yaml
apiVersion: tests.testkube.io/v1
kind: Script
metadata:
creationTimestamp: "2021-11-17T12:29:32Z"
generation: 1
name: kubeshop-cypress
namespace: testkube
resourceVersion: "225162"
uid: f0d856aa-04fc-4238-bb4c-156ff82b4741
spec:
repository:
branch: main
path: examples
type: git
uri: https://github.com/kubeshop/testkube-executor-cypress.git
type: cypress/project
This script has spec.repository
with Git repository data. Those data can now be used by the executor to download script data.
Summary
Scripts are the smallest abstractions over tests in Testkube. They can be created with different sources and used by executors to run on top of a particular test framework.