Variables and Data¶
This chapter covers variable management, data extraction from responses, and using RESTest's powerful path parser for accessing nested data structures.
Variable Basics¶
RESTest uses Python's string formatting syntax %(variable_name)s
for variable references.
Setting Variables¶
There are several ways to set variables:
1. Command Line¶
2. Using the Set Action¶
3. Extracting from Responses¶
{
"method": "post",
"url": "/auth/login",
"fields": [
["token", "auth_token"],
["user.id", "user_id"]
]
}
Variable Scope¶
Variables in RESTest are: - Global across all test files - Persistent throughout test execution - Case-sensitive - String-based (numbers are converted to strings)
Data Extraction¶
Basic Field Extraction¶
{
"method": "get",
"url": "/users/1",
"fields": [
"id", // Same name extraction (a new variable named 'id' is created)
["email", "user_email"] // Renamed extraction (a new variable named 'user_email' is created from 'email')
]
}
Nested Data Extraction¶
Array Data Extraction¶
Path Parser¶
RESTest's path parser provides powerful ways to access nested data.
Basic Path Notation¶
Direct Field Access¶
{
"fields": [
"id", // Root level field
"user.name", // Nested field
"deeply.nested.value" // Multiple levels
]
}
Array Access¶
{
"fields": [
"[0]", // First element of root array
"items[0]", // First element of items array
"items[1].name" // Name of second item
]
}
Conditional Access¶
{
"fields": [
"users.[status=active]", // First active user
"items.[price>100].name", // Names of expensive items
"data.[type!=deleted].id" // IDs of non-deleted items
]
}
Advanced Path Parser Examples¶
Multiple Conditions¶
{
"actions": [
{
"method": "get",
"url": "/api/search",
"fields": [
["results.[type=user].[status=active].name", "active_users"],
["results.[price>=100].[stock>0].id", "available_premium_items"]
]
}
]
}
Nested Array Navigation¶
{
"fields": [
["categories[0].products[0].variants[0].sku", "first_sku"],
["orders.[status=completed].items.[quantity>1].product_id", "bulk_order_products"]
]
}
Complete Examples¶
User Authentication Flow¶
{
"actions": [
{
"title": "Set API Version",
"action": "set",
"key": "api_version",
"value": "v2"
},
{
"title": "Login",
"method": "post",
"url": "/api/%(api_version)s/auth/login",
"params": {
"email": "user@example.com",
"password": "secret123"
},
"fields": [
["token", "auth_token"],
["user.id", "user_id"],
["user.permissions", "user_perms"]
]
},
{
"title": "Get User Profile",
"method": "get",
"url": "/api/%(api_version)s/users/%(user_id)s",
"headers": {
"Authorization": "Bearer %(auth_token)s"
},
"fields": [
["profile.settings.*", "user_settings"],
["profile.preferences", "user_prefs"]
]
}
]
}
Complex Data Processing¶
{
"actions": [
{
"title": "Search Products",
"method": "get",
"url": "/api/products/search",
"params": {
"category": "electronics",
"min_price": 100
},
"fields": [
["results.[price>=500].[stock>0].id", "premium_products"],
["facets.categories.[count>10].name", "popular_categories"],
["metadata.total_count", "total_results"]
],
"tests": [
{
"field": "results.[price>=500]",
"mode": "SIZE-GT",
"value": 0
}
]
},
{
"title": "Process Premium Products",
"method": "post",
"url": "/api/batch/process",
"params": {
"product_ids": "%(premium_products)s",
"action": "discount",
"amount": 10
}
}
]
}
Environment Variables¶
Loading Environment Variables¶
Environment File Example¶
Saving Variables¶
Variable Manipulation¶
String Concatenation¶
Using in URLs¶
Headers and Authentication¶
Best Practices¶
- Naming Conventions
- Use descriptive variable names
- Follow a consistent naming pattern
-
Indicate variable type in name when useful
-
Data Extraction
- Extract only needed data
- Use meaningful variable names
-
Document expected data structures
-
Path Parser
- Use appropriate conditions
- Handle missing data gracefully
-
Test complex paths separately
-
Variable Management
- Clean up temporary variables
- Document required variables
- Use environment variables for sensitive data
Troubleshooting¶
Common Issues¶
-
Variable Not Found
-
Wrong Path Syntax
-
Missing Variable Type Specifier