Difference between revisions of "Unit Tests"
(Added section on how to run unit tests)
m (Ignore this section comment)
|Line 109:||Line 109:|
pear install phing/phing
pear install phing/phing
Revision as of 15:31, 20 October 2009
- 1 Installing and Configuring your Test Environment
- 2 Developing Unit Tests
- 3 Running Unit Tests
- 4 Installing and Configuring the Build Environment
Installing and Configuring your Test Environment
You'll need PEAR installed for the following steps to work. If you are on Linux simply install the corresponding package provided by your packaging system. If you are on Windows execute the "go-pear.bat" in the PHP installation directory and follow the on-screen instructions.
If you are on Linux you may also find some or all of the other required software pre-packaged for your distribution. Please check that before installing software manually.
On the command line execute
pear channel-discover pear.phpunit.de pear install phpunit/PHPUnit
PKP Unit Test Framework
The PKP unit test framework can be checked out as part of the pkp library. You find it in pkp/tests.
You'll however have to use the unit test framework within an application to work properly.
Before starting your tests, please make copies of the files in "/lib/pkp/tests/config", remove the "TEMPLATES" placeholder and configure the pgsql and mysql database connection data.
Please see our Configure Eclipse for PHPUnit tutorial.
Developing Unit Tests
This is a very terse documentation of the PHPUnit features we usually use in our unit tests. It is meant to document PKP-specific usage of PHPUnit and as a cheat sheet for the most important PHPUnit functionality. It does not replace the PHPUnit manual. Please refer to the manual when you are using PHPUnit for the first time.
- Unit test classes are called after the class they are testing, e.g. the test to test the "Config" class would be "ConfigTest".
- PHPUnit works well with our usual PKP file name nomenclature. Files are named after the test class name with a ".inc.php" postfix, e.g. the ConfigTest class would be found in the file "ConfigTest.inc.php".
- We'll use the same folder structure inside the tests directory that we use for the classes we test.
- Tests for PKP library classes will be in "lib/pkp/tests". Tests for PKP application classes will be in "/tests".
Which base class to extend
- Unit tests that require database access extend "DatabaseTestCase".
- All other test cases extend "PKPTestCase".
- One of the functions of a test case is documenting the class specification in a very detailed and precise way. Please make sure that your source code contains enough method and inline documentation so that an outsider can quickly understand how the class' API is to be used. This is not necessary if the test method name speaks for itself.
- Use a @see annotation in the class comment that points to the tested class.
How to test exceptions
- A test method that is annotated with "@expectedException ExceptionClass" will fail if it does not throw the declared exception.
Annotations have to be declared where they apply.
Please see the PHPUnit manual for details on each annotation.
The @covers annotation indicates to the test coverage analyser the class or method to be covered by a given unit test. The @covers annotation is mandatory for all tests. The function format is always preferable to the class format. You'll very rarely write unit tests that cover a whole class.
- method format: @covers ClassName::methodName
- class format: @covers ClassName
Documenting Test Dependency
The @depends defines a logical dependency of one test case from another. You use the depends annotation if you want to document such dependency or if you want to use results from one test case as input to another test case. Test execution order is only dependent on the order of the methods in the test class, not on the @depends annotation.
Running Unit Tests
If you have followed our tutorial to Configure Eclipse for PHPUnit then running tests will be very easy.
Select the test file you want to execute. You can do this in two ways.
- Select the editor tab that contains the test class:
- Select the file with the test class in the project explorer
Make sure that the file / edit tab is marked blue as in the screen shots. Otherwise you'll get an error when executing your test.
Next you have to decide whether you want to simply run or whether you want to debug your test.
- To run your test select the phpunit-ext external tool configuration from the external tools' favorites drop-down:
You can see the test's output in the console view.
- To debug your test set breakpoints at appropriate places in the code, then select the phpunit debug configuration from your debug configurations' favorites drop-down:
You can see test results in the "Debug Output" view. Sometimes the console output is not complete in the debug output. Use the external tool if you want the complete console output.
That's it. Now you're ready to develop and execute your tests. Enjoy!
Installing and Configuring the Build Environment
On the command line execute
pear channel-discover pear.phing.info pear install phing/phing
The section about the build environment is not complete yet. Please ignore it for the time being!