Unit Testing
This is a first draft...feel free to elaborate.
phpMyAdmin has two types of unit tests build on PHPUnit:
- Classic unit tests
- Browser scenarios (run via Selenium RC, introduced in phpMyAdmin 3.3)
Contents |
[edit] PHP Unit
[edit] Setup
See: TestingEnvironment
[edit] Configuration files
bootstrap.php
This file runs before starting test and contain all general code that should run before tests.
<?php
// Adding phpMyAdmin sources to include path
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(realpath("../index.php");
// Setting constants for testing
define('PHPMYADMIN', 1);
define('TESTSUITE', 1);
session_start();
// You can put some additional code that should run before tests here
?>
phpunit.xml
phpUnit configuation located in file phpunit.xml.dist. In this file located all configuration related for automated testing
- Organization of TestSuites
- Selenium browsers configuration
- Type of logging that phpUnit needs to create
- php.ini settings, constants and globals variables
More detailed information could be found in PHPUnit official documentation - [1].
Personal configuration files
In repository located files phpunit.xml.dist and test\bootstrap-dist.php that used by default by Continious Integration server.
Developers may made a local copy of this files - bootstrap.php and phpunit.xml. And make it specific for they developer environment.
[edit] Running tests
You can run PHPUnit test from command line for whole suite.
$ cd <pma_dir> $ phpunit
PHPUnit 3.5.13 by Sebastian Bergmann. ............................................................... 63 / 317 ( 19%) .............SSSSS............................................. 126 / 317 ( 39%) ............................................................... 189 / 317 ( 59%) ............................................................... 252 / 317 ( 79%) ............................................................SSS 315 / 317 ( 99%) SS Time: 4 seconds, Memory: 28.50Mb
Or for specific test
$ phpunit test/libraries/core/PMA_isValid_test.php
Also you can specify configuration files for PHPUnit
$ phpunit -c my_phpunit.xml --bootstrap my_bootstrap.php
And include or exclude specific groups.
$ phpunit --list-groups
PHPUnit 3.5.13 by Sebastian Bergmann. Available test group(s): - Selenium - __nogroup__ - common.lib-tests
And for example exclude Selenium tests.
$ phpunit --exclude-group Selenium
From a browser.
The test/ directory is protected by .htaccess file. Please refer to test\README file for details howto allow http access to this directory.
And then you can run test from browser:
- <pma_dir>/test/wui.php
[edit] Selenium
[edit] Setting up Selenium
- Download Selenium RC
- Place Selenium RC php-client-driver in your PHP include_path
- Place the Selenium server jar in a convenient location
- This can be on a totally different computer, e.g. a machine that has a specific browser available.
- Add a Test section to your config.inc.php
$cfg['Test'] = array( 'pma_host' => 'http://my-test-server', 'pma_url' => '/phpMyAdmin/', // Use with access to several test tables 'testuser' => array('username' => 'selenium', 'password' => 'verysecret'), // User with global privileges 'testadmin' => array('username' => 'seleniumadmin', 'password' => 'verysecret'), );
- Download Selenium IDE (or from mozilla.org)
- Only required to record new test cases
[edit] Configuring Selenium on headless system
If you have server without gui and monitor and you want to run Selenium test on it, You need to install Xvfb and create virtual display.
- Make sure that you have Firefox installed
- Install
sudo apt-get install xvfb
- Start Xvfb on virtual display (eg. 15)
/usr/bin/Xvfb :15 -ac -screen 0 1024x768x8 &
- Export DISPLAY env variable
export DISPLAY=localhost:15.0
- Start Selenium Server
/usr/bin/java -jar /opt/selenium/selenium-server.jar -debug > /var/log/selenium-server.log 2>&1 &
Alternatively (eg. on Debian) you can use xvfb-run wrapper:
xvfb-run /usr/bin/java -jar /opt/selenium/selenium-server.jar -debug > /var/log/selenium-server.log 2>&1 &
[edit] Creating tests
- Record using the Selenium IDE
- using the context menu things like 'verifyTextPresent' can be added
- Replay the recording
- verify that it runs OK
- optional: cleanup unnecessary commands
- Save the recording
- Export it to PHP code
- Create a TestCase, or submit both files with a description in the patches tracker
[edit] Selenium issues
- Using a self-signed ssl certificate with Selenium RC is a pain, using
non-ssl for now. http://townx.org/blog/elliot/dealing-self-signed-ssl-certificates-when-running-selenium-server-firefox
- Somehow selenium does not like the language selection on the cookie
login page, force English in your config.inc.php for now.
$cfg['Lang'] = 'en-utf-8';
[edit] Test Class structure
TODO: link to phpdoc ?
The following class hierarchy is used:
- PHPUnit_Framework_TestCase
- PHPUnit_Extensions_SeleniumTestCase
- PmaSeleniumTestCase
- PmaSeleniumLoginTest
- PmaSeleniumTestCase
- PmaTestCase
- PmaMessageTest
- PHPUnit_Extensions_SeleniumTestCase