© 2015 X2Engine Inc.
Difference between revisions of "Testing With PHPUnit"
(→What are fixtures and init scripts?) |
|||
Line 57: | Line 57: | ||
| * At the beginning of tests in test cases that use them, and: | | * At the beginning of tests in test cases that use them, and: | ||
* At the very beginning of a test suite (before any tests run) | * At the very beginning of a test suite (before any tests run) | ||
+ | |- | ||
! scope="row" | Structure of the returned array | ! scope="row" | Structure of the returned array | ||
| <tt>array(\[row alias name\] => array( \[column name\] => \[column value\]))</tt> | | <tt>array(\[row alias name\] => array( \[column name\] => \[column value\]))</tt> | ||
| <tt>array( array( \[column name\] => \[column value\]) )</tt> | | <tt>array( array( \[column name\] => \[column value\]) )</tt> | ||
+ | |- | ||
} | } | ||
Revision as of 02:03, 30 January 2013
This page covers development using unit and functional tests to develop components and extensions to X2CRM. While not all of X2CRM has been built using test-driven development, some tools and structure have been developed to make it easier to use unit and functional testing when developing new features. All automated tests in X2CRM use PHPUnit, and in the case of functional testing, Selenium. To understand more what these tools are, why it is important to know about them, and how they will strengthen your design and development methodology (in addition to helping you make your code more stable), the following is recommended reading:
- PHPUnit Manual, Ch. 1: Automating Tests
- PHPUnit Manual, Ch. 2: PHPUnit's Goals
- Selenium: Introduction
Introduction
What are tests?
Tests are organized into PHP classes called test cases that are stored within protected/tests/unit (unit tests) or protected/tests/functional (functional tests). Test cases extend one of the following classes:
- yii:CTestCase
- For ordinary, non-databse unit tests; useful for defining expected behaviors of component classes that don't rely on tables or data that is specific to a HTTP request
- yii:CDbTestCase
- For tests involving communication with a MySQL database, especially if that communication involves a change in any data.
- x2doc:WebTestCase (extends CWebTestCase)
- For tests that can involve web browser actions and change in the persistent data of the web app.
Running test cases
Running a test case proceeds as follows: on the server where the testing environment is installed, inside of the protected/tests folder, runphpunit path/to/TestCase.phpor, to run a group of test cases that exist in the same directory:
phpunit directory/PHPUnit will recursively scan directories for test cases to run.
Installing PHPUnit
For the most up-to-date information on how to install PHPUnit, see Chapter 3: Installing PHPUnit in the official PHPUnit manual. In addition to PHPUnit, you will need each of the following PHPUnit extensions:
- DBUnit
- PHP_Invoker
- PHPUnit_Selenium
- PHPUnit_Story
See the installation guide for more information on how to obtain these extensions.
Setting up a testing database
A few very important points to note about tests involving a database connection:
- A different database will be used than the live database
- The contents of each of the test database's tables will be fixed at the beginning of each test using fixtures
- All database records generated during tests or through manual interaction will be purged when fixtures for the table in question are used
After creating the test database, the following steps can be used to set it up for database testing:
- Copy the installer files back into the root of the web application: index.php, initialize.php, requirements.php, and initialize_pro.php if on professional edition.
- Re-run the installer, but enable "Testing Database".
What are fixtures and init scripts?
When writing tests that extend CDbTestCase or WebTestCase, it is important to define fixtures if the test will change any data in the database. Fixtures are PHP scripts stored in protected/tests/fixtures, each named after a table in the database. Each fixture script contains a return statement; the returned value is an array that defines the contents of the table at the beginning of the test. By default, no fixtures will be used for a test; the fixtures to use must be defined in the public fixtures attribute (array). For more information on fixtures and how to use them: see Testing: Defining Fixtures in The Definitive Guide to Yii.
As a rule of thumb, the more fixtures you define for a unit test, the more time the test will take to execute, as it means that more database operations will need to take place at the beginning of every test method. If you do not need to reset a particular table in a test case's tests, but need there to be preexisting data to test against, you should use init scripts. Note the following comparison between initscripts and fixtures:
Property | Fixtures | Init scripts |
---|---|---|
File path / name | protected/tests/fixtures/{table name}.php | protected/tests/fixtures/{table name}.init.php |
When run/applied | At the beginning of tests in test cases that use them | * At the beginning of tests in test cases that use them, and:
* At the very beginning of a test suite (before any tests run) |
Structure of the returned array | array(\[row alias name\] => array( \[column name\] => \[column value\])) | array( array( \[column name\] => \[column value\]) ) |