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 injectionecho
Ordie
Statement 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:
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.
Execute all tests to make sure the new test fails.
Write code to make the test pass.
Execute all tests to make sure all tests pass.
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 webapp
When 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
,functional
Andunit
Under these three directories,report
The 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 executedunit
All tests under the Directory are thenreport
Generate 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.php
What 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.php
File, which initializes some global constants and necessary test base classes. then we usetest.php
This configuration file is used to create an application instance.test.php
File, you will find that it is inherited frommain.php
This configuration file only adds a class named CDbFixtureManagerfixture
Application 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 abovedb
Configure the annotation, and then enterconnectionString
The 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 )!