Home > PHP Framework > YII > How to write unit tests in Yii

How to write unit tests in Yii

(*-*)浩
Release: 2019-11-05 15:04:45
Original
3327 people have browsed it

Unit tests

Unit tests are located in the tests/unit directory and should contain all types of unit and integration tests.

How to write unit tests in Yii

Each test case extends the Codeception\Test\Unit class, which is the standard Codeception format for unit testing. It is very difficult to develop fully isolated unit tests in Yii, so an application is started before each test case.                                                                                                                             (Recommended learning: yii tutorial )

Configure the test in the file with the Yii2 module enabled in tests/unit.suite.yml:

modules:
    enabled:
      - Yii2:
            part: [orm, email]
Copy after login

This module is A test case launches the Yii application and provides additional helper methods to simplify testing. It only has ORM and email parts, to rule out the need for only functional testing methods.

You can use the methods of the Yii2 module by accessing the class in the $this->tester test case. So if the orm and email parts are enabled, you can call methods belonging to these parts:

<?php
// insert records in database
$this->tester->haveRecord(&#39;app/model/User&#39;, [&#39;username&#39; => &#39;davert&#39;]);
// check records in database
$this->tester->seeRecord(&#39;app/model/User&#39;, [&#39;username&#39; => &#39;davert&#39;]);
// test email was sent
$this->tester->seeEmailIsSent();
// get a last sent emails
$this->tester->grabLastSentEmail();
Copy after login

If the fixtures part is enabled, you will also get methods to load and use fixtures in your tests:

<?php
// load fixtures
$this->tester->haveFixtures([
    &#39;user&#39; => [
        &#39;class&#39; => UserFixture::className(),
        // fixture data located in tests/_data/user.php
        &#39;dataFile&#39; => codecept_data_dir() . &#39;user.php&#39;
    ]
]);
// get first user from fixtures
$this->tester->grabFixture(&#39;user&#39;, 0);
Copy after login

If Yii2 has the module enabled, it is safe to call Yii::$app within a test because the application will be initialized and cleaned up after the test. If you want to add helper methods or custom assertions for your test cases, you should not extend Codeception\Test\Unit, but write your own standalone helper class.

The above is the detailed content of How to write unit tests in Yii. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
yii
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template