Requests (Python)
Overview
The python-requests plugin generates Python code using the Requests library.
within API (plugin: python-requests, type: api) GET "https://api.example.com/users"Types
api
The main API context for making HTTP requests.
Modifiers:
| Modifier | Type | Description |
|---|---|---|
base | quoted | Base URL for all requests |
timeout | quoted | Request timeout |
headers | quoted | Default headers for requests |
Patterns:
| Pattern | Description |
|---|---|
GET <path> | HTTP GET request |
POST <path> body <body> | HTTP POST request with JSON body |
PUT <path> body <body> | HTTP PUT request with JSON body |
DELETE <path> | HTTP DELETE request |
expect failure | Expect following actions to fail (modifiers: error, capture) |
http_response
Response assertions returned by HTTP request patterns.
Patterns:
| Pattern | Description |
|---|---|
expect status <code> | Assert status code |
expect json <path> == <value> | Assert JSON value at path equals value |
failure_block
Child context for expect failure blocks. Accepts passthrough patterns from parent context.
Example
Vanya test scenario (vanya-lang.org/v0.1)
Script title: "Create and fetch user"
Within the API (plugin: python-requests, type: api, base: "https://api.example.com") POST "/users" body '{"name": "Alice"}' expect status 201
GET "/users/1" expect status 200 expect json "$.name" == "Alice"Error Handling Example
Vanya test scenario (vanya-lang.org/v0.1)
Script title: "Test error handling"
Within the API (plugin: python-requests, type: api, base: "https://api.example.com") expect failure (error: "requests.exceptions.HTTPError") GET "/nonexistent"More Examples
Complete CRUD Operations
Vanya test scenario (vanya-lang.org/v0.1)
Script title: "user_crud"
Within the API (plugin: python-requests, type: api, base: 'http://localhost:8080') # Create POST "/users" body '{"name": "alice", "email": "alice@example.com"}' expect status 201 expect json "$.id" == "1" Capture "$.id" as userId
# Read GET "/users/{userId}" expect status 200 expect json "$.name" == "alice"
# Update PUT "/users/{userId}" body '{"name": "alice_updated"}' expect status 200
# Delete DELETE "/users/{userId}" expect status 204
Export userIdUsing Captured Variables
Vanya test scenario (vanya-lang.org/v0.1)
Script title: "chained_requests"
Within the API (plugin: python-requests, type: api, base: 'http://localhost:8080') POST "/auth/login" body '{"username": "admin", "password": "secret"}' expect status 200 Capture "$.token" as authToken
# Use captured token in subsequent requests GET "/users" expect status 200
Export authTokenHealth Check Pattern
Vanya test scenario (vanya-lang.org/v0.1)
Script title: "health_check"
Within the API (plugin: python-requests, type: api, base: 'http://localhost:8080') GET "/health" expect status 200
GET "/ready" expect status 200