IO Module¶
The IO module provides steps for file, JSON, CSV and directory operations.
It covers file CRUD, file assertions, JSON 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 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. |
|
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. |
|
Read a file and store its lines as a list in a variable. |
File assertions¶
Pattern |
Description |
|---|---|
|
Assert that a file exists. |
|
Assert that a file does not exist. |
|
Assert that two files have identical content. |
|
Assert that a file’s size is greater than a threshold. |
|
Assert that a file has a specific extension. |
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¶
CSV operations¶
Pattern |
Description |
|---|---|
|
Create a CSV file with a header row. |
|
Create a CSV writer for incremental writing. |
|
Write a row to a CSV file. |
|
Flush the active CSV writer without closing it. |
|
Read a CSV file using a specific row as the header and store rows in a variable. |
|
Close the active CSV writer. |
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
And the file size of "output/logs/test.txt" is greater than 0 bytes
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"
Then the JSON 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 path "$.name" as "config_name"
Then the variable "config_name" equals "myapp"
Scenario: CSV operations
When I create the CSV file "output/data.csv" with header "name,age"
And I write the CSV row "Alice,30" to the file "output/data.csv"
And I write the CSV row "Bob,25" to the file "output/data.csv"
And I save the CSV file
Given I set the header row 1 for the CSV file "output/data.csv" as "rows"
Then the variable "rows" has length 2
API reference¶
See Modules API Reference for the full autodoc reference of the IO module.