Getting Started

Generating Tests

Generating Tests

Introduction

In the Writing Tests guide, you learned the basic anatomy of a test using Skyramp in Python. In this section you will learn about how Skyramp saves you time and expedites the testing process by automatically generating the test code you need. The Skyramp Generate functionality uses API schemas that you provide to quickly and automatically create the all the tests you need.

Generate Command

With the skyramp tester generate command, generating tests for various API endpoints is easy. Customize test generation parameters through command-line flags like API specification file, Kubernetes service/Docker alias name, port number, output language, specific endpoint paths, and sample JSON data. Here's an example of generating tests for the REST path /artists:

skyramp tester generate rest --api-schema openapi.json \
            --alias my-service \
            --port 443 \
            --language python \
            --path "/artists" \
            --sample-request

This command yields two Python test files:

Main Test Execution File (my-service_test.py)

This file serves as the primary test execution file, callable in pipelines or test frameworks like Robot. Here’s a peek at its structure:

from tests import (
    artists_test,
)

global_vars={}

failure_list = []

def execute_tests(address="localhost:35142", override_code_path="", global_vars={}, duration=None):
    status_list = []
    status = None
    status = artists_test.execute_tests(address=address, override_code_path=override_code_path, global_vars=global_vars, duration=duration)
    status_list.extend(status)
    print_test_status(test_name="artists", status=status)
    return status_list

def print_test_status(test_name, status):
    if status.passed():
        print(f"Test case { test_name } passed")
    else:
        print(f"Test case { test_name } failed : { status.failed() }")
        failure_list.append(status.failed())

if __name__ == "__main__":
    status = execute_tests(global_vars=global_vars)
    if len(failure_list) > 0:
        print(f"Some test cases failed : {failure_list}")
        exit(1)
    else:
        print("All test cases passed")
        exit(0)

Test Scenario File (artists_test.py)

This file contains test execution for functional and negative scenarios specific to the endpoint path (/artists). Its structure is outlined below:

import skyramp

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

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

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

# Define Scenarios
# ...
# (Scenario definitions omitted for brevity)
# ...

def execute_tests(
    address="localhost:35142",
    override_code_path=None,
    global_vars={"token":"defaultValue"},
    **kwargs
):
    # Test execution logic
    # ...
    # (Test execution logic omitted for brevity)
    # ...

if __name__ == "__main__":
    args = skyramp.parse_args()
    execute_tests(**args)

This file organization enhances modularity and clarity, simplifying test suite management and maintenance.

Note: The use of skyramp tester generate rest here is an example of how to generate tests for a REST service and endpoint. Subcommands for tester generate are available to perform across protocols (gRPC, GraphQL, etc.). For more details on available subcommands and their usage, refer to the tester generate command documentation.

What’s Next

Now that you’ve generated your test files, it’s time to run them and validate your APIs. Check out the Running Tests guide to learn how to execute these test files and ensure your services meet expectations. Running tests helps catch potential issues early on, maintaining the reliability of your applications.

© 2024 Skyramp, Inc. All rights reserved.

© 2024 Skyramp, Inc. All rights reserved.

© 2024 Skyramp, Inc. All rights reserved.