Data Module¶
The data module provides generic, cross-module variable management and
environment variable handling. It fills a real gap: each technology module
(api, web, db, kafka) has its own store/variables
namespace, but without generic steps there is no way to share data across
modules or manage environment variables in a scenario-safe way.
The data module stores variables in context.steplib.data.variables and
tracks environment variable modifications so they are automatically restored
after each scenario — 32 steps in total, all with es and pt
translations.
Installation¶
The data module has no mandatory third-party dependencies. It uses only the
Python standard library (json, os, pathlib). YAML file loading
requires PyYAML to be installed separately:
pip install pyyaml
No extra is needed — the data module is always available when steplib is installed.
Context¶
The DataContext lives at
context.steplib.data. It holds:
variables— adict[str, Any]for user-defined variables._env_backup— internal snapshot of environment variables modified during the scenario, so they are restored onreset()/cleanup().
def before_all(context):
context.steplib = autoload(context)
def before_scenario(context, scenario):
context.steplib.reset() # clears variables, restores env vars
def after_scenario(context, scenario):
context.steplib.cleanup() # restores any modified env vars
Steps¶
Variable management¶
Pattern |
Description |
|---|---|
|
Set a generic variable in the data context. |
|
Assert that a variable equals an expected value. |
|
Assert that a variable does not equal a value. |
|
Assert that a variable exists. |
|
Assert that a variable does not exist. |
|
Assert that a variable’s string value contains a substring. |
|
Assert that a variable is empty (empty string, list, dict, or None). |
|
Assert that a variable is not empty. |
|
Assert that a variable has a specific length. |
|
Delete a variable from the data context. |
|
Copy a variable to a new name. |
|
Remove all variables from the data context. |
|
Set a variable to a parsed JSON value. |
|
Assert that a variable’s value matches a regex pattern. |
|
Assert that a variable’s value starts with the given text. |
|
Assert that a variable’s value ends with the given text. |
|
Increment a numeric variable by a given amount. |
|
Assert that a variable’s numeric value is greater than a threshold. |
|
Assert that a variable’s numeric value is less than a threshold. |
File loading¶
Pattern |
Description |
|---|---|
|
Load a YAML file and store the parsed content as a variable. |
|
Load a JSON file and store the parsed content as a variable. |
|
Extract a value from a variable using dot-path navigation
(e.g. |
Environment variables¶
Pattern |
Description |
|---|---|
|
Set an environment variable (restored after the scenario). |
|
Delete an environment variable (restored after the scenario). |
|
Assert that an environment variable equals an expected value. |
|
Assert that an environment variable does not equal a value. |
|
Assert that an environment variable exists. |
|
Assert that an environment variable does not exist. |
|
Store an environment variable’s value into a data variable. |
|
Set an environment variable from a data variable’s value. |
|
Load environment variables from a |
Utility¶
Pattern |
Description |
|---|---|
|
Sleep for a given number of seconds. |
Example¶
Feature: Data management
Scenario: Set and assert variables
Given I set the variable "user_id" to "42"
Then the variable "user_id" equals "42"
And the variable "user_id" exists
And the variable "user_id" has length 2
When I delete the variable "user_id"
Then the variable "user_id" does not exist
Scenario: Regex and string assertions
Given I set the variable "email" to "user@example.com"
Then the variable "email" matches the pattern ".*@.*\..*"
And the variable "email" starts with "user"
And the variable "email" ends with ".com"
Scenario: Numeric operations
Given I set the variable "counter" to "5"
When I increment the variable "counter" by 3
Then the variable "counter" is greater than 7
And the variable "counter" is less than 100
Scenario: Wait
When I wait for 0.5 seconds
Scenario: Load and extract from JSON
Given I load the JSON file "data/user.json" into the variable "user"
When I extract the key path "address.city" from the variable "user" as "city"
Then the variable "city" equals "Berlin"
Scenario: Environment variables
Given I set the environment variable "API_KEY" to "secret123"
Then the environment variable "API_KEY" equals "secret123"
And the environment variable "API_KEY" exists
When I store the environment variable "API_KEY" as "api_key"
Then the variable "api_key" equals "secret123"
Environment variable safety¶
All environment variable modifications (set, delete, load from file) are
tracked in an internal backup. When reset() or cleanup() is called
(typically from before_scenario / after_scenario), the original
values are restored. This prevents test pollution between scenarios.
API reference¶
See Modules API Reference for the full autodoc reference of the data module.