


Yii Framework Official Guide Series Supplement 40 - Testing: Functional Testing
Before reading this chapter, it is highly recommended that you read the Selenium documentation and the PHPUnit documentation. Below we briefly outline the steps for writing functional tests in the Yii framework Basic principles:
Like unit tests, functional tests are written in the form of the XyzTest class that inherits from CWebTestCase, where
Xyz
represents the class being tested. BecausePHPUnit_Extensions_SeleniumTestCase
is the ancestor class of CWebTestCase, we can inherit all methods from this class.The functional test class is saved in the PHP file in the form of XyzTest.php. For convenience, functional test files are usually saved in the
protected/tests/functional folder
.The test class mainly contains a series of test methods named testAbc, among which
Abc
is usually the name of the feature to be tested. For example, if we want to test the user login function, we may have a test method namedtestLogin
.The test method contains a series of command statements used to test the interaction between Selenium RC and the web application. It also contains assertion statements used to confirm the responses we expect from the web application.
Before describing how to write a functional test, let's first look at the WebTestCase.php
file automatically generated through the yiic webapp command. This file defines the base class for all functional test classes WebTestCase:
define('TEST_BASE_URL','http://localhost/yii/demos/blog/index-test.php/'); class WebTestCase extends CWebTestCase { /** * Sets up before each test method runs. * This mainly sets the base URL for the test application. */ protected function setUp() { parent::setUp(); $this->setBrowserUrl(TEST_BASE_URL); } ...... }
WebTestCase
Mainly sets up the test The root URL of the page. Later in the test method we can use relative URLs to specify the page to be tested.
We also need to note that in the test root URL, index-test.php
is used as the entry script instead of index.php
. The difference between the two The only difference is that the former uses test.php as the application configuration file, while the latter uses main.php
.
Now we start to tell how to test how to display an article in the blog demo. A functional feature. First write the test class as written. Note that the test class inherits from the base class WebTestCase we mentioned above:
class PostTest extends WebTestCase { public $fixtures=array( 'posts'=>'Post', ); public function testShow() { $this->open('post/1'); // verify the sample post title exists $this->assertTextPresent($this->posts['sample1']['title']); // verify comment form exists $this->assertTextPresent('Leave a Comment'); } ...... }
is the same as writing unit tests. , we first declare the specific states (fixtures) used in this test. Here we specify the use of Post
fixture. In the testShow
test method, we first use Selenium RC to open the URL post/1
. Note that this is a relative URL. The complete URL is concatenated with the root URL in the base class (i.e. http://www.php.cn/
). Then we verify Whether the title of sample1
post can be found in the current page. We can also verify whether this page contains the text Leave a Comment
.
Tip: Before running functional tests, Selenium-RC server must be started first. This can be done by executing the command java -jar selenium-server.jar
## in your Selenium server installation directory # to fulfill.
The above is the content of Yii Framework Official Guide Series Supplement 40 - Testing: Functional Testing. Please pay attention to more related content. PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Yii is a high-performance MVC framework based on PHP. It provides a very rich set of tools and functions to support the rapid and efficient development of web applications. Among them, the RESTful API function of the Yii framework has attracted more and more attention and love from developers, because using the Yii framework can easily build high-performance and easily scalable RESTful interfaces, providing strong support for the development of web applications. . Introduction to RESTfulAPI RESTfulAPI is a

Yii framework middleware: providing multiple data storage support for applications Introduction Middleware (middleware) is an important concept in the Yii framework, which provides multiple data storage support for applications. Middleware acts like a filter, inserting custom code between an application's requests and responses. Through middleware, we can process, verify, filter requests, and then pass the processed results to the next middleware or final handler. Middleware in the Yii framework is very easy to use

GitLab's integration testing function and common use cases [Introduction] In the software development process, testing is an indispensable link. In the development environment of continuous integration and continuous delivery, integration testing plays a vital role. As a popular code hosting platform, GitLab not only provides version management and collaboration tools, but also provides rich integration testing functions. This article will introduce GitLab's integration testing function in detail and provide common test cases and code examples. [GitLab integrated testing function]G

With the rapid development of web applications, modern web development has become an important skill. Many frameworks and tools are available for developing efficient web applications, among which the Yii framework is a very popular framework. Yii is a high-performance, component-based PHP framework that uses the latest design patterns and technologies, provides powerful tools and components, and is ideal for building complex web applications. In this article, we will discuss how to use Yii framework to build web applications. Install Yii framework first,

Steps to implement web page caching and page chunking using the Yii framework Introduction: During the web development process, in order to improve the performance and user experience of the website, it is often necessary to cache and chunk the page. The Yii framework provides powerful caching and layout functions, which can help developers quickly implement web page caching and page chunking. This article will introduce how to use the Yii framework to implement web page caching and page chunking. 1. Turn on web page caching. In the Yii framework, web page caching can be turned on through the configuration file. Open the main configuration file co

Tips and experience sharing on the use of PHP code testing function When developing PHP applications, code testing is a very important link. Code testing can check and verify the correctness of the code to ensure the stable operation of the program. This article will introduce some tips and experiences in PHP code testing to help developers better conduct code testing. Using the unit testing framework Unit testing is a test for each independent functional module in the program. Using a unit testing framework simplifies the testing process and provides some powerful assertion and test result reporting

In recent years, with the rapid development of the game industry, more and more players have begun to look for game strategies to help them pass the game. Therefore, creating a game guide website can make it easier for players to obtain game guides, and at the same time, it can also provide players with a better gaming experience. When creating such a website, we can use the Yii framework for development. The Yii framework is a web application development framework based on the PHP programming language. It has the characteristics of high efficiency, security, and strong scalability, and can help us create a game guide more quickly and efficiently.

Yii framework middleware: Add logging and debugging capabilities to applications [Introduction] When developing web applications, we usually need to add some additional features to improve the performance and stability of the application. The Yii framework provides the concept of middleware that enables us to perform some additional tasks before and after the application handles the request. This article will introduce how to use the middleware function of the Yii framework to implement logging and debugging functions. [What is middleware] Middleware refers to the processing of requests and responses before and after the application processes the request.
