thinkPHP Framework Unit Test Library tpunit Usage Sample

  • 2021-11-01 02:23:41
  • OfStack

This paper illustrates the usage of tpunit, a unit test library of thinkPHP framework. Share it for your reference, as follows:

thinkphp itself does not provide corresponding unit testing support, so here is a library tpunit that can unit test tp.

The Tpunit library is dependent on phpunit and is characterized by ease of use, convenience, and non-intrusion.

Using TPUNIT is very simple, just define a path constant and import the file to be tested.

There is a controller under the Home module as follows:


namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
  function test(){
    echo 123;
  }
}

With tpunit, test the class:


class IndexTest extends PHPUnit_Framework_TestCase{
  // Constructor 
  function __construct(){
    // Definition TP Version of 
    define('TPUNIT_VERSION','3.2.3');
    // Define directory paths, preferably absolute paths 
    define('TP_BASEPATH', 'E:/www/novel/');
        // Import base Library 
        include_once'E:\www\novel\Application\test\base.php';
        // Import the controller to test 
        include_once'E:\www\novel\Application\Home\Controller\IndexController.php';
  }
  // Test index Action 
  public function testIndex(){
    // New Controller 
    $index=new \Home\Controller\IndexController();
        // Invoke the method of the controller 
        $index->test();
        // Assertion 
        $this->expectOutputString('123');
  }
}

Then use:


phpunit E:\\www\\novel\\Application\\test\\IndexTest.php

The test can be carried out automatically.

In use, it is almost the same as using phpunit. After use, you can carry out unit test on models, controllers and so on.

TPUNIT library github download address (detailed instructions):

https://github.com/CODE7070/TPUNIT

Or click here to download it.

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: