Introduction
Reading and writing data is a key part of test automation. Whether it’s parsing logs, storing test results, or using CSV and JSON files, this module will help you master Python’s file-handling features.
Lesson 1: Reading and Writing Text Files
Concept:
Text files help store logs, config values, and test data persistently.
Key Topics:
- open(), read(), write(): Basic file operations.
-
File Modes:
"r"
,"w"
,"a"
,"r+"
-
Context Manager: Using
with
to manage file resources.
Example:
with open("results.txt", "w") as file:
file.write("Login Test: Passed\n")
Pro Tip: Always use with
for safer file handling.
Lesson 2: Working with CSV Files
Concept:
CSV files are widely used in data-driven testing to store and read test cases and results.
Key Topics:
- csv.reader and csv.writer: Standard Python CSV tools.
- DictReader and DictWriter: Work with CSV rows as dictionaries.
- Reading Multiple Rows: Looping through test datasets.
Example:
import csv
with open("test_data.csv") as f:
reader = csv.DictReader(f)
for row in reader:
print(row["test_name"], row["status"])
Pro Tip: Use CSV files for quick and readable input data during test case execution.
Lesson 3: Parsing JSON Data
Concept:
JSON is a flexible format used in API automation and configuration files.
Key Topics:
- json.load() / json.dump(): Reading and writing JSON files.
- json.loads() / json.dumps(): Parsing strings in-memory.
- Use Cases in QA: Validating API responses, storing test data.
Example:
import json
with open("config.json") as f:
config = json.load(f)
print(config["username"])
Pro Tip: Keep your JSON schemas clean and predictable for easy debugging.
Lesson 4: Introduction to File System Operations
Concept:
Python’s os
and shutil
modules help manage test directories and files.
Key Topics:
-
Checking File Existence:
os.path.exists()
-
Creating/Deleting Folders:
os.mkdir()
,os.remove()
,shutil.rmtree()
-
Listing Files:
os.listdir()
Example:
import os
if not os.path.exists("logs"):
os.mkdir("logs")
Pro Tip: Use tempfile
for safe temporary file storage during test runs.
Lesson 5: Best Practices for Handling Large Test Data Files
Concept:
Efficient file I/O becomes important when dealing with large datasets.
Key Topics:
- Reading in Chunks: Avoid memory overload.
- File Compression: Using gzip.
- Moving to Databases: When files become unwieldy.
Example:
with open("large_file.txt") as f:
for line in f:
process(line)
Pro Tip: Always close large files or use with
to manage memory efficiently.
Conclusion
Python provides powerful tools to handle file I/O, making it easy to store, retrieve, and manipulate test data.
Key Takeaways:
- Use text files for logs and results.
- Use CSV for readable, structured data inputs.
- Use JSON for config and API test data.
- Use
os
andshutil
to organize automation directories and output.
What’s Next?
In the final module, we’ll explore Python for Selenium: Preparation for UI Automation, where you’ll get hands-on with browser automation using Python.
Visit us at Testamplify | X | Instagram | LinkedIn
Top comments (0)