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

ไคลเอนต์ทดสอบและฟิกซ์เจอร์

จำลองคำขอและเตรียมข้อมูลทดสอบ

ไคลเอนต์ทดสอบและฟิกซ์เจอร์ เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Testing Whole Views

Model tests are great, but you also want to test full pages. The test client lets you simulate a real browser request inside a test. 🌐

The Client Is Ready

Every TestCase gives you a self.client for free. No setup needed, it is wired up and waiting in each test.

Make a GET Request

Call self.client.get with a URL to fetch a page just like a browser would, then inspect what comes back.

response = self.client.get('/articles/')

Check the Status Code

The response carries a status_code. Assert it equals 200 to confirm the page loaded successfully.

self.assertEqual(response.status_code, 200)

A Cleaner Status Check

Django offers a shortcut. Use assertContains to confirm both a 200 status and that expected text is on the page.

self.assertContains(response, 'Latest Articles')

Posting Form Data

Simulate a form submission with self.client.post, passing a dictionary of the fields a user would fill in.

self.client.post('/add/', {'title': 'Hi'})

Test by Route Name

Hardcoded URLs break easily. Use reverse to look up the URL from its route name instead.

url = reverse('article-list')
response = self.client.get(url)

What Are Fixtures?

A fixture is a file of pre-built rows. It seeds your test database so every test starts with the same known data.

Create a Fixture File

Export existing rows with dumpdata. Django writes them to JSON that you can replay later as a fixture.

python manage.py dumpdata blog > data.json

Load a Fixture in Tests

List your fixture files in the fixtures attribute and Django loads them automatically before each test runs.

class BlogTests(TestCase):
    fixtures = ['data.json']

Fixtures vs setUp

Reach for fixtures when you need a fixed dataset to load fast, and setUp when each test needs small, tailored objects.

Quick Check

You want to confirm a page loaded and shows a heading.

Recap

You drove views with the test client, checked status and content, posted form data, and seeded data with fixtures and dumpdata. 🎉

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

บทเรียน “ไคลเอนต์ทดสอบและฟิกซ์เจอร์” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ไคลเอนต์ทดสอบและฟิกซ์เจอร์”

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

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

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

บทเรียน “ไคลเอนต์ทดสอบและฟิกซ์เจอร์” ใช้เวลานานแค่ไหน

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

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

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

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

  1. คลาส Paginator
  2. ตัวควบคุมหน้าใน ListView
  3. การเขียน TestCase และคำยืนยัน
  4. ไคลเอนต์ทดสอบและฟิกซ์เจอร์
← กลับไปที่ Django Academy