Natty’s blog

Stay hungry. Stay foolish. — Steve Jobs

[PHP] Simple Test

Posted by natty on October 22, 2008

เนื่องจากมีคนเรียกร้องอยากใช้งาน SimpleTest ใน PHP ขึ้นมาอย่างกระทันหัน ไม่ได้ตั้งตัว ก็เลยเอามาวางให้ที่นี่นะคะ

ก่อนอื่น ไป ดาวน์โหลดมาก่อนนะคะ

เมื่อได้มาแล้ว ก็แตกไฟล์ แล้วเอา folder ชื่อ simpletest ก็เอาไปวางใน apache ของตัวเอง สำหรับเก๋ เก๋จะวางไว้นอกสุด ก็คือใน htdocs เลย ใครจะมาเรียกใช้เมื่อไหร่ก็ได้

เก๋จะให้ตัวอย่างสำหรับการทำ SimpleTest ของ blog ตอนหนึ่ง คือ [PHP] Class สำหรับ convert DBF to MySQL

โดยเก๋จะสร้าง folder ชื่อ unittest และมีไฟล์ชื่อ TestDBFMySQL.php อยู่ การทำหน้า test เราต้องใส่ require_once(‘../../simpletest/autorun.php’); เพื่อ include ไปยัง lib ของ simpletest และก็ include ไฟล์ที่เราจะ test ด้วย ปกติจะ new เป็น test case และ test suite ใน class อื่นได้ แต่อันนี้ไม่ได้ new ไว้ไฟล์อื่น แบบว่าเรียกไฟล์นี้โดยตรงเลย

การ test ก็จะ assert ในหลายกรณี อันนี้ก็แล้วแต่เราว่าจะทำยังงัย อ่านได้ที่นี่

โดยหน้าตาของไฟล์ตัวอย่าง เป็นดังนี้


<?php
//DBFMySQL/unittest/TestDBFMySQL.php

require_once('../../simpletest/autorun.php');
require_once('../DBFMySQL.php');

class TestDBFMySQL extends UnitTestCase {
        //public $fromDBF='D:\PO_McDonald\INVORHD0.DBF';
        public static $fromDBF='D:\PO_McDonald\INVORHD0.DBF';
        public static $toMySQL='INVORHD0';
        public static $myDB='db_mcdonald';
        public static $myUser='root';
        public static $myPass='root';
        public static $myHost='localhost';

    function setUp() {

    }

    function tearDown() {
        //@unlink('/temp/test.log');
    }

    function testConvertDBFToMySQL(){
        $x = DBFMySQL::convertDBFToMySQL(self::$fromDBF, self::$toMySQL, self::$myDB, self::$myUser, self::$myPass, self::$myHost);
        $this->assertTrue($x);
    }
    function testReadDBFToArray(){
        $x = DBFMySQL::readDBFToArray(self::$fromDBF);
        $this->assertNotNull($x);
    }
    function testPersistToMySQL(){
        $arrTable = DBFMySQL::readDBFToArray(self::$fromDBF);
        $x = DBFMySQL::persistToMySQL($arrTable, self::$toMySQL, self::$myDB, self::$myUser, self::$myPass, self::$myHost);
        $this->assertTrue($x);
    }
}
?>

ลองเข้าไปที่ http://localhost/DBFMySQL/unittest/TestDBFMySQL.php นะคะ (เครื่องเก๋นะคะ ยกตัวอย่างให้ดู)

หากว่า Test ผ่าน จะได้ผลดังนี้ค่ะ

Image hosting by UpPicth.com

ถ้า fail

Image hosting by UpPicth.com

ถ้า connect db ไม่ผ่าน
Image hosting by UpPicth.com

สงสัยก็ comment ถามมาได้ค่ะ จะตอบให้

ดาวน์โหลดตัวอย่าง code ได้ที่นี่ค่ะ ก่อนอื่นควรไปอ่าน [PHP] Class สำหรับ convert DBF to MySQL ด้วยนะคะ เผื่อรันไม่ผ่าน

One Response to “[PHP] Simple Test”

  1. nattyait said

    อยากจะโน๊ตไว้ให้กับตัวเองว่า การทำ test case ของ PHP ต้องใส่คำว่า test ไว้ด้านหน้าฟังก์ชันด้วย ไม่งั้นจะไม่ถูกเรียกไปรัน ปกติเราไม่ต้อง new test case เหมือน java ก็สามารถทำงานได้ แต่ว่าเราต้องใส่คำว่า test ไว้หน้าฟังก์ชันด้วย

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>