Configuration, Environments, Secrets and Test Data
Do Not Hard-Code
A framework must run against different environments (dev, test, staging) with different URLs and users. Never put URLs, passwords or browser names in test code.
A Config File
json
// config/config.json
{
"dev": { "base_url": "https://dev.example.com", "timeout": 15 },
"staging": { "base_url": "https://staging.example.com", "timeout": 10 },
"demo": { "base_url": "https://www.saucedemo.com", "timeout": 10 }
}
python
# utils/config.py
import json, os
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
def load_config(env: str | None = None) -> dict:
env = env or os.getenv("TEST_ENV", "demo")
with open(ROOT / "config" / "config.json", encoding="utf-8") as f:
data = json.load(f)
if env not in data:
raise KeyError(f"Unknown environment '{env}'. Choose from {list(data)}")
return {**data[env], "env": env}
Secrets with Environment Variables
User names are sometimes fine in files, but passwords and tokens never go into Git. Use environment variables or a .env file that is ignored by Git.
bash
pip install python-dotenv
text
# .env (add to .gitignore!)
TEST_ENV=demo
APP_USER=standard_user
APP_PASSWORD=secret_sauce
python
# conftest.py
import os, pytest
from dotenv import load_dotenv
from utils.config import load_config
load_dotenv()
@pytest.fixture(scope="session")
def config():
return load_config()
@pytest.fixture(scope="session")
def credentials():
return {
"username": os.environ["APP_USER"],
"password": os.environ["APP_PASSWORD"],
}
text
# .gitignore
.venv/
.env
__pycache__/
reports/
screenshots/
downloads/
allure-results/
Use Config in Tests
python
def test_open_home(driver, config):
driver.get(config["base_url"])
assert driver.title
bash
TEST_ENV=staging pytest --browser=firefox --headless
pytest.ini Defaults
ini
[pytest]
testpaths = tests
addopts = -v --strict-markers --html=reports/report.html --self-contained-html
markers =
smoke: quick checks
regression: full suite
log_cli = true
log_cli_level = INFO
Test Data Builders
python
# utils/data.py
from dataclasses import dataclass
import uuid
@dataclass
class User:
first_name: str = "Ana"
last_name: str = "Silva"
email: str = ""
zip_code: str = "12345"
@staticmethod
def random():
return User(email=f"ana.{uuid.uuid4().hex[:8]}@example.com")
# test
user = User.random()
| What | Where |
|---|---|
| URLs, timeouts, browser defaults | config/config.json and pytest options |
| Passwords, tokens | Environment variables or a secret manager |
| Test data tables | test_data/*.csv or *.json |
| Generated data | Builders and Faker |
Never commit secrets: If a password reaches Git history, treat it as leaked and change it. CI systems provide secret storage (GitHub Secrets, Jenkins credentials).
Conclusion
Keep environment settings in a config file, secrets in environment variables, test data in files or builders and choose all of it at run time with TEST_ENV and pytest options.
Quick Summary: config.json + env vars + .env (ignored) + pytest options.

