Automatic testing needs to be executed many times. to ensure that the test process can be repeated, we would like to perform the test in some known states. this state is called a specific state. for example, test document creation in a blog application...
The automatic test needs to be executed many times. to ensure that the test process can be repeated, we would like to perform the test in some known states. this state is calledSpecific Status. For example, test the document creation feature in a blog application. each time we perform a test, the table related to the article (for example.Post
Table,Comment
Table) should be restored to a specific state. the PHPUnit document describes the construction of a specific state. this section describes how to build a specific database status as described in the preceding example.
Set the specific status of the database to be built. this is probably one of the most time-consuming parts of the application that uses the database as the backend support. the CBbFixtureManager application component introduced by Yii can effectively alleviate this problem. when performing a group of tests, it basically does the following:
Before all tests are run, it resets test-related data to a known state.
Before a single test is run, it resets a specific table to a known state.
During the execution of a test method, it provides an access interface to provide row data in a specific state.
Use the CDbFixtureManager configured in application configuration as follows.
return array( 'components'=>array( 'fixture'=>array( 'class'=>'system.test.CDbFixtureManager', ), ),);
Then, in the directoryprotected/tests/fixtures
Provides a specific status data. this directory can be specified as another directory by configuring the CDbFixtureManager: basePath attribute in the application configuration file. specific state data is composed of multiple php files called specific state files. each specific status file returns an array representing the initial row of a specific table of data. the file name is the same as the table name. in the following examplePost
The table's specific status data is stored inPost.php
Example in the file.
array( 'title'=>'test post 1', 'content'=>'test post content 1', 'createTime'=>1230952187, 'authorId'=>1, ), 'sample2'=>array( 'title'=>'test post 2', 'content'=>'test post content 2', 'createTime'=>1230952287, 'authorId'=>1, ),);
As we can see, the above two rows of data are returned. Each row represents an array. its key is the field name of the table, and its value is the corresponding field value.Row alias(For example:simple1
,simple2
). Later, when writing a test script, we can call this line of data through its alias. we will introduce this in detail in the next section.
You may have noticed that we did not specifyid
Field value. this is becauseid
The field has been defined as the auto-increment primary key, and its value will be automatically generated when we insert new data.
When CDbFixtureManager is referenced for the first time, it will carefully check all the specific state files and use them to reset the corresponding table. it clears the table, resets the auto-incrementing sequence value of the table's primary key, and inserts data rows from a specific state file into the table to reset the table.
Sometimes, we may not want to reset each table described in a specific state file before a set of tests, because resetting too many specific state files may take a lot of time. in this case, we can write a PHP script to customize the initialization process. this script should be saved in the directory where the specified state file is stored and namedinit.php
. When CDbFixtureManager detects the existence of this script, it will execute this script instead of resetting every table.
You do not like to reset the table by default. for example, you can clear the table and insert data in the specified status. in this case, we can write an initialization script for the specified state file. this script must be named table name +.init.php
. Example:Post
The table initialization script file isPost.init.php
. When CDbFixtureManager discovers this script, it will execute this script instead of resetting the table by default.
Tip:Too many specific state files greatly extend the test time. therefore, you should only provide specific state files for tables whose data changes during the test. the tables used as search services do not change, so no specific state files are required.
In the next two sections, we will talk about how to use the specific status managed by CDbFixtureManager in unit testing and functional testing.
The above is the content of the Yii Framework official guide series 38-defining specific statuses (Fixtures). For more information, see PHP Chinese website (www.php1.cn )!