Skip to main content

เนื้อหาภาษาไทย / Thai content

System guide Content reviewed

การสร้าง Test Case แรก: ทดสอบฟังก์ชันง่ายๆ

บทความนี้อธิบายวิธีการสร้าง Test Case แรกด้วย PHPUnit โดยเริ่มต้นจากการสร้าง Test Class ที่ extend `TestCase` และสร้าง Test Method ภายใน Class นั้น ซึ่งจะใช้ As...

การสร้าง Test Case แรก: ทดสอบฟังก์ชันง่ายๆ

หลังจากที่เราได้ทำความรู้จักกับ PHPUnit และความสำคัญของการ Unit Testing ในบทที่แล้ว (เริ่มต้นกับ Unit Testing: ทำความรู้จัก PHPUnit) ในบทนี้ เราจะมาลงมือสร้าง Test Case แรกกันครับ โดยเราจะทดสอบฟังก์ชันง่ายๆ เพื่อให้เห็นภาพรวมของการเขียน Test และการใช้ Assertion ต่างๆ

การสร้าง Test Class

ขั้นตอนแรกในการสร้าง Test Case คือการสร้าง Test Class ครับ โดย Test Class จะเป็นเหมือน container ที่เก็บ Test Method ต่างๆ ที่เราจะเขียนขึ้นมา โดยทั่วไปแล้ว Test Class จะอยู่ใน directory ที่แยกออกมาจาก source code หลักของเรา เช่น tests/

ข้อควรรู้: ชื่อของ Test Class ควรมีความหมายและสื่อถึง class ที่เราต้องการทดสอบ โดยทั่วไปจะใช้ชื่อ class ที่ต้องการทดสอบ ตามด้วยคำว่า Test เช่น ถ้าเราต้องการทดสอบ class ชื่อ Calculator, Test Class ของเราก็ควรมีชื่อว่า CalculatorTest

สมมติว่าเรามีฟังก์ชันง่ายๆ ที่อยู่ในไฟล์ src/Calculator.php ดังนี้:

<?php

namespace App;

class Calculator
{
    public function add(int $a, int $b): int
    {
        return $a + $b;
    }
}

เราจะสร้าง Test Class ชื่อ CalculatorTest ในไฟล์ tests/CalculatorTest.php โดย code มีลักษณะดังนี้:

<?php

use PHPUnit\Framework\TestCase;
use App\Calculator;

class CalculatorTest extends TestCase
{
    // Test methods จะถูกเขียนที่นี่
}

คำอธิบาย:

  • <?php: ประกาศว่าเป็นไฟล์ PHP
  • use PHPUnit\Framework\TestCase;: Import class TestCase จาก PHPUnit ซึ่งเป็น class หลักที่เราต้อง extend เพื่อสร้าง Test Class
  • use App\Calculator;: Import class Calculator ที่เราต้องการทดสอบ
  • class CalculatorTest extends TestCase: สร้าง class CalculatorTest และ extend จาก TestCase

การสร้าง Test Method

เมื่อเรามี Test Class แล้ว ขั้นตอนต่อไปคือการสร้าง Test Method ครับ Test Method คือ function ที่เราจะเขียน code เพื่อทดสอบการทำงานของฟังก์ชันที่เราต้องการ Test

ข้อควรรู้: ชื่อของ Test Method ควรมีความหมายและสื่อถึงสิ่งที่ต้องการทดสอบ โดยทั่วไปจะเริ่มต้นด้วยคำว่า test เช่น testAddTwoPositiveNumbers

เราจะเพิ่ม Test Method เพื่อทดสอบฟังก์ชัน add() ใน class Calculator:

<?php

use PHPUnit\Framework\TestCase;
use App\Calculator;

class CalculatorTest extends TestCase
{
    public function testAddTwoPositiveNumbers(): void
    {
        // Test code จะถูกเขียนที่นี่
    }
}

คำอธิบาย:

  • public function testAddTwoPositiveNumbers(): void: สร้าง function ชื่อ testAddTwoPositiveNumbers ซึ่งเป็น Test Method ของเรา โดย void หมายถึง function นี้ไม่มี return value

การใช้ Assertions

Assertion คือ function ที่ใช้ตรวจสอบว่าผลลัพธ์ที่ได้จากการรัน function ที่เราต้องการทดสอบ ตรงกับที่เราคาดหวังหรือไม่ PHPUnit มี Assertion ให้เราเลือกใช้มากมาย เช่น assertEquals(), assertTrue(), assertFalse(), assertNull(), assertNotNull() เป็นต้น

เราจะใช้ assertEquals() เพื่อตรวจสอบว่าฟังก์ชัน add() คืนค่าผลรวมที่ถูกต้อง:

<?php

use PHPUnit\Framework\TestCase;
use App\Calculator;

class CalculatorTest extends TestCase
{
    public function testAddTwoPositiveNumbers(): void
    {
        $calculator = new Calculator();
        $result = $calculator->add(2, 3);

        $this->assertEquals(5, $result);
    }
}

คำอธิบาย:

  • $calculator = new Calculator();: สร้าง instance ของ class Calculator
  • $result = $calculator->add(2, 3);: เรียกใช้ function add() ด้วย input 2 และ 3 และเก็บผลลัพธ์ไว้ในตัวแปร $result
  • $this->assertEquals(5, $result);: ใช้ assertEquals() เพื่อตรวจสอบว่า $result มีค่าเท่ากับ 5 หรือไม่ โดย parameter แรกคือค่าที่เราคาดหวัง (expected value) และ parameter ที่สองคือค่าที่ได้จริง (actual value)

สรุป: ใน Test Method นี้ เราได้สร้าง instance ของ Calculator, เรียกใช้ function add() และใช้ assertEquals() เพื่อตรวจสอบผลลัพธ์ หากผลลัพธ์ไม่ตรงกับที่เราคาดหวัง PHPUnit จะแจ้งว่า Test Fail

ตัวอย่างเพิ่มเติม: ทดสอบฟังก์ชันที่คืนค่า String

สมมติว่าเรามี function ที่คืนค่า String:

<?php

namespace App;

class Greeter
{
    public function greet(string $name): string
    {
        return "Hello, " . $name . "!";
    }
}

เราสามารถสร้าง Test Case เพื่อทดสอบ function นี้ได้ดังนี้:

<?php

use PHPUnit\Framework\TestCase;
use App\Greeter;

class GreeterTest extends TestCase
{
    public function testGreetWithName(): void
    {
        $greeter = new Greeter();
        $result = $greeter->greet("World");

        $this->assertEquals("Hello, World!", $result);
    }
}

การใช้ Assertions อื่นๆ

นอกจาก assertEquals() แล้ว PHPUnit ยังมี Assertion อื่นๆ ที่มีประโยชน์อีกมากมาย เช่น:

  • assertTrue(bool $condition, string $message = ''): ตรวจสอบว่า $condition เป็น true
  • assertFalse(bool $condition, string $message = ''): ตรวจสอบว่า $condition เป็น false
  • assertNull(mixed $actual, string $message = ''): ตรวจสอบว่า $actual เป็น null
  • assertNotNull(mixed $actual, string $message = ''): ตรวจสอบว่า $actual ไม่เป็น null
  • assertSame(mixed $expected, mixed $actual, string $message = ''): ตรวจสอบว่า $expected และ $actual มีค่าและชนิดข้อมูลเหมือนกัน
  • assertNotSame(mixed $expected, mixed $actual, string $message = ''): ตรวจสอบว่า $expected และ $actual มีค่าหรือชนิดข้อมูลไม่เหมือนกัน
  • assertContains(mixed $needle, iterable $haystack, string $message = ''): ตรวจสอบว่า $needle อยู่ใน $haystack (array หรือ string)

เราจะเรียนรู้การใช้งาน Assertions เหล่านี้เพิ่มเติมในบทถัดไป (Assertion คืออะไร: รู้จัก Assertions พื้นฐาน)

สรุป

ในบทนี้ เราได้เรียนรู้วิธีการสร้าง Test Case แรก โดยเริ่มต้นจากการสร้าง Test Class, สร้าง Test Method และใช้ assertEquals() เพื่อตรวจสอบผลลัพธ์ นอกจากนี้ เรายังได้เห็นตัวอย่างการทดสอบ function ที่คืนค่า String และได้รู้จัก Assertions อื่นๆ ที่มีประโยชน์ หวังว่าบทนี้จะช่วยให้คุณเข้าใจพื้นฐานของการเขียน Unit Test ด้วย PHPUnit มากขึ้น ในบทถัดไป เราจะเจาะลึกเรื่องของ Assertions กันครับ

Office.in.th Custom Service

Want to build or adapt a system from this structure?

Office.in.th designs and develops enterprise web applications, API integrations, and public- and private-sector management systems, with long-term support.

See our software development service