Gonkey - a testing automation tool
View Russian version of this file here
Gonkey will test your services using their API. It can bomb the service with prepared requests and check the responses. Test scenarios are described in YAML-files.
Capabilities:
To test a service located on a remote host, use gonkey as a console util.
./gonkey -host -tests [-spec ] [-db_dsn -fixtures ] [-allure] [-v]
-spec path to a file or URL with the swagger-specs for the service-host service host:port-tests test file or directory-db-type - database type. PostgreSQL, Aerospike, Redis are currently supported.-aerospike_host when using Aerospike - connection URL in a form of host:port/namespace-redis_url when using Redis - connection address, for example redis://user:password@localhost:6789/1?dial_timeout=1&db=1&read_timeout=6s&max_retries=2-db_dsn DSN for the test DB (the DB will be cleared before seeding!), supports only PostgreSQL-fixtures fixtures directory-allure generate an Allure-report-allure-format Allure report format: v2/json (modern JSON, default) or v1/xml (legacy XML)-v verbose output-debug debug outputYou can't use mocks in this mode.
To integrate functional and native Go tests and run them together, use gonkey as a library.
Create a test file, for example func_test.go.
Import gonkey as a dependency to your project in this file.
import (
"github.com/lamoda/gonkey/runner"
"github.com/lamoda/gonkey/mocks"
)Create a test function.
…Starts from version 1.18.3, externally written fixture loader may be used for loading test data, if gonkey used as a library. To start using the custom loader, you need to import the custom module, that contains implementation of fixtures.Loader interface.
Example with a redis fixtures loader:
…The tests can be now ran with go test, for example: go test ./....
…As you can see in this example, you can use Regexp for checking response body. It can be used for all body (if it's plaint text):
response:
200: "$matchRegexp(^xy+z$)"or for elements of map/array (if it's JSON):
response:
200: |
{
"id": "$matchRegexp([\\w-]+)",
"jsonrpc": "$matchRegexp([12].0)",
"result": [
"data": [
"$matchRegexp(ORDER[0]{3}[0-9])",
"$matchRegexp(ORDER[0]{3}[0-9])"
],
]
}Also, "?" in query is optional
status - a parameter, for specially mark tests, can have following values:
broken - do not run test, only mark it as brokenskipped - do not run test, skip itfocus - run only this specific test, and mark all other tests with unset status as skippedYou can add Allure metadata to tests for integration with Test Management Systems (TestIT, Allure TestOps, etc.). Metadata is added in the allure section:
…By default, gonkey generates reports in modern Allure 2 format (JSON). To use legacy XML format (Allure 1.x), specify -allure-format=v1 or GONKEY_ALLURE_FORMAT=v1.
When using gonkey as a library, you can set default package and testClass labels globally via RunWithTestingParams:
runner.RunWithTesting(t, &runner.RunWithTestingParams{
Server: srv,
TestsDir: "cases",
AllurePackage: "api", // Default package label for all tests
AllureTestClass: "UserHandler", // Default testClass label for all tests
})These labels are used for TestIT integration and help organize tests by component (package) and implementation class (testClass). Typical values:
AllurePackage: "api", "cron", "consumer", "scheduler"AllureTestClass: handler or method name, e.g., "OrdersHandler", "CreateUser"Priority: Test-level labels (defined in YAML) always override these global defaults. This allows you to:
method - a parameter for HTTP request type, the format is in the example above.
path - a parameter for URL path, the format is in the example above.
headers - a parameter for HTTP headers, the format is in the example above.
cookies - a parameter for cookies, the format is in the example above.
response - the HTTP response body for the specified HTTP status codes.
responseHeaders - all HTTP response headers for the specified HTTP status codes.
You can use variables in the description of the test, the following fields are supported:
Example:
- method: "{{ $method }}"
description: "{{ $description }}"
path: "/some/path/{{ $pathPart }}"
query: "{{ $query }}"
headers:
header1: "{{ $header }}"
request: '{"reqParam": "{{ $reqParam }}"}'
response:
200: "{{ $resp }}"
mocks:
server_mock:
strategy: constant
body: >
{
"message": "{{ $mockParam }}"
}
statusCode: 200
dbQuery: >
SELECT id, name FROM testing_tools WHERE id={{ $sqlQueryParam }}
dbResponse:
- '{"id": {{ $sqlResultParam }}, "name": "gonkey"}'You can assign values to variables in the following ways (priorities are from top to bottom):
Example:
- method: "{{ $method }}"
path: "/some/path/{{ $pathPart }}"
variables:
reqParam: "reqParam_value"
method: "POST"
pathPart: "part_of_path"
query: "query_val"
header: "header_val"
resp: "resp_val"
query: "{{ $query }}"
headers:
header1: "{{ $header }}"
request: '{"reqParam": "{{ $reqParam }}"}'
response:
200: "{{ $resp }}"Example:
# if the response is plain text
- name: "get_last_post_id"
...
variables_to_set:
200: "id"
# if the response is JSON
- name: "get_last_post_info"
variables_to_set:
200:
id: "id"
title: "title"
authorId: "author_info.id"You can access nested fields like this:
"author_info.id"
Any nesting levels are supported.
Example:
- name: Get info with database
method: GET
path: "/info/1"
variables_to_set:
200:
golang_id: query_result.0.0
response:
200: '{"result_id": "1", "query_result": [[ {{ $golang_id }} , "golang"], [2, "gonkey"]]}'
dbQuery: >
SELECT id, name FROM testing_tools WHERE id={{ $golang_id }}
dbResponse:
- '{"id": {{ $golang_id}}, "name": "golang"}'A mock can capture values of the request it receives into variables. Variables become available after the test finishes, so they can be used in subsequent tests.
Use variablesToSet on the mock definition (at the same level as strategy). The values are templates that read the incoming request using the same syntax as the template reply strategy: {{ .request.Json }}, {{ .request.Query "name" }} and {{ .request.Header "name" }}.
Short form:
- name: "create user"
...
mocks:
backend:
strategy: constant
body: '{"ok": true}'
variablesToSet:
capturedUserId: '{{ .request.Json.user.id }}'
capturedPage: '{{ .request.Query "page" }}'
capturedToken: '{{ .request.Header "X-Auth-Token" }}'
- name: "next test can use captured values"
...
request: '{"user_id": "{{ $capturedUserId }}"}'If the mock (more precisely, the definition holding variablesToSet) is called several times within one test, the value of the last call is stored by default. Use the full form with fromCall to keep the value of the first call instead:
No open issues yet, or sync has not completed.