Yii Framework official guide series supplemental version 40 -- test: function testing (FunctionalTesting)

Source: Internet
Author: User
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.XyzIndicates the class to be tested.PHPUnit_Extensions_SeleniumTestCaseIs 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.AbcIt 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.phpFile. 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);    }    ......}

WebTestCaseThe 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.phpAs 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 usePostFixture.testShowIn 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.sample1Post 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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.