Yii2 unit test usage example, yii2 unit test example
This example describes the Yii2 unit test usage. We will share this with you for your reference. The details are as follows:
After installing the yii2-app-basic (https://github.com/yiisoft/yii2-app-basic/blob/master/README.md) in composer mode, you can use
Create a Model file EntryForm. php In the models directory.
<?phpnamespace app\models;use Yii;use yii\base\Model;class EntryForm extends Model{ public $name; public $email; public function rules() { return [ [['name', 'email'], 'required'], ['email', 'email'], ]; }}
Create an EntryFormTest. php file in the tests/unit/models directory.
<? Phpnamespace tests \ models; use app \ models \ EntryForm; class EntryFormTest extends \ Codeception \ Test \ Unit {public function testValidInput () {$ model = new EntryForm (); $ model-> name = 'Harry qin'; $ model-> email = '2017 @ qq.com '; expect_that ($ model-> validate (); return $ model ;} public function testInvalidInput () {$ model = new EntryForm (); $ model-> name = 'Harry qin'; $ model-> email = 'xxyy '; expect_not ($ model-> validate (); $ model = new EntryForm (); $ model-> name = ''; $ model-> email = '2017 @ qq.com '; expect_not ($ model-> validate ());} /*** the following line indicates that the input parameter value comes from the output of testValidInput * @ depends testValidInput */public function testModelProperty ($ model) {keep CT ($ model-> name) -> equals ('Harry qin ');}}
Run in the root directory of the project
composer exec codecept run unit
Output
。。。。。。✔ EntryFormTest: Valid input (0.00s)✔ EntryFormTest: Invalid input (0.00s)✔ EntryFormTest: Model property (0.00s)
All of them are successful. If the test fails, the specific failure information is displayed.
Here are three methods.
Expect_that: Assuming true
Expect_not: false
Verify CT: assume that the target object can be followed by the verify method. The specific method list is in the file vendor/codeception/Verify/src/Codeception/verify. php.