Yii Framework official guide series Updated version 37 -- test: overview

Source: Internet
Author: User
Testing is an essential part of software development. whether or not we realize that we are always testing Web applications during development. for example, when we use PHP to write a class, we may use some injection echo or die...



Testing is an essential part of software development. whether or not we realize that we are always testing Web applications during development. for example, when we use PHP to write a class, we may use some injectionechoOrdieStatement to show whether a method is correctly implemented. when we implement a web page containing a complex HTML form, we may try to enter some test data to check whether the page interacts as expected. more advanced developers will write some code to automatically complete the test process, so that whenever we need to test something, we only need to call the code, and the rest will be handed over to the computer. this is the so-calledAutomatic testIs also the main topic of this chapter.

Test support provided by Yii includesUnit testAndFunction testing.

Unit testing checks whether an independent unit of the code works as expected. in object-oriented programming, the most basic code unit is class. therefore, the main responsibility of unit testing is to verify that every method implemented by this class works normally. unit tests are usually written by people who develop this class.

The function test checks whether the features work as expected (for example, submitting operations in a blog system ). compared with unit testing, functional testing is generally more advanced, because the features to be tested often involve multiple classes. functional testing is usually written by people who have a good understanding of system requirements. (this person can be either a developer or a quality engineer ).

1. test-driven development

The following shows the development cycle of the so-called Test-Driven Development (TDD:

  1. Create a new test that covers the features to be implemented. the test is expected to fail during the first execution because the feature has not yet been implemented.

  2. Execute all tests to make sure the new test fails.

  3. Write code to make the test pass.

  4. Execute all tests to make sure all tests pass.

  5. Refactor the newly written code and make sure these tests still pass.

Repeat steps 1 to 5 to implement the overall function.

2. build a test environment

The test support provided by Yii requires PHPUnit 3.5 + and Selenium Remote Control 1.0 +. (For details about how to install PHPUnit in Linux, refer to this article: detailed steps for installing PHPUnit in Ubuntu and solutions to errors. for details about how to install PHPUnit in Windows, refer to this article: for more information about installing Pear and PHPUnit in windows, click here to download Selenium)

When we useyiic webappWhen the console command is used to create a new Yii application, it will generate the following files and directories for us to compile and complete the test.

Testdrive/protected/contains the protected application file tests/contains the application test fixtures/contains the data fixtures functional/contains the function test unit/contains the unit test report/contains the coverage report bootstrap. the php script runs phpunit at the beginning. xml PHPUnit configuration file WebTestCase. php Web-based functional testing base class

As shown above, our test code is mainlyfixtures,functionalAndunitUnder these three directories,reportThe directory is used to store the generated code coverage report.

You can run the following command in the console to perform the test (unit test or function test ):

% Cd testdrive/protected/tests % phpunit functional/PostTest. php // execute a single Test % phpunit -- verbose functional // execute all the tests under 'functional' % phpunit -- coverage-html. /report unit

The Last Command above will be executedunitAll tests under the Directory are thenreportGenerate a code-coverage report under the Directory. Note that the code-coverage report must be installed and the PHP xdebug extension must be enabled.

3. pilot script for testing

Let's take a lookbootstrap.phpWhat will happen in the file. first, this file is a bit special, because it looks like an entry script, and it is also the entry for executing a series of tests.


$yiit='path/to/yii/framework/yiit.php';$config=dirname(__FILE__).'/../config/test.php';require_once($yiit);require_once(dirname(__FILE__).'/WebTestCase.php');Yii::createWebApplication($config);

As shown above, we first includeyiit.phpFile, which initializes some global constants and necessary test base classes. then we usetest.phpThis configuration file is used to create an application instance.test.phpFile, you will find that it is inherited frommain.phpThis configuration file only adds a class named CDbFixtureManagerfixtureApplication components. we will introduce fixtures in detail in the next section.


Return CMap: mergeArray (require (dirname (_ FILE __). '/main. php '), array ('components' => array ('fixture' => array ('class' => 'system. test. CDbFixtureManager ',),/* Remove the following comments to provide a database connection for the test. 'DB' => array ('ononstring' => 'dsn for test database ',),*/),));

When I perform tests involving database operations, we should provide a dedicated database for testing so that the test execution will not interfere with normal development or production activities. in this way, we need to remove the abovedbConfigure the annotation, and then enterconnectionStringThe DSN (data source name) used to connect to the database.

With such a startup script, when we perform a unit test, we can obtain an application instance that is similar to the service requirements, the main difference is that the test has a fixture manager and its exclusive test database.

Test series tutorials:

Yii Framework official guide series 38-define specific states (Fixtures)

Yii Framework Official Guide Series 39-Unit Testing)

Yii Framework Official Guide Series 40-Test: function Testing)

The above is the version 37 of the Yii Framework official guide series-test: overview content. For more information, see PHP Chinese website (www.php1.cn )!

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.