-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL07_testing_and_quality.py
More file actions
603 lines (540 loc) · 22.6 KB
/
Copy pathL07_testing_and_quality.py
File metadata and controls
603 lines (540 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# ============================================================
# L07: Testing and Code Quality in Python
# ============================================================
# WHAT: Comprehensive coverage of pytest, mocking, property-based
# testing, async testing, and coverage analysis.
# WHY: Tests are the only thing that lets you change code
# confidently. Without them, every refactor is a gamble.
# Good tests also force good design — testable code is
# almost always better-structured code.
# LEVEL: Advanced
# ============================================================
"""
CONCEPT OVERVIEW:
Python's testing ecosystem centers on pytest — a framework that
replaced unittest as the industry standard because of its simpler
syntax (plain assert, not assertEqual), powerful fixture system,
and rich plugin ecosystem.
Testing hierarchy:
Unit tests → test one function/class in isolation (mocked deps)
Integration tests → test multiple real components together
End-to-end tests → test the full system (real DB, real HTTP)
The key architectural decision is WHERE to draw the mock boundary.
Mock too little → tests are slow and brittle (real DB required).
Mock too much → tests pass even when real integrations are broken.
PRODUCTION USE CASE:
A payment service with 2000 unit tests, 200 integration tests,
and 20 E2E tests. The 2000 unit tests run in 3 seconds (all mocked).
The 200 integration tests run in 30 seconds (real SQLite/Postgres).
The 20 E2E tests run in 5 minutes (deployed to staging).
CI gates on all three layers before merging to main.
COMMON MISTAKES:
- Using assertEqual/assertTrue instead of plain assert in pytest
- Fixtures that are too large / do too much (test setup should be obvious)
- Mocking the thing under test (you're testing your mock, not your code)
- Not using parametrize (copy-pasting test functions for variants)
- Ignoring branch coverage — line coverage of 90% can hide uncovered paths
- async tests that accidentally run synchronously (no @pytest.mark.asyncio)
"""
# ============================================================
# SECTION 1: pytest basics
# ============================================================
# Test discovery rules (no configuration needed):
# - Files matching test_*.py or *_test.py in the current directory tree
# - Functions starting with test_ inside those files
# - Classes starting with Test (no __init__) with methods starting with test_
#
# pytest uses PLAIN ASSERT — not unittest's assertEqual/assertTrue.
# The framework rewrites assert statements at import time (assertion
# rewriting) so you get detailed failure messages automatically.
#
# Run tests:
# pytest → discover and run all tests
# pytest tests/test_user.py → specific file
# pytest -k "test_create" → keyword filter (matches test names)
# pytest -x → stop on first failure
# pytest -v → verbose (print each test name)
# pytest --tb=short → shorter traceback format
# Example test file structure:
# def test_addition():
# result = 1 + 1
# assert result == 2 # plain assert — pytest rewrites this
# assert result != 3
#
# def test_exception():
# with pytest.raises(ValueError, match="invalid"):
# int("not_a_number") # match= is a regex on the exception message
#
# def test_approx_float():
# assert 0.1 + 0.2 == pytest.approx(0.3) # floating point comparison
# ============================================================
# SECTION 2: pytest fixtures
# ============================================================
# Fixtures are pytest's dependency injection system.
# A test function declares what it needs as PARAMETERS,
# and pytest resolves and provides them automatically.
#
# Scope controls how often the fixture is created:
# function → fresh instance per test (default, safest)
# class → shared within a test class
# module → shared within a test file
# session → shared across the entire test session
#
# Use the narrowest scope that works. Sharing mutable state
# between tests causes mysterious failures ("test order dependency").
#
# conftest.py: a special pytest file. Fixtures defined here are
# automatically available to all tests in the same directory and below.
# No import needed — pytest discovers conftest.py automatically.
# ---- conftest.py (would live at tests/conftest.py) ----
#
# import pytest
# import sqlite3
#
# @pytest.fixture(scope="function")
# def in_memory_db():
# """
# Provides a fresh SQLite in-memory database for each test.
# The database is automatically destroyed when the test ends
# because we use 'yield' — code after yield is teardown.
# """
# conn = sqlite3.connect(":memory:")
# conn.execute("""
# CREATE TABLE users (
# id INTEGER PRIMARY KEY,
# email TEXT UNIQUE NOT NULL,
# name TEXT NOT NULL
# )
# """)
# conn.commit()
#
# yield conn # ← test receives this value
#
# conn.close() # ← always runs, even if test fails
#
# @pytest.fixture(scope="session")
# def app_config():
# """Session-scoped: read config once for all tests."""
# return {"db_url": "sqlite:///:memory:", "debug": True}
# ============================================================
# SECTION 3: parametrize — data-driven tests
# ============================================================
# Instead of writing one test function per case (copy-paste),
# parametrize generates N test instances from a list of inputs.
# Each instance gets its own pass/fail status in the report.
#
# Benefits:
# - Adding a new test case = adding one tuple to the list
# - Failures show exactly which parameter set failed
# - pytest -k "test_validate[empty]" runs just that case
# import pytest
#
# @pytest.mark.parametrize("email, expected_valid", [
# ("user@example.com", True), # normal case
# ("bad-email", False), # missing @
# ("", False), # empty string
# ("a@b.c", True), # short but valid
# ("user@.com", False), # missing domain name
# ])
# def test_email_validation(email, expected_valid):
# assert validate_email(email) == expected_valid
#
# # Multiple parameter sets with ids for readable output:
# @pytest.mark.parametrize("a,b,result", [
# (1, 2, 3),
# (0, 0, 0),
# (-1, 1, 0),
# ], ids=["positive", "zeros", "negative"])
# def test_add(a, b, result):
# assert add(a, b) == result
# ============================================================
# SECTION 4: marks — tagging and controlling test execution
# ============================================================
# Marks attach metadata to tests. Built-in marks:
# @pytest.mark.skip(reason="...") → always skip
# @pytest.mark.skipif(condition, reason) → conditional skip
# @pytest.mark.xfail(reason="...") → expected to fail (passes if it fails)
# @pytest.mark.xfail(strict=True) → fail the suite if it PASSES (used to
# track bugs that get fixed unexpectedly)
#
# Custom marks: register in pytest.ini to avoid warnings.
# [pytest]
# markers =
# slow: marks tests as slow (deselect with '-m "not slow"')
# integration: tests that require a real database
#
# Then: pytest -m "not slow" → skip all slow tests in CI fast path
# pytest -m integration → run only integration tests
# @pytest.mark.slow
# @pytest.mark.integration
# def test_full_user_creation_flow(real_database):
# ...
# ============================================================
# SECTION 5: tmp_path — temporary files per test
# ============================================================
# pytest provides a tmp_path fixture (pathlib.Path) that gives
# each test its own temporary directory, cleaned up automatically.
# Never use /tmp directly in tests — tests would collide.
#
# def test_writes_report_file(tmp_path):
# report_file = tmp_path / "report.csv"
# generate_report(output_path=report_file)
# assert report_file.exists()
# content = report_file.read_text()
# assert "Total" in content
# ============================================================
# SECTION 6: monkeypatch — safe runtime patching
# ============================================================
# monkeypatch provides test-scoped patches that are AUTOMATICALLY
# undone after the test — no cleanup code needed.
#
# vs mock.patch: monkeypatch is simpler for simple cases;
# mock.patch is more powerful (tracks calls, supports return values).
#
# Key methods:
# monkeypatch.setattr(obj, "attr", value) → patch an attribute
# monkeypatch.setenv("VAR", "value") → set env variable
# monkeypatch.delenv("VAR") → delete env variable
# monkeypatch.setitem(dict, key, value) → patch a dict entry
# monkeypatch.syspath_prepend(path) → modify sys.path
# def test_uses_env_var(monkeypatch):
# monkeypatch.setenv("API_KEY", "test-key-123")
# monkeypatch.setenv("DEBUG", "true")
# result = load_config() # reads env vars
# assert result.api_key == "test-key-123"
#
# def test_patches_datetime(monkeypatch):
# fixed_now = datetime(2024, 1, 15, 12, 0, 0)
# monkeypatch.setattr("mymodule.datetime", lambda: fixed_now)
# assert get_current_date() == "2024-01-15"
# ============================================================
# SECTION 7: unittest.mock — Mock and MagicMock
# ============================================================
# Mock(): a generic mock object. Records all calls made to it.
# - mock.some_method() → returns another Mock (auto-specs children)
# - mock.return_value = 42 → mock() returns 42
# - mock.side_effect = exception → calling mock() raises it
# - mock.side_effect = [1, 2, 3] → returns values in sequence
#
# MagicMock(): Mock + pre-configured magic methods (__len__, __iter__,
# __enter__/__exit__ for context managers, etc.)
# Use MagicMock when the code under test uses the object in a
# special way (e.g., with statement, len(), iteration).
#
# patch(): temporarily replaces a name in a module with a Mock.
# IMPORTANT: patch the name WHERE IT IS USED, not where it is defined.
# If mymodule.py does 'import requests', patch 'mymodule.requests',
# NOT 'requests.get' — the latter won't affect the already-imported name.
from unittest.mock import Mock, MagicMock, patch, call, AsyncMock
# --- Basic Mock usage ---
def demonstrate_mock_basics():
mock_db = Mock()
# Configure return value
mock_db.find_user.return_value = {'id': 1, 'name': 'Alice'}
# Configure side effect (exception)
mock_db.delete_user.side_effect = PermissionError("Not allowed")
# Use the mock (simulating production code calling it)
user = mock_db.find_user(user_id=1)
assert user['name'] == 'Alice'
# Assertion methods — these are what make Mock powerful
mock_db.find_user.assert_called_once_with(user_id=1)
mock_db.find_user.assert_called_with(user_id=1) # same, but for last call
assert mock_db.find_user.call_count == 1
# call_args_list: inspect ALL calls if called multiple times
mock_db.find_user(user_id=2)
mock_db.find_user(user_id=3)
assert mock_db.find_user.call_args_list == [
call(user_id=1),
call(user_id=2),
call(user_id=3),
]
# --- patch as decorator ---
# @patch("mymodule.requests.get")
# def test_fetch_user(mock_get):
# mock_get.return_value.json.return_value = {"id": 1, "name": "Alice"}
# mock_get.return_value.status_code = 200
#
# result = fetch_user_from_api(user_id=1)
#
# mock_get.assert_called_once_with("https://api.example.com/users/1")
# assert result["name"] == "Alice"
# --- patch as context manager ---
# def test_sends_email():
# with patch("notifications.smtp.send") as mock_send:
# mock_send.return_value = True
# notify_user(user_id=1, message="Welcome!")
# mock_send.assert_called_once()
# ============================================================
# SECTION 8: Testing async code
# ============================================================
# asyncio.run() doesn't work inside pytest's sync event loop.
# Use pytest-asyncio: pip install pytest-asyncio
#
# Configure in pytest.ini or pyproject.toml:
# [tool.pytest.ini_options]
# asyncio_mode = "auto" # all async test functions get the marker
#
# AsyncMock: like Mock but returns a coroutine when called.
# Required when the code under test does 'await mock()'.
# import pytest
# from unittest.mock import AsyncMock
#
# @pytest.mark.asyncio
# async def test_async_fetch():
# mock_client = AsyncMock()
# mock_client.get.return_value = {"data": [1, 2, 3]}
#
# result = await fetch_data(client=mock_client)
#
# mock_client.get.assert_awaited_once_with("/data")
# assert result == [1, 2, 3]
#
# # Test that async function raises correctly
# @pytest.mark.asyncio
# async def test_async_timeout():
# mock_client = AsyncMock()
# mock_client.get.side_effect = asyncio.TimeoutError()
#
# with pytest.raises(ServiceUnavailableError):
# await fetch_data(client=mock_client)
# ============================================================
# SECTION 9: hypothesis — property-based testing
# ============================================================
# Traditional tests: you write specific inputs and expected outputs.
# Property-based tests: you describe PROPERTIES that must always hold,
# and Hypothesis generates hundreds of inputs to try to falsify them.
#
# Hypothesis finds edge cases you wouldn't think to test manually:
# - empty strings, very long strings, Unicode, null bytes
# - integer overflow, negative numbers, zero
# - NaN, infinity, -0.0 for floats
#
# When Hypothesis finds a failure, it SHRINKS the input to the
# smallest example that still fails — critical for debugging.
#
# Install: pip install hypothesis
#
# Core strategies:
# st.integers(min_value=0, max_value=100)
# st.text(alphabet=st.characters(whitelist_categories=('L',)))
# st.lists(st.integers(), min_size=1, max_size=10)
# st.from_regex(r'\d{3}-\d{4}') → generates matching strings
# st.builds(MyClass, name=st.text()) → build objects with strategies
# from hypothesis import given, settings, assume
# from hypothesis import strategies as st
#
# @given(st.lists(st.integers()))
# def test_sort_is_idempotent(lst):
# """Sorting twice gives the same result as sorting once."""
# assert sorted(sorted(lst)) == sorted(lst)
#
# @given(st.text())
# def test_encode_decode_roundtrip(s):
# """Whatever we encode, we can decode back."""
# encoded = encode(s)
# decoded = decode(encoded)
# assert decoded == s
#
# @given(st.integers(min_value=1), st.integers(min_value=1))
# def test_add_is_commutative(a, b):
# assert add(a, b) == add(b, a)
#
# # assume() discards inputs that don't meet preconditions
# @given(st.integers())
# def test_positive_sqrt(n):
# assume(n >= 0) # discard negative inputs
# assert sqrt(n) >= 0
# ============================================================
# SECTION 10: Coverage — measuring what's tested
# ============================================================
# Install: pip install pytest-cov
# Run:
# pytest --cov=src --cov-report=html --cov-report=term-missing
#
# --cov=src → measure coverage for the 'src' package only
# --cov-report=html → generates htmlcov/index.html (browsable)
# term-missing → shows which line numbers are NOT covered
#
# LINE COVERAGE vs BRANCH COVERAGE:
# Line coverage: was this line executed at all?
# Branch coverage: was EVERY branch (if/else) taken?
#
# Example:
# def check(x):
# if x > 0: # ← line covered
# return True # ← line covered
# return False # ← NOT covered by "if x > 0" test alone
#
# Line coverage: 75% (3 of 4 lines hit)
# Branch coverage: 50% (only the True branch was tested)
# → Always use branch coverage: pytest --cov-branch
#
# Setting thresholds (fail build if below):
# [tool.coverage.report]
# fail_under = 85
# ============================================================
# SECTION 11: Mocking vs not mocking
# ============================================================
# MOCK external dependencies:
# - HTTP APIs (slow, requires network, may cost money)
# - Email/SMS services (would send real messages)
# - Payment gateways
# - Time / random (non-deterministic)
# - File system (sometimes — prefer tmp_path for simple cases)
#
# DO NOT MOCK your own business logic:
# - If you mock the UserService to test the OrderService,
# you're not testing their actual interaction
# - If you mock a function to always return True, tests pass
# even if the real function is broken
#
# RULE: mock at the BOUNDARY of your system (external I/O).
# Test your internal logic with real objects, possibly with
# test doubles (in-memory implementations of interfaces).
# ============================================================
# SECTION 12: Arrange-Act-Assert pattern
# ============================================================
# Every test should follow AAA:
# Arrange: set up the data, fixtures, mocks needed
# Act: call the one thing being tested
# Assert: verify the outcome
#
# One test = one behavior. If a test has two Acts, split it.
# Tests that do multiple things become impossible to name clearly.
# def test_user_creation_sends_welcome_email():
# # Arrange
# db = InMemoryUserRepository()
# mock_emailer = Mock()
# service = UserService(db=db, emailer=mock_emailer)
#
# # Act
# user = service.create_user(email="alice@example.com", name="Alice")
#
# # Assert
# assert user.id is not None # user was persisted
# assert db.find_by_email("alice@example.com") == user # retrievable
# mock_emailer.send_welcome.assert_called_once_with( # email was sent
# to="alice@example.com",
# name="Alice"
# )
# ============================================================
# SECTION 13: Complete real-world test suite example
# ============================================================
# A User service with DB interaction, showing both unit and
# integration test approaches.
import sqlite3
from dataclasses import dataclass, field
from typing import Optional
# ---- Domain model ----
@dataclass
class User:
email: str
name: str
id: Optional[int] = None
class UserNotFoundError(Exception):
pass
class DuplicateEmailError(Exception):
pass
# ---- Repository (data access layer) ----
class UserRepository:
"""Production repository — talks to a real SQLite DB."""
def __init__(self, conn: sqlite3.Connection):
self._conn = conn
def save(self, user: User) -> User:
try:
cursor = self._conn.execute(
"INSERT INTO users (email, name) VALUES (?, ?)",
(user.email, user.name)
)
self._conn.commit()
return User(email=user.email, name=user.name, id=cursor.lastrowid)
except sqlite3.IntegrityError:
raise DuplicateEmailError(f"Email already exists: {user.email}")
def find_by_email(self, email: str) -> User:
row = self._conn.execute(
"SELECT id, email, name FROM users WHERE email = ?", (email,)
).fetchone()
if row is None:
raise UserNotFoundError(email)
return User(id=row[0], email=row[1], name=row[2])
# ---- Service (business logic) ----
class UserService:
"""Business logic layer. Depends on abstractions, not concrete classes."""
def __init__(self, repo: UserRepository, emailer):
self._repo = repo
self._emailer = emailer
def create_user(self, email: str, name: str) -> User:
if not email or '@' not in email:
raise ValueError(f"Invalid email: {email}")
user = self._repo.save(User(email=email, name=name))
self._emailer.send_welcome(to=email, name=name)
return user
# ============================================================
# TEST FILE EXAMPLE (would live at tests/test_user_service.py)
# ============================================================
# import pytest
# from unittest.mock import Mock
# from myapp.users import UserService, UserRepository, User
# from myapp.users import DuplicateEmailError, UserNotFoundError
#
# # ---- Unit tests (all mocked) ----
#
# class TestUserServiceUnit:
#
# def setup_method(self):
# """Runs before each test method. Fresh mocks each time."""
# self.mock_repo = Mock()
# self.mock_emailer = Mock()
# self.service = UserService(repo=self.mock_repo, emailer=self.mock_emailer)
#
# def test_create_user_success(self):
# self.mock_repo.save.return_value = User(id=1, email="a@b.com", name="Alice")
# user = self.service.create_user(email="a@b.com", name="Alice")
# assert user.id == 1
# self.mock_emailer.send_welcome.assert_called_once_with(to="a@b.com", name="Alice")
#
# def test_create_user_invalid_email_raises(self):
# with pytest.raises(ValueError, match="Invalid email"):
# self.service.create_user(email="not-an-email", name="Alice")
# self.mock_repo.save.assert_not_called() # DB not touched
#
# def test_create_user_duplicate_propagates(self):
# self.mock_repo.save.side_effect = DuplicateEmailError("a@b.com")
# with pytest.raises(DuplicateEmailError):
# self.service.create_user(email="a@b.com", name="Alice")
#
# # ---- Integration tests (real SQLite, no HTTP mocks) ----
#
# @pytest.fixture(scope="function")
# def db_conn():
# conn = sqlite3.connect(":memory:")
# conn.execute("""
# CREATE TABLE users (
# id INTEGER PRIMARY KEY AUTOINCREMENT,
# email TEXT UNIQUE NOT NULL,
# name TEXT NOT NULL
# )
# """)
# conn.commit()
# yield conn
# conn.close()
#
# class TestUserRepositoryIntegration:
#
# def test_save_and_find(self, db_conn):
# repo = UserRepository(db_conn)
# saved = repo.save(User(email="x@y.com", name="Bob"))
# assert saved.id is not None
# found = repo.find_by_email("x@y.com")
# assert found.name == "Bob"
#
# def test_duplicate_email_raises(self, db_conn):
# repo = UserRepository(db_conn)
# repo.save(User(email="x@y.com", name="Bob"))
# with pytest.raises(DuplicateEmailError):
# repo.save(User(email="x@y.com", name="Alice"))
#
# def test_find_nonexistent_raises(self, db_conn):
# repo = UserRepository(db_conn)
# with pytest.raises(UserNotFoundError):
# repo.find_by_email("nobody@example.com")