: This article describes how to integrate ThinkPHP library with PHPUnit. For more information about PHP tutorials, see. Project address: https://github.com/web3d/TPUnit
TPUnit
ThinkPHP PHPUnit framework integration. based on TP3.2, we recommend that you use a PHP 5.4 or later environment.
Unit testing should be the solution to improve PHP coding quality, but few PHP teams should use unit testing to improve the development process, not to mention TDD development methods.
It is undeniable that the ThinkPHP framework has a "highest" position in the domestic PHP circle. Therefore, this integration attempt was made and related practices were also carried out in the team.
1. initial configuration
To reduce learning costs, it is recommended that you use an IDE like Netbeans to set the basic PHPUnit environment.
Through the Netbeans operation interface, you can directly understand some core concepts of the PHPUnit usage process.
- PHPUnit environment configuration based on Netbeans
git clone git@github.com:web3d/TPUnit.gitTo the ThinkPHP Vendor Directory
2. start
Copy the bootstrap. php file under the demo directory in TPUnit to your tests directory.
During the configuration process in the previous step, remember to check and specify the Directory of the bootstrap. php file in the "use guide" area.
Due to the suffix of. class. php in the ThinkPHP framework, the class file to be tested cannot be directly specified in NB to automatically generate the testing method skeleton.
You can temporarily rename the file to remove. class and then use NB to quickly generate the test code skeleton.
3. Supported features
3.1 Basic UnitTest
Classic example:
See the above reference configuration document: Netbeans-based PHPUnit environment configuration
3.2 DB UnitTest
See the demo directory/demo/Application/Common/Model/UrlModelTest. php in this project.
DBUnit consists of four assertions. Currently, TP supports these four assertions:
- Assert the number of data rows in the table
- Assert the table status
- Assert the query result
- Assert the status of multiple tables
See https://github.com/web3d/TPUnit/blob/master/demo/Application/Common/Model/UrlModelTest.php for details
Here, {% url} is the table name prefix replacement solution introduced from the Yii2 framework. {and} are the table name delimiters, and % is the table name prefix placeholder.
The above introduces the PHPUnit integration ThinkPHP library, including the content, hope to be helpful to friends who are interested in PHP tutorials.