Functional Testing
Like unit test, a functional test is written in terms of a class XyzTest which extends from CWebTestCase, where Xyz stands for the class being tested. Because PHPUnit_Extensions_SeleniumTestCase is the ancestor class for , we can use all methods inherited from this class.
The functional test class is saved in a PHP file named as XyzTest.php. By convention, the functional test file may be stored under the directory protected/tests/functional.
A test method usually contains a sequence of statements that would issue commands to Selenium RC to interact with the Web application being tested. It also contains assertion statements to verify that the Web application responds as expected.
Before we describe how to write a functional test, let's take a look at the file generated by the yiic webapp
command. This file defines WebTestCase
that may serve as the base class for all functional test classes.
We should also pay attention that in the base test URL, we use index-test.php
as the entry script instead of . The only difference between index-test.php
and index.php
is that the former uses test.php
as the application configuration file while the latter main.php
.
We now describe how to test the feature about showing a post in the blog demo. We first write the test class as follows, noting that the test class extends from the base class we just described:
- class PostTest extends WebTestCase
- {
- 'posts'=>'Post',
- );
- public function testShow()
- {
- $this->open('post/1');
- $this->assertTextPresent($this->posts['sample1']['title']);
- // verify comment form exists
- $this->assertTextPresent('Leave a Comment');
- }
- ......
原文: