Untitled

 avatar
unknown
plain_text
a month ago
4.2 kB
6
Indexable
STH-Wizard: Automated Test Generation from Swagger
Overview
STH-Wizard is a tool that automatically generates Java controller classes and test methods from Swagger API specifications. It parses Swagger definitions and creates boilerplate code that follows the STH automation framework structure, significantly reducing manual setup time.
Prerequisites

Python 3+ (Python 3.7.1 or newer recommended)
The Python "in-place" module: pip3 install in-place
A valid, well-formed Swagger document for your API
A clone of the sth-template-test project

Installation

Clone the STH-Wizard repository
Install the required Python dependency:

pip3 install in-place


End-to-End Usage Guide
Step 1: Parse the Swagger Document

Place your Swagger JSON file in the swagger-parser/src/main/resources directory
Run the swagger parser:

# Run the SthWizard.java class with the Swagger JSON filename or URL
java -cp [path-to-jar] sth.wizard.SthWizard [filename-or-url]

Find the generated JSON file in the swagger-parser/outputs directory

Step 2: Set Up Project Structure
Clone the sth-template-test project to serve as your base structure:

git clone [repository-url] sth-template-test

Ensure the following directory structure exists within your project:

sth-template-test/
├── src/
│   ├── main/
│   │   └── java/
│   │       └── sth/
│   │           └── template/
│   │               └── restservice/
│   │                   └── models/  # Create this if it doesn't exist
│   └── test/
│       └── java/
│           └── sth/
│               └── template/

Create any missing directories:

mkdir -p sth-template-test/src/main/java/sth/template/restservice/models
mkdir -p sth-template-test/src/test/java/sth/template

Step 3: Generate Code Using STH-Wizard
Navigate to the sth-wizard directory and run the generate.py script:

python3 generate.py -f [path-to-generated-json] -dir [path-to-template-project-directory] -base Template

Example:
python3 generate.py -f outputs/my-api-swagger.json_output.json -dir /path/to/sth-template-test/src/main/java/sth/template/restservice -base Template


Command Parameters

-f: Path to the JSON file generated by the swagger-parser
-dir: Path to the directory where REST controller classes will be generated
-base: Base name for test class generation (typically "Template")

Step 4: Post-Generation Tasks
After successfully generating the code:

Check the project for compiler errors
Search for "TODO" comments that require your attention
Replace placeholder values with actual test data:

Query parameters (e.g., customerId, accountId)
Request bodies


Review and customize the defaultMethod in request body classes
Enhance the generated tests with additional validations as needed

Common Issues and Solutions
"Output path not found" Error
If you see an error like:

ERROR > Output path not found, attempted path: /path/to/directory

Ensure that:

The directory structure exists as specified in the -dir parameter
The /models subdirectory exists in the specified REST service directory
You have write permissions to the target directory

SyntaxWarnings
You may see warnings like:

SyntaxWarning: "is not" with 'str' literal. Did you mean "!="?



These are related to Python syntax but won't prevent the script from running.
Generated Code Structure
Controller Classes

Located in the main source directory
Include methods for each API endpoint
Follow RESTful conventions with proper annotations

Test Classes

Located in the test directory
Include basic tests for each endpoint
Verify 200 OK responses by default
Can be extended for more complex validation

Model Classes

Generated based on request/response schemas in Swagger
Include default methods for creating test data
Located in the models subdirectory

Additional Notes

The generated tests are intentionally basic and should be expanded with more rigorous validation
Query parameters are named according to the Swagger documentation
Request body classes have a defaultMethod that should be customized for your test data
The tool works best with a well-structured Swagger document that follows standard conventions


Editor is loading...
Leave a Comment