Thule - 0.9.6
  • Welcome
  • Introduction
  • - MVC(Model-View-Controller)
    - Architecture of Framework
    - Flow Chart of Application
    - Services In One Server
    - Developing Guide
    - Features
  • Installation
  • Getting Started
  • - Direct Access HTTP
    - Router Access HTTP
    - Router Direct Access HTTP
    - Reserved Function
    - Convention
    - Bootstrap
    - Pack Javascript , CSS
  • Basic Modules
  • - Model
    - View
    - Controller
  • Libraries
  • - Router
    - Config
    - Response
    - Request
    - Database
    - Query
    - Cache
    - Debug
    - Log
    - Unit Test
    - Registry
    - Profile
  • Helper
  • - Auth
    - Calendar
    - Date
    - Error page
    - Email
    - Form
    - GeoIP
    - Google
    - Html Element
    - Html
    - Http
    - I18n
    - Image
    - Json
    - Jquery
    - Login
    - Layout
    - Locale
    - PHP to Javascript
    - Paging
    - Payflow
    - Soap
    - Text
    - Table
    - Uri
    - Upload
    - Validation
  • Plugins
  • - Smarty Template Engine
  • History
  • Home > Unit Test

    Unit test is very useful in development with Test Driven Development.Backender can test each class and all class . Unit test provides the statistics of success and fail classes and total classes,where is fail, where is success in detail and logged in "tests/logs" directory below.

    Create Unit test class

    To create unit-test-class is very simple and powerful,no longer bother of making unit-test-class. It's better to be same class name as model class name.

    models/vm.php

    class Vm extends Model {
    ........
    }

    tests/vm.php

    include_once("unittest_config.php");
    class Vm extends Unittest {
    ...........
    }

    Add the "test function"

    Most powerful thing is that you can use "function" model has and you can make test environment as same as target model.
    "test function" are $this->typeTest, $this->matchTest.

    //model class Vm extends Model {
        ........
        function f1(){
            $arr_sql['member_id']=2;
        }
    ........
    }

    // test/vm.php
    include_once("unittest_config.php");
    class Vm extends Unittest {
        ...........
        function f1(){
            $this->typeTest($arr_sql['member_id']=2,"int"); // test type
            $this->matchTest(2,$arr_sql['member_id']); // test match-value
        }
    ...........
    }

    Run test

    include_once("unittest_config.php");
    class Vm extends Unittest {
    ..........
    }
    if( !defined('UNITTEST_ALL')){
        $Vm=new Vm();
        //$Vm->runMethod('rs_f1'); // run each method
        $Vm->runObj(); // run class
    }

    //shell
    shell> php tests/vm.php

    Log file

    Log file will be created in UNITTEST_PATH/logs with filename like this "Vm_Unittest_YYYYMMDDHHMMSS.log", that is each log file name when you run the each test file like "php vm.php".However AllUnittest will make the log file "AllUnittest_YYYYMMDDHHMMSS.log". "P" and "F" means "test function"'s results , FAILED and PASSED means "class or class method"'s results

    shell> php vm.php
    ---- Start Vm Object Test (2008-08-03 23:54:07)------

    Vm.withoutTemplate = > P P F P FAILED
    Vm.withTemplate = > P F F P P P P P P P FAILED
    Vm.f1 = > F P P P P FAILED
    Vm.rs_f1 = > P P P P PASSED

    ---- End Vm Object Test (2008-08-03 23:54:07)------

    shell> vi tests/logs/Vm_UnitTest_2008080323.log
    ---- Start Vm Object Test (2008-08-03 23:54:07)------

    <- Start Vm.withoutTemplate Method ->
    Vm.typeTest(1,int) ==> P
    Vm.typeTest(1,bool) ==> P
    Vm.typeTest(,object) ==> F
    Vm.matchTest(1,1) ==> P
    * Method Result: FAILED


    <- Start Vm.withTemplate Method ->
    Vm.matchTest(1,1) ==> P
    Vm.matchTest(1,2) ==> F
    Vm.matchTest(2,1) ==> F
    Vm.matchTest(2,2) ==> P
    Vm.matchTest(1,1) ==> P
    Vm.typeTest(2,int) ==> P
    Vm.typeTest(2,integer) ==> P
    Vm.typeTest(2,int) ==> P
    Vm.typeTest(2,int) ==> P
    Vm.typeTest(2,numeric) ==> P
    * Method Result: FAILED

    <- Start Vm.f1 Method ->
    Vm.matchTest(Array,2) ==> F
    Vm.matchTest(1,1) ==> P
    Vm.typeTest(2,integer) ==> P
    Vm.typeTest(2,int) ==> P
    Vm.typeTest(object,object) ==> P
    * Method Result: FAILED

    <- Start Vm.rs_f1 Method ->
    Vm.matchTest(2,2) ==> P
    Vm.matchTest(1,1) ==> P
    Vm.typeTest(2,integer) ==> P
    Vm.typeTest(2,int) ==> P
    * Method Result: PASSED


    ---- End Vm Object Test (2008-08-03 23:54:07)------

    AllUnitTest register class

    In tests/allunittest.php
    registry each one

    class AllUnittest extends Unittest {
    .......
    }

    $AllUnitTest=new AllUnittest();

    $AllUnitTest->regTestClass('vm'); // for each one
    $AllUnitTest->regTestClass('mvc'); // for each one
    $AllUnitTest->allRun(); // execute for all

    registry array

    $AllUnitTest=new AllUnittest();

    $AllUnitTest->regTestClass(array('mvc','vm')); // for array

    //$AllUnitTest->eachObj('mvc'); // execute for each one
    //$AllUnitTest->eachObj('vm'); // execute for each one

    $AllUnitTest->allRun(); // execute for all

    Run AllUnitTest

    "P" and "F" means "test function"'s results , FAILED and PASSED means "class or class method"'s results as same each unit-test.

    shell> php allunittest.php
    * Start AllUnitTest (2008-08-03 23:49:01) *
    Total Class(2) Total Method(8)

    ---- Start Mvc Object Test (2008-08-03 23:49:01)------
    .........
    Mvc.withTemplate = > P P F FAILED
    Mvc.f1 = > P PASSED
    ---- End Mvc Object Test (2008-08-03 23:49:01)------

    ---- Start Vm Object Test (2008-08-03 23:49:01)------
    ................
    ---- End Vm Object Test (2008-08-03 23:49:01)------


    Total Success Class(0) Total Success Method(3), Total Failure Class(2) , Total Failure Method(5)
    * End AllUnitTest (2008-08-03 23:49:01) *

    shell> vi logs/AllUnittest_2008080323.log

    ---- Start Mvc Object Test (2008-08-03 23:49:01)------
    ..............................
    ---- End Mvc Object Test (2008-08-03 23:49:01)------

    ---- Start Vm Object Test (2008-08-03 23:49:01)------
    ..............................
    ---- End Vm Object Test (2008-08-03 23:49:01)------