<commit_before>Functional Testing
==================

Before reading this section, it is recommended that you read the [Selenium documentation](http://seleniumhq.org/docs/) and the [PHPUnit documentation](http://www.phpunit.de/wiki/Documentation) first. We summarize in the following the basic principles of writing a functional test in Yii:

 * 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 [CWebTestCase], 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 unit test file may be stored under the directory `protected/tests/functional`.

 * The test class mainly contains a set of test methods named as `testAbc`, wherer `Abc` is often the name of a feature to be tested. For example, to test the user login feature, we can have a test method named as `testLogin`.

 * 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.

<div class="revision">$Id$</div><commit_msg>* completed the implementation and documentation for the testing framework.<commit_after>Functional Testing
==================

Before reading this section, it is recommended that you read the [Selenium documentation](http://seleniumhq.org/docs/) and the [PHPUnit documentation](http://www.phpunit.de/wiki/Documentation) first. We summarize in the following the basic principles of writing a functional test in Yii:

 * 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 [CWebTestCase], 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 unit test file may be stored under the directory `protected/tests/functional`.

 * The test class mainly contains a set of test methods named as `testAbc`, wherer `Abc` is often the name of a feature to be tested. For example, to test the user login feature, we can have a test method named as `testLogin`.

 * 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 `WebTestCase.php` file generated by the `yiic webapp` command. This file defines `WebTestCase` that may serve as the base class for all functional test classes.

~~~
[php]
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);
	}

	......
}
~~~

The class `WebTestCase` mainly sets the base URL of the pages to be tested. Later in test methods, we can use relative URLs to specify which pages to be tested.

We should also pay attention that in the base test URL, we use `index-test.php` as the entry script instead of `index.php`. 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](http://www.yiiframework.com/demos/blog). We first write the test class as follows, noting that the test class extends from the base class we just described:

~~~
[php]
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 class, we declare the fixtures to be used by this test. Here we indicate that the `Post` fixture should be used. In the `testShow` test method, we first instruct Selenium RC to open the URL `post/1`. Note that this is a relative URL, and the complete URL is formed by appending it to the base URL we set in the base class (i.e. `http://localhost/yii/demos/blog/index-test.php/post/1`). We then verify that we can find the title of the `sample1` post can be found in the current Web page. And we also verify that the page contains the text `Leave a Comment`.


<div class="revision">$Id$</div>
{'fraction_non_alphanumeric': 0.08351825701028848, 'fraction_numerical': 0.0010086746015735323, 'mean_word_length': 4.394994559303591, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1439691', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1247, 'n_words': 686}