SoftwareTestPilot

Beginner · Certificate included

Python Basics for Test Automation

8 practical lessons in 45 minutes: the Python a tester really needs — variables, conditions, loops, lists and dictionaries, functions, files, JSON and errors, pytest with fixtures and parametrize, and your first API and browser tests with requests and Playwright for Python.

8 lessons45 minCertificate+90 points
01

Lesson 1 of 86 min read

Why Python for testers, and your setup

Python reads almost like English, which makes it the easiest first programming language for many manual testers. It's widely used for test automation (pytest, Selenium, Playwright, requests) and for everyday QA scripts like comparing files or generating test data.

Key takeaways

  1. 1Install Python 3.12+ from python.org (tick 'Add Python to PATH' on Windows). Check with python --version (or python3 --version on Mac/Linux).
  2. 2Use VS Code with the Python extension, or PyCharm Community. Both run and debug scripts with one click.
  3. 3Create a virtual environment per project: python -m venv .venv, then activate it. It keeps each project's libraries separate.
  4. 4Install libraries with pip: pip install pytest requests playwright. Save them with pip freeze > requirements.txt so teammates and CI install the same versions.
  5. 5Indentation is part of the language. Python uses 4 spaces (not braces) to mark blocks; wrong indentation is the most common beginner error.

Setup in 6 commands + first script

Python
mkdir qa-python && cd qa-python
python -m venv .venv
# Windows: .venv\Scripts\activate     Mac/Linux: source .venv/bin/activate
pip install pytest requests
pip freeze > requirements.txt

# hello_qa.py
name = "Riya"
bugs_found = 7
print(f"{name} found {bugs_found} bugs today")

if bugs_found > 5:
    print("Busy day! Remember to retest the fixes.")   # 4 spaces indent

# run it
python hello_qa.py

Go deeper

Expert notes beyond the basics

4 topics
  1. 01

    How Python runs your code

    Python is interpreted: python script.py reads your file top to bottom and runs each line. Errors appear only when that line runs, so a typo in a rarely-used branch can hide until production. This is why tests, a linter (ruff or flake8) and an editor with error hints matter. Python is also dynamically typed: a variable can hold any type, and the type is checked at runtime. Type hints plus a checker like mypy bring some compile-time safety when projects grow.

  2. 02

    Everyday QA scripts beyond test automation

    Even before writing automated tests, Python saves hours: generate 500 unique test users into a CSV; compare two exported reports and list the rows that differ; find every ERROR line in a 2 GB log file; call an API 50 times to create test orders; rename screenshot files; check a list of URLs for broken links (status not 200). These small scripts are great interview stories because they show you automate your own boring work.

  3. 03

    The REPL: try code instantly

    Type python (or python3) with no file to open the interactive shell (REPL). Every line runs immediately, which makes it perfect for experimenting: ' $29.99 '.strip(), float('1,299'.replace(',', '')), [1, 2, 3][-1]. Leave with exit(). In VS Code, select lines and press Shift+Enter to run them. When unsure how something behaves, test it in the REPL in 10 seconds instead of guessing.

  4. 04

    Reading error messages (tracebacks)

    When Python fails it prints a traceback. Read it from the bottom: the last line names the error and reason, for example KeyError: 'email' or TypeError: can only concatenate str (not "int") to str. The lines above show the file and line number where it happened and how you got there. Common beginner errors: IndentationError (mixed tabs/spaces or wrong level), NameError (typo or variable defined later), ModuleNotFoundError (library not installed in the active venv).

Common mistakes to avoid

  • - Installing libraries globally and then getting ModuleNotFoundError because the project's venv isn't active.
  • - Mixing tabs and spaces. Set your editor to insert 4 spaces.
  • - Naming your own file selenium.py, requests.py or test.py, which shadows the real library and causes confusing import errors.
  • - Skipping requirements.txt, so CI or a teammate can't install the same versions.

Try it yourself

Set up a project folder qa-python with a virtual environment, install pytest and requests, save requirements.txt, and write hello_qa.py that prints today's date and 'Environment ready' only if both libraries import successfully.

Write your answer on paper or in a sheet first, then compare.

Interview questions

How to answer like a confident QA engineer

4 questions
  1. Q1

    Why would a team choose Python for test automation?

    Model answer

    Python has simple, readable syntax, so testers become productive quickly and tests are easy to review. It has a strong testing ecosystem: pytest for running tests with fixtures and parametrisation, requests for APIs, Selenium and Playwright for browsers, plus libraries for data, files and databases. It's also used for utility scripts like test data generation. The trade-off is that it's dynamically typed, so we rely on good tests, linters and optionally type hints.

  2. Q2

    Is Python compiled or interpreted?

    Model answer

    Mostly interpreted: CPython compiles source to bytecode (the .pyc files in __pycache__) and then the Python virtual machine runs that bytecode line by line. There's no separate compile step for the developer, and errors in a line appear when it runs.

  3. Q3

    What is pip and what is requirements.txt?

    Model answer

    pip is Python's package installer. requirements.txt lists a project's dependencies with versions (pytest==8.3.2), so anyone can recreate the environment with pip install -r requirements.txt. CI uses it to install exactly the same libraries.

  4. Q4

    What is PEP 8?

    Model answer

    Python's official style guide: 4-space indentation, snake_case for variables and functions, PascalCase for classes, lines around 79-100 characters, and spacing rules. Tools like ruff or black enforce it automatically, which keeps test code consistent across a team.

Knowledge check

+5 points

What is a virtual environment for?

Student reviews

–

Loading…

– enrolled

  • 0%
  • 0%
  • 0%
  • 0%
  • 0%

Sign in and complete 20% of the course to leave a review.

Loading reviews…