Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

test documentation.

  • Loading branch information...
commit 1fb0b805f5743594676f4cc09b9bb54dfb5429d6 1 parent 2a95d75
qiang.xue authored
View
BIN  demos/blog/protected/data/blog-test.db
Binary file not shown
View
2  demos/blog/protected/tests/readme.txt
@@ -8,6 +8,8 @@ This directory contains unit and functional tests for the blog demo.
- unit: contains unit test cases.
+ - report: contains any coverage reports.
+
In order to run these tests, the following requirements must be met:
View
0  demos/blog/protected/tests/report/.yii
No changes.
View
5 demos/blog/protected/tests/unit/PostTest.php
@@ -10,4 +10,9 @@ class PostTest extends CDbTestCase
'posts'=>'Post',
'comments'=>'Comment',
);
+
+ public function testSave()
+ {
+ // write code here to test post saving method
+ }
}
View
4 docs/guide/test.fixture.txt
@@ -1,4 +1,8 @@
Defining Fixtures
=================
+One of the most time-consuming parts of writing tests is preparing the environment so that the tests start execution in some known state. This known state is called the *fixture* of the tests.
+
+
+
<div class="revision">$Id$</div>
View
57 docs/guide/test.overview.txt
@@ -1,5 +1,62 @@
Overview
========
+> Note: The testing support described in this chapter requires Yii version 1.1 or higher. This does not mean, however, that you cannot test applications developed using Yii 1.0.x. There are many great testing frameworks available to help you accomplish this task, such as [PHPUnit](http://www.phpunit.de/), [SimpleTest](http://www.simpletest.org/).
+
+Testing is an indispensable process of software development. Whether we are aware of it or not, we conduct testing all the time when we are developing a Web application. For example, when we write a class in PHP, we may use some `echo` or `die` statement to show that we implement a method correctly; when we implement a Web page containing a complex HTML form, we may try entering some test data to ensure the page interacts with us as expected. More advanced developers would write some code to automate this testing process so that each time when we need to test something, we just need to call up the code and let the computer to perform testing for us. This is known as *automated testing*, which is the main topic of this chapter.
+
+The testing support provided by Yii includes *unit testing* and *functional testing*.
+
+A unit test verifies that a single unit of code is working as expected. In object-oriented programming, the most basic code unit is a class. A unit test thus mainly needs to verify that each of the class interface methods works properly. That is, given different input parameters, the test verifies the method returns expected results. Unit tests are usually developed by people who write the classes being tested.
+
+A functional test verifies that a feature (e.g. post management in a blog system) is working as expected. Compared with a unit test, a functional test sits at a higher level because a feature being tested often involves multiple classes. Functional tests are usually developed by people who know very well the system requirements (they could be either developers or quality engineers).
+
+
+Test-Driven Development
+=======================
+
+Below we show the development cycles in the so-called [test-driven development (TDD)](http://en.wikipedia.org/wiki/Test-driven_development):
+
+ 1. Create a new test that covers a feature to be implemented. The test is expected to fail at its first execution because the feature has yet to be implemented.
+ 2. Run all tests and make sure the new test fails.
+ 3. Write code to make the new test pass.
+ 4. Run all tests and make sure they all pass.
+ 5. Refactor the code that is newly written and make sure the tests still pass.
+
+Repeat step 1 to 5 to push forward the functionality implementation.
+
+
+Test Environment Setup
+======================
+
+The testing supported provided by Yii requires [PHPUnit](http://www.phpunit.de/) 3.3+ and [Selenium Remote Control](http://seleniumhq.org/projects/remote-control/) 1.0+.
+
+When we use the `yiic webapp` console command to create a new Yii application, it will generate the following files and directories for us to write and perform new tests:
+
+~~~
+testdrive/
+ protected/ containing protected application files
+ tests/ containing tests for the application
+ fixtures/ containing database fixtures
+ functional/ containing functional tests
+ unit/ containing unit tests
+ report/ containing coverage reports
+ bootstrap.php the script executed at the very beginning
+ phpunit.xml the PHPUnit configuration file
+ WebTestCase.php the base class for Web-based functional tests
+~~~
+
+As shown in the above, our test code will be mainly put into three directories: `fixtures`, `functional` and `unit`, and the directory `report` will be used to store the generated code coverage reports.
+
+To execute tests (whether unit tests or functional tests), we can execute the following commands in a console window:
+
+~~~
+% cd testdrive/protected/tests
+% phpunit functional/PostTest.php <-- executes an individual test
+% phpunit --verbose functional <-- executes all tests under 'functional'
+% phpunit --coverage-html ./report unit
+~~~
+
+In the above, the last command will execute all tests under the `unit` directory and generate a code-coverage report under the `report` directory. Note that [xdebug extension](http://www.xdebug.org/) must be installed and enabled in order to generate code-coverage reports.
<div class="revision">$Id$</div>
View
0  framework/cli/views/webapp/protected/tests/report/.yii
No changes.
View
2  framework/test/CDbTestCase.php
@@ -36,7 +36,7 @@
* @package system.test
* @since 1.1
*/
-class CDbTestCase extends CTestCase
+abstract class CDbTestCase extends CTestCase
{
/**
* @var array a list of fixtures that should be loaded before each test method executes.
View
2  framework/test/CTestCase.php
@@ -18,6 +18,6 @@
* @package system.test
* @since 1.1
*/
-class CTestCase extends PHPUnit_Framework_TestCase
+abstract class CTestCase extends PHPUnit_Framework_TestCase
{
}
View
2  framework/test/CWebTestCase.php
@@ -21,7 +21,7 @@
* @package system.test
* @since 1.1
*/
-class CWebTestCase extends PHPUnit_Extensions_SeleniumTestCase
+abstract class CWebTestCase extends PHPUnit_Extensions_SeleniumTestCase
{
/**
* @var array a list of fixtures that should be loaded before each test method executes.
Please sign in to comment.
Something went wrong with that request. Please try again.