Python for QA โ
Python is the QA's Swiss army knife: test data scripts, internal tools, results analysis and, with pytest and requests, a complete and very readable testing stack. Even if your main suite is Java or TypeScript, Python shows up sooner or later.
pytest in five minutes โ
A test is a function that starts with test_ and uses plain assert:
python
def test_active_order():
order = create_service_order(status="active")
assert order.status == "active"The three pieces worth knowing:
- Fixtures โ reusable setup and teardown, injected by parameter name:
python
import pytest
@pytest.fixture
def api_client():
client = ApiClient(base_url=BASE_URL)
yield client # everything after the yield is the teardown
client.close()
def test_health(api_client):
assert api_client.get("/health").status_code == 200- Parametrize โ the same test over several inputs, ideal for partitions and boundary values:
python
@pytest.mark.parametrize("msisdn", ["12345", "abcdefghi", "+34-600", ""])
def test_invalid_msisdn_rejected(api_client, msisdn):
r = api_client.post("/service-orders", json={"productId": "mobile-20gb", "msisdn": msisdn})
assert r.status_code == 400- Markers (
@pytest.mark.smoke) โ labeling and filtering execution (pytest -m smoke), like tags in other frameworks.
requests for APIs โ
python
import requests
order = {"customerId": "C-100", "productId": "fiber-1gbps"}
r = requests.post(f"{BASE_URL}/service-orders", json=order, timeout=10)
assert r.status_code == 201
assert r.json()["status"] == "created"With pytest + requests + jsonschema you get the lightweight equivalent of REST Assured: the anatomy of an API test is the same, only the syntax changes.
What else I use it for โ
- Generating test data (files, bulk payloads, random data with
faker). - Support scripts: cleaning environments, comparing responses between environments, processing logs or results CSVs.
- Internal tools: in my case, the sharding optimization model that later became CI Shard Advisor started as a Python script.
When to pick Python as the suite's language โ
| Pick Python ifโฆ | Pick Java/TS ifโฆ |
|---|---|
| The team already speaks it (data, scripts, Python backend) | The suite lives next to Java/TS product code |
| You prioritize readability and writing speed | You want the product stack's typing and tooling |
| Testing is mostly APIs and data | There's a lot of browser E2E (Playwright TS is first-class) |
Key idea
Frameworks change, concepts don't: fixtures are setup/teardown, parametrize is data-driven and markers are tags. If you master those ideas in one stack, in Python you're only missing the syntax โ and it's the friendliest one to learn.