Yii2 unit test usage example, yii2 unit test example

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.