IO Module¶
The IO module provides steps for file, JSON, CSV and directory operations.
It covers file CRUD, file assertions, JSON key-path navigation and
validation, CSV writing and reading, directory management, and reading
files as lines — 38 steps in total, all with es and pt
translations.
Installation¶
The IO module uses only the Python standard library (json, csv,
pathlib). JSON Schema validation requires the jsonschema package:
pip install "behave-steplib[io]"
No extra is needed for basic file, JSON, CSV and directory operations —
the IO module is always available when steplib is installed. Install the
[io] extra to enable JSON Schema validation.
Context¶
The IOContext lives at
context.steplib.io. It holds:
variables— adict[str, Any]for user-defined variables._last_json— the last loaded JSON object for key-path-based assertions._csv_writer/_csv_file— active CSV writer and file handle.
def before_all(context):
context.steplib = autoload(context)
def before_scenario(context, scenario):
context.steplib.reset() # clears variables, closes CSV handles
def after_scenario(context, scenario):
context.steplib.cleanup() # closes any open file handles
Steps¶
File operations¶
Pattern |
Description |
|---|---|
|
Read a text file and store its content as a variable. |
|
Read a file and store its lines as a list in a variable. |
|
Write content to a file (overwrites existing). |
|
Append content to a file. |
|
Delete a file. |
|
Copy a file to a new location. |
|
Move a file to a new location. |
|
Rename a file. |
|
Create an empty file. |
File assertions¶
Pattern |
Description |
|---|---|
|
Assert that a file exists. |
|
Assert that a file does not exist. |
|
Assert that two files have identical content. |
|
Store a file’s size in bytes as a variable. |
|
Store a file’s extension as a variable. |
Directory operations¶
Pattern |
Description |
|---|---|
|
Create a directory, including parents if needed. |
|
Assert that a directory exists. |
|
Assert that a directory does not exist. |
|
List files in a directory and store them as a variable. |
|
Delete a directory and all its contents. |
JSON operations¶
JSON key paths use dot notation (user.name, items.0.id) — not the
$. JSONPath syntax used by the API module.
Pattern |
Description |
|---|---|
|
Load a JSON file into the context’s last JSON object. |
|
Save the last JSON object to a file. |
|
Assert that a JSON key path equals a value. |
|
Store a JSON key path value into a variable. |
|
Update a JSON key path value. |
|
Create a new JSON key path with a value. |
|
Delete a JSON key path. |
|
Assert that a JSON string is valid JSON. |
|
Assert that the last loaded JSON matches a JSON Schema file.
Requires the |
|
Assert that the last loaded JSON is valid by re-serializing it. |
|
Store the type name of a JSON key path value as a variable. |
|
Diff two JSON objects and store the differences as |
|
Merge two stored JSON variables; priority decides which side wins
on conflicts ( |
CSV operations¶
Pattern |
Description |
|---|---|
|
Create a CSV file from a JSON array of objects (keys become the header row). |
|
Create a CSV writer for incremental writing. |
|
Write a row (a JSON object) to the active CSV writer. |
|
Flush the active CSV writer to disk without closing it. |
|
Read a CSV file using a specific row as the header and store the rows in a variable. |
|
Close the active CSV writer and its file handle. |
Example¶
Feature: File and data operations
Scenario: File CRUD
Given I create the directory "output/logs"
When I write "hello world" to the file "output/logs/test.txt"
Then the file "output/logs/test.txt" exists
When I read the file "output/logs/test.txt" as lines into "lines"
Then the variable "lines" has length 1
When I delete the directory "output"
Then the directory "output" does not exist
Scenario: JSON operations
Given I load the JSON file "data/config.json" as the last JSON
Then the JSON key path "version" equals "1.0"
And the JSON matches the schema "schemas/config.json"
And the last JSON is valid
When I store the JSON key path "name" as "config_name"
Then the variable "config_name" equals "myapp"
Scenario: CSV operations
When I create the CSV file "output/data.csv" from the data '[{"name":"Alice","age":30}]'
Given I set the header row 1 for the CSV file "output/data.csv" as "rows"
Then the variable "rows" has length 1
API reference¶
See Modules API Reference for the full autodoc reference of the IO module.