0Pricing
Load Testing & Performance Benchmarking (JMeter & k6) · บทเรียน

การยืนยันเพื่อตรวจสอบการตอบกลับ

เพิ่มการตรวจสอบเพื่อยืนยันการตอบกลับของเซิร์ฟเวอร์และรับรองความถูกต้องระหว่างการทดสอบโหลด

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

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

What are Assertions?

Welcome! In performance testing, it's not enough to just know if your application is fast. You also need to ensure it's giving the correct responses.

This is where Assertions come in. They are checks that you add to your test plan to validate the actual response from the server against an expected outcome.

  • Verify correctness: Ensure the server sends the right data.
  • Catch errors: Identify unexpected error messages or missing content.
  • Validate performance: Check if response times or sizes meet criteria.

Why Assertions Matter

Imagine running a load test with thousands of users. If your application starts sending incorrect data or error pages under load, simply measuring response time won't tell you the full story.

Assertions help you:

  • Identify functional issues: Even under stress, the app should work as expected.
  • Ensure data integrity: Confirm that data retrieved or sent is complete and accurate.
  • Set performance gates: Fail a test if responses are too slow or too large.

They transform a simple load test into a more comprehensive performance validation.

Adding Assertions in JMeter

Adding an assertion in JMeter is straightforward. You can add them as a child element to a Sampler, a Transaction Controller, or even directly to the Test Plan or a Thread Group.

To add an assertion:

  1. Right-click on the element where you want to apply the assertion (e.g., an HTTP Request).
  2. Hover over Add > Assertions.
  3. Select the type of assertion you want to add (e.g., Response Assertion).

Assertions will then apply to the element they are a child of (or its children, if added higher up).

Response Assertion: The Workhorse

The Response Assertion is one of the most commonly used assertion types. It allows you to check for specific text patterns, regular expressions, or even entire strings within various parts of the server's response.

You can configure it to check:

  • Response Body: The main content of the page/API.
  • Response Headers: HTTP headers like 'Content-Type'.
  • Response Code: The HTTP status code (e.g., 200, 404).
  • Response Message: The HTTP status message (e.g., 'OK', 'Not Found').

You can also choose how to match: 'Contains', 'Matches', 'Equals', or 'Substring'.

Response Assertion Example

Let's say you send an HTTP request to a login page and, upon successful login, expect to see the text "Welcome, User!" on the next page.

Here's how you might configure a Response Assertion:

  • Apply to: Main sample only
  • Response Field to Test: Text Response
  • Pattern Matching Rules: Contains
  • Patterns to Test: Welcome, User!

If the response body contains this text, the assertion passes. Otherwise, it fails, indicating a potential issue.

Patterns to Test:
Welcome, User!

Duration Assertion: Time Constraints

While response time is a key performance metric, sometimes you need to enforce strict limits. The Duration Assertion allows you to do just that.

It checks if the response time for a sample is less than or equal to a specified maximum duration (in milliseconds).

  • Why use it? To ensure critical transactions meet Service Level Agreements (SLAs).
  • Configuration: Simply enter the maximum allowed duration in milliseconds.

If a request takes longer than the set duration, the assertion fails, highlighting a performance bottleneck.

Size Assertion: Data Completeness

The Size Assertion helps verify the size of the server's response. This can be useful for:

  • Detecting incomplete responses: If a response is unexpectedly small, it might indicate an error or truncated data.
  • Checking for bloated responses: If a response is too large, it might point to inefficient data transfer.

You can specify the size in bytes and choose comparison operators like 'Equals', 'Greater Than', 'Less Than', 'Not Equals', 'Greater Than or Equals', or 'Less Than or Equals'.

JSON Assertion: For API Responses

Many modern applications use JSON (JavaScript Object Notation) for API communication. The JSON Assertion in JMeter lets you validate specific values within a JSON response using JSONPath expressions.

For example, to check if a user's 'status' is 'active' in a JSON response:

  • JSONPath Expression: $.user.status
  • Expected Value: active

This is much more precise than trying to use a Response Assertion with regular expressions on complex JSON.

Example JSON:
{
  "user": {
    "id": 123,
    "name": "Alice",
    "status": "active"
  }
}

JSONPath to get status:
$.user.status

XML Assertion: For XML Responses

If your application communicates using XML, the XML Assertion is your tool. It allows you to validate XML responses using XPath expressions.

Similar to JSONPath, XPath helps you navigate and select specific nodes or values within an XML document.

For example, to check the 'price' of an item:

  • XPath Expression: //item/price/text()
  • Expected Value: 19.99

This ensures the XML structure and its data are as expected.

Example XML:
<catalog>
  <item>
    <name>Book</name>
    <price>19.99</price>
  </item>
</catalog>

XPath to get price:
//item/price/text()

Assertion Challenge

You are testing an e-commerce API. Which assertions would you use for the following scenarios?

Recap: Validate Your Responses

You've learned how critical assertions are for ensuring the correctness of your application's responses during performance testing. They allow you to go beyond just measuring speed.

  • Response Assertion: Checks for text/patterns in various response fields.
  • Duration Assertion: Ensures response times meet specific limits.
  • Size Assertion: Validates the size of the server's response.
  • JSON Assertion: Uses JSONPath to validate values in JSON responses.
  • XML Assertion: Uses XPath to validate values in XML responses.

By effectively using assertions, you build more robust and reliable performance tests!

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

บทเรียน “การยืนยันเพื่อตรวจสอบการตอบกลับ” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การยืนยันเพื่อตรวจสอบการตอบกลับ”

เพิ่มการตรวจสอบเพื่อยืนยันการตอบกลับของเซิร์ฟเวอร์และรับรองความถูกต้องระหว่างการทดสอบโหลด คุณปฏิบัติ Load Testing & Performance Benchmarking (JMeter & k6) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Load Testing & Performance Benchmarking (JMeter & k6) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Load Testing & Performance Benchmarking (JMeter & k6) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การยืนยันเพื่อตรวจสอบการตอบกลับ” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Load Testing & Performance Benchmarking (JMeter & k6) นี้ได้ไหม

ได้ บทเรียน Load Testing & Performance Benchmarking (JMeter & k6) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. ตัวควบคุมตรรกะและลูป
  2. การยืนยันเพื่อตรวจสอบการตอบกลับ
  3. ตัวจับเวลาสำหรับการกำหนดจังหวะที่สมจริง
  4. การทดสอบแบบกระจายด้วยเซิร์ฟเวอร์ระยะไกล
← กลับไปที่ Load Testing & Performance Benchmarking (JMeter & k6)