Getting Started

Writing Tests

Writing Tests

Introduction

In Skyramp, writing tests is easy. Using a simple programming style, you can define test scenarios, endpoints, and assertions effortlessly. Skyramp makes testing straightforward, helping you ensure your applications and services work reliably.

The following section describes the basic anatomy of a test so that you can manually create your own and take advantage of Skyramp's powerful automatic test generation capabilities described in the next section.

For the purpose of the upcoming guides, we will use Smartbear's "Simple Artist API" as an example. The Simple Artist API is a simple service that allows consumers to obtain a list of stored artists and add new artists to a database. Below is a snippet of the OpenAPI spec:

openapi: 3.0.0
info:
  version: 1.0.0
  title: Simple Artist API
  description: A simple API to illustrate OpenAPI concepts

servers:
  - url: https://example.io/v1

components:
  securitySchemes:
    BasicAuth:
      type: http
      scheme: basic
security:
  - BasicAuth: []

paths:
  /artists:
    get:
     description

1. Initializing Endpoints

To commence testing API services with Skyramp, you first need to define endpoint objects. These objects encapsulate the details of the API endpoint you want to test, such as its name, port, and relevant path. Here’s an example of initializing an endpoint for the Artist REST service:

import skyramp

# Initialize Endpoint(s)
artists_endpoint = skyramp.RestEndpoint(
    name="service",
    port=443,
    rest_path="/artists"
)

2. Defining Request Objects

Once endpoints are initialized, you can define request objects, representing requests to be made to the specified endpoints. This includes specifying request methods, payloads, and other relevant details.

# Create JSON Payload Constants
artists_request_data = r'''{
  "name": "John Doe",
  "genre": "Rock"
}
'''

# Define Request Objects
artists_POST_request = skyramp.Request(
    name="artists_POST",
    method_name="POST",
    endpoint_descriptor=artists_endpoint,
    vars_={},
    blob=artists_request_data
)

3. Constructing Test Scenarios

Test scenarios define a series of steps to be executed during testing. These steps involve making requests defined in the previous step and asserting expected outcomes.

# Define Scenarios
def add_artists_functional_scenario():
    scenario = skyramp.Scenario(
        "artists_functional_scenario",
        ignore=False
    )
    # Endpoint /artists and Method POST
    scenario.add_request_v1(
        request=artists_POST_request,
        step_name="artists_functional_scenario-0",
        vars_override={},
        description="Endpoint /artists and Method POST"
    )
    # Assert of scenario step artists_functional_scenario-0 - Endpoint /artists and Method POST
    scenario.add_assert_v1(
        assert_value="requests.artists_POST.code",
		assert_expected_value="201", 
        assert_step_name="artists_functional_scenario-1",
        description="Assert of scenario step artists_functional_scenario-0 - Endpoint /artists and Method POST"
    )
    return scenario

4. Configuring and Executing Tests

Test configuration logic orchestrates the execution of defined scenarios. This involves configuring various parameters such as address, test name, and execution environment. You can find more detailed instructions in the Test Configuration section.

def execute_tests(address="localhost:35142", global_vars={}, **kwargs):
    docker_client = skyramp.DockerClient()
    scenarios = [add_artists_functional_scenario()]

    status = docker_client.tester_start_v1(
        scenario=scenarios,
        test_name="artists test",
        address=address,
        blocked=True,
        global_vars=global_vars,
        override_code_path=kwargs.get("override_code_path", None),
        skip_verify=kwargs.get("skip_verify", False),
        blobs=kwargs.get("blobs", {}),
        duration=kwargs.get("duration", None),
        at_once=kwargs.get("at_once", None),
        count=kwargs.get("count", None),
        is_formatting_enabled=True
    )
    return status

Missing a dependency?

Skyramp makes it easy for you to quickly mock an internal or external API - saving you time so that you can focus on writing software instead of managing dependencies. You can find detailed instructions and examples here:

What's Next

Instead of writing tests by hand, you can further streamline your testing process by dynamically generating tests leveraging the Skyramp terminal client’s tester generate command. This allows you to automate test case creation, reducing manual effort and ensuring thorough test coverage. Learn more about generating tests here.

© 2024 Skyramp, Inc. All rights reserved.

© 2024 Skyramp, Inc. All rights reserved.

© 2024 Skyramp, Inc. All rights reserved.