Before reading this section, we strongly recommend that you read Selenium and PHPUnit documents first. next we will give a brief overview of the basic principles for writing function tests in the Yii Framework: Like unit tests, function tests inherit from CWebTestCas...
Before reading this section, we strongly recommend that you read the Selenium document and PHPUnit document. The following describes the basic principles for functional testing in the Yii Framework:
Like unit testing, function testing is written in the form of XyzTest that inherits the child from CWebTestCase.Xyz
Indicates the class to be tested.PHPUnit_Extensions_SeleniumTestCase
Is the ancestor class of CWebTestCase. we can inherit all methods from this class.
The function test class is stored in the php file in the form of XyzTest. php. for convenience, the function test file is usually stored inUnder the protected/tests/functional folder
.
The test class mainly contains a series of test methods named testAbc.Abc
It is usually the name of the feature to be tested. for example, to test the user login function, we may have a name namedTest Method of testLogin
.
The test method contains a series of command statements used to test the interaction between Selenium RC and web applications. It also contains the asserted statements used to confirm the web application response we expected.
Before describing how to write a function test, let's take a look atWebTestCase.php
File. This file defines the base class as all functional test classesWebTestCase:
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. in the test method, you can use relative URLs to specify the page to be tested.
We should also note that in the Test root URL,index-test.php
As an entry script insteadindex.php
. The only difference between the two is that the former uses test. php as the application configuration file, while the latter usesmain.php
.
Now let's start to talk about how to test and display the feature of an article in the blog demonstration. First, write a Test class for example. Note that the test class inherits from the base class WebTestCase 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'); } ......}
Like writing a unit test, we first declare the specific state (fixtures) used in this Test. here we specify to usePost
Fixture.testShow
In the test method, we first use Selenium RC to open the URLpost/1
. Note that this is a relative URL. the complete URL is spliced with the root URL (I. e.#
). Then we verify whether it can be found on the current page.sample1
Post title. we can also verify whether the page contains textLeave a Comment
.
Tip:Before running the function test, Selenium-RC server must be started first. this can be done by running the command in your Selenium server installation directory.java -jar selenium-server.jar
.
The above is the 40-Test: Functional Testing content of the Yii Framework Official Guide series. For more information, see The PHP Chinese website (www.php1.cn )!