C.W.K.
Stream
Lesson 05 of 10 · published

테스트 전략 — In-Memory + Fixture

~12 min · testing, fixtures, production

Level 0Scout
0 XP0/80 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

SQLite 가 테스트 DB 의 super power

SQLite 가 테스트 setup 거의 공짜. 잘 테스트된 codebase 마다 등장하는 3 전략:

  • :memory: per testsqlite3.connect(':memory:') open, migration 실행, 테스트 실행, process end 에 DB 사라짐. 가장 빠른 setup.
  • tempfile per test — :memory: 같은데 file-backed. 코드가 connection close + reopen 해야 할 때 유용.
  • shared template + COPY — fixture DB 한 번 빌드, 테스트마다 복사. Migration 비싸면 빠름.
Self-reference: 피파 테스트가 pytest fixture 통해 :memory: 패턴 — 모든 store-level 테스트가 ms 안에 fresh 빈 DB. 100+ 테스트 있는 backend test suite 가 ~2 초 안에 끝나는 이유.

Code

pytest fixture — 테스트마다 fresh in-memory store·python
import pytest, asyncio, aiosqlite

@pytest.fixture
async def store(tmp_path):
    # tempfile 로 file locking 도 운동
    path = tmp_path / 'test.db'
    s = await ConversationStore.open(str(path))
    yield s
    await s.conn.close()

async def test_create_and_list(store):
    cid = await store.create_conversation('Hello')
    convs = await store.list_conversations()
    assert len(convs) == 1
    assert convs[0]['id'] == cid
순수 :memory: — 가장 빠름, file I/O X·python
import sqlite3

def test_pure_in_memory():
    conn = sqlite3.connect(':memory:')
    conn.execute('CREATE TABLE t(id INTEGER PRIMARY KEY, v TEXT)')
    conn.execute('INSERT INTO t(v) VALUES (?)', ('hello',))
    assert conn.execute('SELECT v FROM t').fetchone() == ('hello',)

External links

Exercise

sqlite3 쓰는 Python 프로젝트 (본인 거 또는 피파) 에 테스트마다 fresh DB 주는 pytest fixture 추가. Test 사이 state leak 없이 테스트 suite 도는지 확인. tempfile + WAL 쓰는 fixture variant 추가 — 일반 CRUD 테스트엔 동일 동작 검증.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.