Home  >  Article  >  Backend Development  >  Yii Framework Official Guide Series Supplement 40 - Testing: Functional Testing

Yii Framework Official Guide Series Supplement 40 - Testing: Functional Testing

黄舟
黄舟Original
2017-02-16 09:13:331037browse



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. Because PHPUnit_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 named testLogin.

  • 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);
    }

    ......
}

WebTestCaseMainly 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)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn