0Pricing
Flask Academy · レッスン

テスト用データベースでテストを分離する

テスト実行ごとに使い捨てのDBを使用します

「テスト用データベースでテストを分離する」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Isolate the Database

Tests must never touch your real data. A test database keeps every run clean, so one test can never corrupt another or your production rows.

Use a Throwaway SQLite DB

The easy choice is an in-memory SQLite database. It lives only in RAM and vanishes the moment the test process ends.

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'

Flip on Testing Config

Set TESTING to True before tests run. It turns off error catching so exceptions surface clearly in your output.

app.config['TESTING'] = True

Build a Fresh Schema

Inside an app context, call db.create_all(). It builds every table from your models so each test run starts from an empty schema.

with app.app_context():
    db.create_all()

Tear Down After Tests

After the test, call db.drop_all() to remove every table. This guarantees the next run cannot inherit leftover rows.

with app.app_context():
    db.drop_all()

Put It in a Fixture

Wrap setup and teardown in one fixture. The code before yield creates the schema; the code after it drops everything cleanly.

@pytest.fixture
def app_db():
    db.create_all()
    yield
    db.drop_all()

Seed Sample Data

Many tests need a known row to read. Add a record in the fixture, then commit it so the test starts from a predictable state.

db.session.add(User(name='Ada'))
db.session.commit()

Why Fresh State Matters

Tests should pass in any order. Isolation means each test sees the same starting data, so results never depend on what ran before.

Roll Back Between Tests

An alternative to recreating tables is a rollback. Wrap each test in a transaction and undo it afterward for a fast, clean slate.

db.session.rollback()

Keep Test Config Separate

Store test settings in a dedicated TestConfig class. Your factory loads it only during tests, never in development or production.

class TestConfig:
    TESTING = True
    SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'

Never Test Against Prod

Always double-check the URI points at a test database. Pointing tests at production data is the fastest way to lose real records.

Quick Check

You want tests that never harm real data. What database do you use?

Recap: Test Database

You spin up a clean test database, seed known data, and tear it down after. Now your tests are fast, repeatable, and safe. 🗄️

よくある質問

「テスト用データベースでテストを分離する」レッスンは無料ですか?

はい。「テスト用データベースでテストを分離する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「テスト用データベースでテストを分離する」で何を学びますか?

テスト実行ごとに使い捨てのDBを使用します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「テスト用データベースでテストを分離する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. テストクライアントとフィクスチャ
  2. ルートとJSONをアサートする
  3. テスト用データベースでテストを分離する
  4. 認証が必要なエンドポイントをテストする
← Flask Academyに戻る