0Pricing
Flask Academy · บทเรียน

แยกการทดสอบด้วยฐานข้อมูลทดสอบ

ใช้ DB ชั่วคราวสำหรับการเรียกใช้การทดสอบแต่ละครั้ง

แยกการทดสอบด้วยฐานข้อมูลทดสอบ เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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. 🗄️

คำถามที่พบบ่อย

บทเรียน “แยกการทดสอบด้วยฐานข้อมูลทดสอบ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “แยกการทดสอบด้วยฐานข้อมูลทดสอบ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “แยกการทดสอบด้วยฐานข้อมูลทดสอบ”

ใช้ DB ชั่วคราวสำหรับการเรียกใช้การทดสอบแต่ละครั้ง คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “แยกการทดสอบด้วยฐานข้อมูลทดสอบ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม

ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ไคลเอนต์ทดสอบและอุปกรณ์ประกอบการทดสอบ
  2. ตรวจสอบเส้นทางและ JSON
  3. แยกการทดสอบด้วยฐานข้อมูลทดสอบ
  4. ทดสอบปลายทางที่ต้องยืนยันตัวตน
← กลับไปที่ Flask Academy