Skip to content
TargetQA

Programming for Automation

Python Interview Questions & Answers

Python interview questions for automation testers: core data types, comprehensions, decorators, generators, pytest fixtures and parametrize, and coding problems.

50 questions18 Beginner20 Intermediate12 Advanced

Interview Questions

Showing 15 of 50 questions

  1. Mutable types (list, dict, set) can be changed in place; immutable types (int, float, str, tuple, frozenset) cannot. This matters for test data shared across tests and for dict keys.

  2. A list is an ordered mutable sequence, a tuple is an ordered immutable sequence, a set holds unique unordered items, and a dict maps keys to values. In tests you use lists for element collections, tuples for locators and parametrize rows, sets for uniqueness checks and dicts for payloads.

  3. Comprehensions build lists, dicts or sets in a single readable expression with optional filtering. They're commonly used to extract texts from elements or transform API data.

  4. *args collects extra positional arguments into a tuple and **kwargs collects extra keyword arguments into a dict. They let wrapper utilities pass arguments through to underlying functions.

  5. Use try/except to catch specific exceptions, else for code that runs when nothing failed, and finally for cleanup that always runs. Raise custom exceptions for clear framework errors.

  6. A virtual environment is an isolated Python installation with its own packages, so each project pins its own versions of selenium, pytest and others. It keeps local runs and CI reproducible.

  7. pytest discovers files named test_*.py or *_test.py, functions prefixed test_ and classes prefixed Test without __init__. It uses plain assert with rich failure output, fixtures and a large plugin ecosystem.

  8. Fixtures provide setup and teardown via dependency injection: a test lists the fixture name as a parameter. Code before yield is setup, after yield is teardown, and scope controls lifetime (function, class, module, package, session).

  9. conftest.py is a local plugin file where you define fixtures and hooks that are automatically available to all tests in that directory and below, without imports.

  10. Use @pytest.mark.parametrize to run one test with multiple input sets; each set becomes a separate test case. Data can also come from CSV/JSON files or parametrized fixtures.

  11. A decorator is a function that takes a function and returns a wrapped version, adding behavior without changing its code. In automation it's used for retries, timing, logging and pytest markers/fixtures themselves.

  12. A generator is a function using yield to produce values lazily one at a time, keeping state between calls. It saves memory for large datasets and powers pytest's yield fixtures.

  13. Send HTTP calls with requests (get, post, put, delete), then assert on status_code, headers and response.json(). Use a Session for shared headers/auth and connection reuse.

  14. Use explicit waits with WebDriverWait and expected_conditions to wait for a specific state. Avoid time.sleep and avoid mixing implicit and explicit waits.

  15. Playwright drives Chromium, Firefox and WebKit over a persistent connection, with built-in auto-waiting, web-first assertions, network interception and tracing. pytest-playwright provides a ready page fixture.