Yii Testing - Yii

How Yii Testing is done?

When a PHP class is written, step by step it is debugged or die or echo statements method is used to check how it is working. When a web application is developed, the test data is entered in forms to ensure working of the page as desired and expected. The test process can be automated.

Automatic test approach is used for long term projects like:

  • Complex and large
  • Grows constantly
  • Too expensive in terms of cost of the failure

How to Prepare for the Yii Tests?

Step 1 − Install the Codeception framework. Run the following code.

Step 2 − Run the following.

The output is “Changed current directory to <directory>”. You should add the '<directory>/vendor/bin' to your PATH variable. In this case, run the following code −

Step 3 – A new database 'yii2_basic_tests' is created.

Step 4 − Inside the tests directory run

The database configuration can be found at tests/codeception/config/config.php.

Step 5 – Test suites are built through

What are Yii Fixtures?

Yii Fixtures facilitate in running the tests in an expected way by setting the environment in an unknown state. A near fixture framework is provided by Yii. A key concept of the Yii fixture framework is the fixture object. It represents a particular aspect of a test environment. The fixture object is an instance of the yii estFixture class.

A fixture is defined by creating a new class and extending it from yii estFixture or yii estActiveFixture. The former is better for general purpose fixtures while the latter is specifically designed to work with database and ActiveRecord.

What are Yii Unit Tests?

Individual functions like model functions or a component class are tested by Unit tests.

Step 1 − A new fixture is created in the file ExampleFixture.php under the tests/codeception/fixtures directory.

Step 2 − A new test file ExampleTest.php is created in the tests/codeception/unit/models folder.

Three tests are defined in the above code −

  • testCreateMyUser,
  • testUpdateMyUser, and
  • testDeleteMyUser.

A new user is created and the name is updated and try to delete. The MyUser model is managed in terms of the yii2_basic_tests database, which is a complete copy of our real DB.

Step 3 − Tests, are started by moving to the tests folder and run.

Once it passes all the tests, the following output appears:

Unit Tests

How does Yii Functional Tests help?

Functional tests help in −

  • testing the application using browser emulator
  • verify that the function works properly
  • interact with the database
  • submit data to server-side scripts

Run the following in the tests folder -

A file AboutPageCept.php is created under tests/codeception/functional folder. In the functional test the existence of about page is checked.

Step 1 −The AboutPageCept.php file is modified by:

In the above given code, we checked whether we are on the about page. Obviously, the word 'about' should appear on the page.

Step 2 − Run the test through:

The output appears as:

Run Unit Tests

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Yii Topics