DEV Community

Cover image for [Part 7]File I/O and Data Persistence in Python for QA Automation
TestAmplify
TestAmplify

Posted on

[Part 7]File I/O and Data Persistence in Python for QA Automation

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")
Enter fullscreen mode Exit fullscreen mode

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"])
Enter fullscreen mode Exit fullscreen mode

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"])
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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 and shutil 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

Image description

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Postmark Image

"Please fix this..."

Focus on creating stellar experiences without email headaches. Postmark's reliable API and detailed analytics make your transactional emails as polished as your product.

Start free

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️