Before reading this section, it is highly recommended that you read the selenium documentation and the PHPUnit documentation first. Below we briefly outline the basic principles of writing functional tests in the YII framework:
As with unit tests, functional testing is written in the form of a Xyztest class that inherits from Cwebtestcase, which Xyz
represents the class being tested. Because PHPUnit_Extensions_SeleniumTestCase
it is the ancestor class of cwebtestcase, we can inherit all the methods from this class.
Functional test classes are saved in PHP files as xyztest.php. For convenience, functional test files are usually saved in the protected/tests/functional文件夹下
.
The test class consists mainly of a series of test methods named Testabc, which Abc
are usually the names of the attributes to be tested, for example, to test the user login function, we may have a named testLogin的测试方法
.
The test method contains a series of command statements to test the interaction between the Selenium RC and the Web application. It also contains an assertion statement that confirms what we expect the Web app to reply to.
Before describing how to write a functional test, let's take a look at the files generated automatically by the YIIC webapp command WebTestCase.php
. 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
The root URL of the test page is set primarily, and later in the test method we can use relative URLs to specify the page to test.
We also note that in the test root URL, it is used index-test.php
as a portal script instead of index.php
. The only difference between the two is that the former uses test.php as the application configuration file, which is used by the latter main.php
.
Now we're going to tell you how to test the functionality of displaying an article in a blog presentation. First write the test class as written, notice that the test class inherits from the base class Webtestcase we described 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 '); } ......}
As with writing unit tests, we first declare the specific state (fixtures) used in this test. Here we specify the use of Post
fixture. In the testShow
test method, we first use the Selenium RC to open the URL post/1
. Note that this is a relative URL, the complete URL is the root URL (i.e.) that is stitched up in the base class. http://www.php.cn/
then we verify that the title of the post can be found in the current page. sample1
We can also verify that the page contains text Leave a Comment
.
Tip: before running a functional test, SELENIUM-RC server starts. This can be done by executing commands in your Selenium server installation directory java -jar selenium-server.jar
to achieve.
Above is the YII Framework Official Guide Series Supplement 40--test: Functional Test (functional testing) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!