Back to Cheat SheetsLanguage · Beginner

Python cheat sheet

Core syntax, data structures and pytest patterns for test engineers.

Data structures

List / dict / set

xs = [1,2]; d = {"k": 1}; s = {1,2}

Comprehension

[x*2 for x in xs if x > 1]

Unpack

a, *rest = xs

f-string

f"user {name} id={uid}"

pytest

Test

def test_login(): assert page.title == "Home"

Fixture

@pytest.fixture
def user(): yield create_user()

Parametrise

@pytest.mark.parametrize("n", [1,2,3])

Run marked tests

-n needs pytest-xdist

pytest -m smoke -n 4

Files and HTTP

Read JSON

import json; json.load(open("d.json"))

HTTP call

requests.get(url, headers=h).json()

Go beyond the reference

Book a free demo class and apply these commands on live project work.