Fundamentals 5 min read

Using INI, YAML, and JSON Configuration Files in Python for API Automation

This article explains the INI, YAML, and JSON configuration file formats, provides example files for each, and demonstrates how to parse them in Python using configparser, PyYAML, and the built‑in json module, especially for API automation testing.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using INI, YAML, and JSON Configuration Files in Python for API Automation

In Python development, configuration files are commonly used to store application parameters such as database connections and API keys, with INI, YAML, and JSON being the most popular formats.

INI format stores simple key‑value pairs and typically uses the .ini extension. Example content:

[database]
host = localhost
port = 3306
username = root
password = 123456
[api]
url = https://api.example.com
timeout = 10

Python can parse INI files with the standard configparser module:

import configparser
config = configparser.ConfigParser()
config.read('config.ini')

database_host = config.get('database', 'host')
database_port = config.getint('database', 'port')
database_username = config.get('database', 'username')
database_password = config.get('database', 'password')
api_url = config.get('api', 'url')
api_timeout = config.getint('api', 'timeout')
# Use the configuration parameters for API automation testing
# ...

YAML format is a human‑readable data‑serialization language that supports complex structures like lists and dictionaries, using .yaml or .yml extensions. Example content:

# Configuration file example
database:
  host: localhost
  port: 3306
  username: root
  password: 123456
api:
  url: https://api.example.com
  timeout: 10

Python can parse YAML files with the third‑party PyYAML library:

import yaml
with open('config.yaml', 'r') as f:
    config = yaml.safe_load(f)

database_host = config['database']['host']
database_port = config['database']['port']
database_username = config['database']['username']
database_password = config['database']['password']
api_url = config['api']['url']
api_timeout = config['api']['timeout']
# Use the configuration parameters for API automation testing
# ...

JSON format is a lightweight data‑exchange format that is easy to read and parse, typically using the .json extension. Example content:

{
  "database": {
    "host": "localhost",
    "port": 3306,
    "username": "root",
    "password": "123456"
  },
  "api": {
    "url": "https://api.example.com",
    "timeout": 10
  }
}

Python can parse JSON files with the built‑in json module:

import json
with open('config.json', 'r') as f:
    config = json.load(f)

database_host = config['database']['host']
database_port = config['database']['port']
database_username = config['database']['username']
database_password = config['database']['password']
api_url = config['api']['url']
api_timeout = config['api']['timeout']
# Use the configuration parameters for API automation testing
# ...

In summary, INI is suitable for simple key‑value configurations in small projects, YAML excels at representing complex structures with good readability for medium‑scale projects, and JSON offers broad compatibility for projects of any size. Choose the format that matches your project's needs and use the appropriate Python library to read the configuration for flexible and maintainable API automation testing.

Using configuration files effectively helps manage and adjust test parameters, improving the flexibility and maintainability of automated tests.

JSONyamlConfiguration Files
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.