Python Foundations
Why Python Rules Test Automation — and Setting It Up Right
Why Python Rules Test Automation — and Setting It Up Right
Walk into almost any QA team today and you'll find Python somewhere: pytest suites, Playwright scripts, API checks, data-seeding jobs, log parsers, the one-off script someone wrote to clean up 4,000 test accounts. Python didn't win because it's the fastest language — it isn't. It won because it gets out of your way. A test engineer can go from "I need to check every product endpoint returns 200" to a working script in ten minutes, and someone else can read that script six months later without a decoder ring.
What Makes Python a Good Fit for QA Work
- Readable by default. Indentation is syntax, so code has to be laid out cleanly. Reviewers spend their time on logic, not brace-matching.
- Batteries included. JSON, CSV, HTTP basics, regex, dates, file paths, subprocesses, logging — all in the standard library, no installs.
- The ecosystem is enormous.
pytest,requests,httpx,playwright,selenium,pandas,faker— every testing need has a mature, well-documented library. - It's the language of AI tooling. If you ever want to test or build anything with LLMs (see the Python for AI path), Python is where the SDKs land first.
The trade-off is speed of execution and some looseness with types. Both are manageable, and later lessons cover exactly how (type hints in Lesson 13, profiling in Lesson 26).
Installing Python Without Making a Mess
The single most common beginner pain is a broken Python install: three versions on one machine, pip install putting packages somewhere unexpected, and a script that works in the terminal but not in the IDE. Avoid it from day one:
- Install a current Python 3 (3.12 or newer) from python.org, or via your OS package manager (
brew install pythonon macOS). Never use the system Python that ships with the OS for your own projects — the OS depends on it. - Check what you got:
python3 --version
# Python 3.12.4
- Never install project packages globally. Use a virtual environment per project.
Virtual Environments: One Project, One Sandbox
A virtual environment is just a folder holding its own Python interpreter link and its own installed packages. Project A can use requests 2.31 while Project B pins requests 2.28, and neither knows the other exists.
cd my-test-project
python3 -m venv .venv # create it
source .venv/bin/activate # macOS/Linux
# .venv\Scripts\activate # Windows
pip install pytest requests # installs INTO .venv only
pip freeze > requirements.txt # record exact versions
When the venv is active, your prompt shows (.venv) and python points at the sandboxed interpreter. Add .venv/ to .gitignore — you commit requirements.txt, never the environment itself. A teammate rebuilds it with pip install -r requirements.txt.
The Faster Modern Option: uv
uv is a newer, extremely fast tool that replaces pip, venv, and version management in one binary. Many teams have switched because installs that took a minute take a couple of seconds:
uv init my-test-project # creates pyproject.toml
cd my-test-project
uv add pytest requests # creates .venv and installs
uv run pytest # runs inside the env, no activate needed
Either approach is fine. What matters is the habit: every project gets its own environment, and dependencies are written down.
Your First Real Script
Here's something genuinely useful — a smoke check you could run against a staging site:
import urllib.request
URLS = [
"https://example.com/",
"https://example.com/login",
]
for url in URLS:
with urllib.request.urlopen(url, timeout=10) as response:
print(f"{response.status} {url}")
Save it as smoke.py and run python smoke.py. No framework, no build step. That immediacy is Python's superpower for testers — and the rest of this path is about growing that ten-line script into code you'd be proud to ship.
Choosing an Editor
VS Code with the official Python extension, or PyCharm Community, both work well. Whichever you choose, point it at your project's .venv interpreter — that one setting fixes most "it works in the terminal but not in my IDE" mysteries.
💬 Discussion
What is the most repetitive manual check in your current testing work that a 20-line Python script could take over?
Why should each Python project have its own virtual environment?
Next Lesson
Variables, Types, and How Python Really Handles Values