Home > Article > Backend Development > Yii Framework Official Guide Series Supplement 37 - Testing: Overview
Testing is an essential part of software development. Whether we realize it or not, when developing Web applications, we are always Testing. For example, when we write a class in PHP, we may use some injection echo
or die
statements to show whether we have implemented a certain method correctly; When we implement a web page that contains a complex set of HTML forms, we may try to enter some test data to confirm whether the page interacts as we expect. More advanced developers will write some code to automatically Complete this testing process so that whenever we need to test something, we only need to call the code and leave the rest to the computer. This is the so-called Automatic Testing, and it is also the main focus of this chapter Topic.
The testing support provided by Yii includes unit testing and functional testing.
Unit testing verifies whether an independent unit of the code works as expected Work. In object-oriented programming, the most basic unit of code is the class. Therefore, the main responsibility of unit testing is to verify that each method implemented by this class works normally. Unit testing is usually performed by the person who developed this class People write it.
Functional testing verifies whether the feature works as expected (for example: a commit operation in a blogging system). Compared with unit testing, functional testing is usually more advanced because of the features to be tested Often involves multiple classes. Functional tests are usually written by people who know the system requirements very well. (This person can be either a developer or a quality engineer).
The following shows the development cycle of the so-called test-driven development (TDD):
Create a new test that covers the feature to be implemented. The test is expected to run the first time Failed during execution because the feature has not been implemented yet.
Execute all tests to ensure that this new test fails.
Write code to Make the tests pass.
Execute all tests and ensure that all tests pass.
Refactor the newly written code and ensure that these tests still pass .
Repeat steps 1 to 5 to promote the implementation of the overall function.
The test support provided by Yii requires PHPUnit 3.5+ and Selenium Remote Control 1.0+. (To install PHPUnit in Linux, please refer to this article: Detailed steps and error solutions for installing PHPUnit in Ubuntu. To install PHPUnit in Windows, please refer to this article: Detailed tutorial on installing Pear and PHPUnit in Windows. The Selenium download address is here: click Download)
When we use the yiic webapp
console command to create a new Yii application, it will generate the following files and directories for us to write and complete tests.
testdrive/ protected/ 包含了受保护的应用文件 tests/ 包含了应用测试 fixtures/ 包含了数据 fixtures functional/ 包含了功能测试 unit/ 包含了单元测试 report/ 包含了 coverage 报告 bootstrap.php 这个脚本在一开始执行 phpunit.xml PHPUnit 配置文件 WebTestCase.php 基于 Web 的功能测试基类
As shown above, our test code is mainly placed in the three directories of fixtures
, functional
and unit
, report
The directory is used to store the generated code coverage report.
We can execute the following command in the console window to execute the test (whether it is a unit test or a functional test):
% cd testdrive/protected/tests % phpunit functional/PostTest.php // 执行单个测试 % phpunit --verbose functional // 执行 'functional' 下的所有测试 % phpunit --coverage-html ./report unit
The above The last command will execute all tests in the unit
directory and generate a code-coverage report in the report
directory. Note that to generate a code-coverage report, PHP must be installed and enabled. xdebug extension.
Let’s take a look at what will be in the bootstrap.php
file. First of all, this file is a bit special, because it looks It looks very much like an entry script, and it is also the entry point for us to execute 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 above As shown, first we include the yiit.php
file from the Yii framework, which initializes some global constants and necessary test base classes. Then we use the test.php
configuration file to Create an application instance. If you look at the test.php
file, you will find that it inherits from the main.php
configuration file, but it adds an extra class named CDbFixtureManager The fixture
application component. 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', ), /* 去除以下注释可为测试提供一个数据库连接. 'db'=>array( 'connectionString'=>'DSN for test database', ), */ ), ) );
When I perform tests that involve database operations, we should provide a dedicated database for testing so that test execution will not interfere with normal development or production activities. In this case, we need to remove the abovedb
Configuration comments, and then fill in the DSN (data source name) of the connectionString
attribute to connect to the database.
With such a startup script, when we execute the unit test, we You can get an application instance similar to the service requirements, and the main difference is that the test has a fixture manager and its own test database.
Testing Series Tutorials:
Yii Framework Official Guide Series 38 - Defining Specific States (Fixtures)
Yii Framework Official Guide Series 39 - Unit Testing (Unit Testing)
Yii Framework Official Guide Series 40 - Testing: Functional Testing Testing)
The above is the content of Yii Framework Official Guide Series Supplement 37 - Testing: Overview. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!