DEV Community

Wasith Theerapattrathamrong
Wasith Theerapattrathamrong

Posted on

1

ประเภทของ Equivalence Class Testing

บทความนี้เกี่ยวกับการทดสอบระบบ จะมาเล่าถึงประเภทของ Equivalence Class Testing โดยแบ่งออกเป็น 4 กลุ่มย่อย ๆ ดังนี้

  1. Weak Normal Equivalence Class Testing: คือการทดสอบที่ ตัวแปลแต่ละตัวจะถูกทดสอบ และผลลัพธ์จะขึ้นอยู่กับค่าเพียงค่าเดียวเท่านั้น หรือรู้จักกันในชื่อ single fault assumption

  2. Strong Normal Equivalence Class Testing: หรือ multiple fault assumption นั้นจะทดสอบผลลัพธ์จาก inputs ที่มากกว่า 1 ตัว

  3. Weak Robust Equivalence Class Testing: จะเหมือนกับข้อแรก แต่จะเน้นในการตรวจสอบค่าที่ไม่ถูกต้อง เช่นค่าที่อยู่นอกขอบเขต ยกตัวอย่างเช่น ถ้าฟังก์ชันที่จะทดสอบรับจำนวนเต็มที่ 0-100 และใส่ -1 เข้าไปโปรแกรมจะทำงานอย่างไร throw exception หรือไม่

  4. Strong Robust Equivalence Class Testing: เป็นการออกแบบการทดสอบที่อาจจะผสมไปด้วยค่าที่ถถูกต้อง และค่าที่ผิด โดยอาจจะผิดทั้งหมด หรือผิดแค่บางส่วนก็ได้

ซึ่งการออกแบบ test cases ที่ดี ควรลดการซ้ำซ้อนของ test cases ต่าง ๆ ด้วย ในมุมผมใช้แค่ 3 แบบแรกก็เพียงพอแล้ว

ถ้ายังไม่เข้าใจไปดูตัวอย่าง code (ภาษา Swift) กัน

class Calculator {
  let left: Int
  let right: Int

  ...

  func leftAddedWithOne() -> Int {
    left + 1
  }

  func add() -> Int {
    left + right
  }

  ...
}
Enter fullscreen mode Exit fullscreen mode

เรามาดูกันที่ 2 ฟังก์ชันของโปรแกรมข้างบน คือ leftAddedWithOne() และ add()

  • leftAddedWithOne() จะทำการเข้าค่า left มาเพิ่มด้วย 1 แล้วให้ผลลัพธ์ออกมา
  • add() จะทำการเอาค่า left กับค่า right มาบวกกัน แล้วให้ผลลัพธ์ออกมา

ถ้าเราเขียน unit test ด้วย XCTestCase อาจจะได้เป็น

let placeholderValue = Int.min

func test_whenLeftIs99_leftAddedWithOne_shouldReturn100() {
  let calculator = Calculator(left: 99, right: placeholderValue)
  XCTAssertEqual(calculator.leftAddedWithOne(), 100)
}

func test_add_shouldReturnValueOfLeftAndRightAddedTogether() {
  let calculator = Calculator(left: 99, right: -66)
  XCTAssertEqual(calculator.add(), 33)
}
Enter fullscreen mode Exit fullscreen mode

จะเห็นได้ว่า test case แรก test_whenLeftIs99_leftAddedWithOne_shouldReturn100() จะไม่ได้สนใจ value ของ right โดยผลลัพธ์จะขึ้นอยู่กับค่า left อย่างเดียวเท่านั้น Test case แบบนี้จัดอยู่ในกลุ่ม Weak Normal Equivalence Class Testing

ส่วน test case ที่ 2 test_add_shouldReturnValueOfLeftAndRightAddedTogether() ผลลัพธ์จะขึ้นอยู่กับค่ามากกว่า 1 ค่า คือ left และ right ซึ่งถ้าค่าใดค่า 1 เปลี่ยนไปจะทำให้ test case นี้ fail. Test case นี้จึงจัดอยู่ในกลุ่มของ Strong Normal Equivalence Class Testing

หมายเหตุ placeholder คือ ค่าที่ใส่แค่ให้ครบตามที่โปรแรกมนั้น ๆ ต้องการไม่งั้น compile ไม่ผ่าน แต่ไม่ได้เอามาคิดคำควณกับ test case นั้น

แต่ก่อนในวงการวิทยาการคอมพิวเตอร์เรียก placeholder ว่า dummy แต่ Google ได้ออกคำแนะนำใหม่ให้เรียกว่า placeholder แทน

ส่วน robust testing

สมมุติว่ามีฟังก์ชันที่เรา string แล้วจะแปลงค่าเป็น Int ให้ โดยถ้าแปลงไม่ได้จะให้ผลลัพธ์เป็น nil แทน

func convertToInt(_ value: String) -> Int? {
  Int(value)
}

XCTAssertNil(Int("ก"))
Enter fullscreen mode Exit fullscreen mode

ซึ่ง test case ที่ดีเราไม่ควรทำการทดสอบตัว framework, หรือ library ที่ใช้ในแบบข้างบนนะครับ

ขอให้สนุกกับการเขียนโค้ด สวัสดีครับ 🙏

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

If you found this post useful, please drop a ❤️ or leave a kind comment!

Okay