Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 35 ยท Test Structure

Test Data and Configuration

Keep Selenium tests flexible: configuration files, environment URLs, credentials from environment variables, and data-driven tests fed from CSV and JSON files.

A test that has https://staging.shop.example and [email protected] / s3cret! typed into its code works exactly once: on the machine and environment its author had in mind. Move it to a different server, change the password, or add a new case, and you must edit code. Worse, if that code is committed with a real password, the secret is now in your repository history forever.

The cure is to separate three things from the test logic: configuration (which browser, which URL, which timeout), secrets (credentials, tokens), and test data (the inputs and expected results of each case). In this lesson you will learn where each belongs, how to layer them so that one test run works against local, staging and CI, and how to feed a data-driven test from a CSV or JSON file.

The Selenium code in this lesson is read-only, since a real browser cannot run here. The runnable examples are real Python that runs in your browser: they parse configuration and CSV text, read environment variables and loop over cases. Only the browser part is replaced by a small fake login function.

What goes where

Three kinds of settings

Configuration Browser, base URL, timeouts. Committed to git.
Secrets Passwords and tokens. Never in git.
Test data Inputs and expected results, one row per case.

Code changes rarely; these three change often.

1 / 2

A configuration file

Put non-secret settings in a plain file next to your tests: INI, JSON, YAML or TOML all work. Python's standard library reads INI (configparser), JSON (json) and TOML (tomllib) without extra installs.

INI
# Read-only: config.ini
[default]
base_url = http://localhost:8000
browser = chrome
headless = false
timeout = 10

[staging]
base_url = https://staging.shop.example
headless = true

The [staging] section only lists what differs from the defaults. A small loader reads the default section, applies the section for the chosen environment, and exposes a single object that the rest of the framework uses. Tests and page objects then ask the config for base_url and never hard-code a URL.

Python
# Read-only: a typical Selenium test using the config
def test_home_page(driver, config):
    driver.get(config.base_url + "/")
    assert "Shop" in driver.title

Secrets belong in environment variables

Credentials must not live in files that you commit. The usual way to hand them to a test is an environment variable: a named value that the operating system, or a CI system's secret store, provides to the process.

Terminal
# Read-only: setting secrets for one terminal session
export SHOP_USER="[email protected]"
export SHOP_PASSWORD="read-it-from-your-password-manager"
pytest
Python
# Read-only: reading them in a fixture
import os

import pytest


@pytest.fixture
def credentials():
    user = os.environ.get("SHOP_USER")
    password = os.environ.get("SHOP_PASSWORD")
    if not user or not password:
        pytest.skip("SHOP_USER and SHOP_PASSWORD are not set")
    return user, password

A few rules keep secrets safe.

  • Use a dedicated test account with the least privileges that the tests need, never a real person's login or an admin.
  • If you keep a local .env file for convenience, add it to .gitignore and commit a .env.example with fake values instead.
  • Never print a password in logs or in an assertion message. A failure report is often posted in public channels.
  • In CI, store credentials in the platform's encrypted secrets (for example GitHub Actions secrets) and expose them as environment variables for the job only. Running in CI shows where that fits, and Security, Ethics and Scraping Limits covers more.

Layering: defaults, file, environment

A robust framework resolves each setting from several sources, from weakest to strongest: built-in defaults, then the config file, then environment variables, then command-line options. The strongest source that sets a value wins. That lets a developer run tests on a laptop with no setup, and lets CI change the URL with one variable.

Python Layered configuration

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The local run falls back on the defaults. The staging run changes two values. The CI run keeps staging's headless but overrides the URL and the timeout through variables, without anyone editing a file. Type conversion (int, boolean) happens in one place, so tests never compare against the string "false" by accident, which is a classic bug: the string "false" is truthy in Python.

Environment variable values are always strings. If you read HEADLESS=false and write if os.environ["HEADLESS"]:, the condition is true, because "false" is a non-empty string. Convert explicitly, as in the loader above.

Data-driven tests

A data-driven test runs the same steps over many rows of data. The logic is written once; adding a case means adding a row, which a non-programmer can do. Rows usually live in CSV (spreadsheet-friendly) or JSON (nested data).

TEXT
# Read-only: data/login_cases.csv
case_id,email,password,expected
valid,[email protected],s3cret!,Dashboard
bad_password,[email protected],wrong,Wrong email or password
empty_email,,s3cret!,Email is required
empty_password,[email protected],,Password is required

With pytest, a small helper reads the file and feeds parametrize.

Python
# Read-only: loading CSV rows into parametrize
import csv
from pathlib import Path

import pytest


def load_cases(name):
    path = Path(__file__).parent / "data" / name
    with path.open(newline="", encoding="utf-8") as handle:
        return [pytest.param(row["email"], row["password"], row["expected"], id=row["case_id"])
                for row in csv.DictReader(handle)]


@pytest.mark.parametrize("email, password, expected", load_cases("login_cases.csv"))
def test_login(driver, email, password, expected):
    page = LoginPage(driver).open().submit_credentials(email, password)
    assert page.message() == expected

csv.DictReader returns each row as a dictionary keyed by the header row. The id= argument gives each case a readable name in the report, such as test_login[bad_password]. JSON works the same way with json.load, and suits cases that need lists or nested objects (for example a whole shopping cart per case).

The next example parses CSV text and a JSON list, runs each row through a fake login, and prints one line per case. It skips blank lines and rows whose case_id starts with #, a convention that lets you disable a row without deleting it.

Python A data-driven loop over CSV and JSON

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All six cases pass: four from the CSV (the commented-out row was skipped) and two from JSON. To add a case, you edit the data, not the loop. Notice that the data covers edge cases a hand-written test would likely forget: a mistyped address and an address that is only spaces. Good test data files grow like a checklist.

Practical guidance

  • Keep data next to the tests (for example data/), in version control, with clear column names.
  • Use readable case ids so a failing report tells you what broke without opening the file.
  • Separate input from expectation. Include the expected result as a column rather than computing it with the same logic that the application uses; otherwise a bug in the app is repeated in the test.
  • Generate unique values when tests create records. An email like qa+{timestamp}@example.com keeps parallel runs from colliding.
  • Do not put data in the page objects. Page objects hold locators and actions. Data comes from files, fixtures or arguments. See The Page Object Model.
  • Reset state. A test that registers a user should clean up (or use a fresh user), so the next run starts clean. The shape of the fixture that does it is in Selenium with pytest.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where is the safest place to keep a test account's password?

    Environment variables and CI secrets keep the value out of the repository. Anything committed stays in git history even after you delete it.
  2. 2. You read HEADLESS from the environment and it equals "false". What does if os.environ["HEADLESS"]: do?

    Environment variables are strings, and the string "false" is non-empty. Convert it explicitly with a comparison such as value.lower() == "true".
  3. 3. In a layered configuration, which source should win if the file and an environment variable both set base_url?

    Stronger, more specific sources override weaker ones. This lets CI change a setting without editing any committed file.
  4. 4. What is the main advantage of a data-driven test?

    The steps are written once and the data decides the cases, so extending coverage is cheap and does not touch the logic.

Summary

  • Keep configuration, secrets and test data out of the test logic.
  • Put safe settings in a committed file, and let environment variables and command-line options override them.
  • Give credentials to tests only through environment variables or CI secrets, use dedicated test accounts, and keep .env files out of git.
  • Convert types in one place; environment values are always strings.
  • Feed tests from CSV or JSON rows with readable case ids, and keep expected results in the data.

Next: What Is New in Selenium 4.