Extension:UnitTest

UnitTest provides unit testing with PHPUnit, Selenium and QUnit.

Overview
The goal of this extension is to bridge the gap between Continuous integration and local developer testing (i.e., testing on your workstation).

This extension also allows testing of PHPUnit, Selenium and QUnit in one application, as opposed to having these tested in different applications.

This extension also generates screenshots for a slideshow if Selenium is used in a test. A screenshot will be generated automatically after each assertion. After the test has been run, links are presented to the developer (on the command line) to view the slideshows.

This extension also generates Code Coverage reports in html and XML.

Additional reports are generated which could be picked up by monitoring tools such as Nagios.

Requirements

 * A web server like Apache httpd
 * PHP > 5.2
 * A database server like MySQL for the Mediawiki instance.
 * Git or Subversion to manage code
 * Git to pull in PHPUnit
 * Xdebug - required for Code Coverage
 * Linux | OS X | Unix

TODO

 * Build out a special page for the UnitTest extension
 * Allow viewing of unit test slideshows
 * Allow cleanup of old slideshows
 * See what extensions are using UnitTest

Some of these features have been built out, but have not been merged into master yet:

https://gerrit.wikimedia.org/r/#/c/14169/

Installation
Setting up your system for unit testing requires a few external libraries.

The following example is a way to set up your system to work with several virtual web hosts. This makes it possible to test many versions of the same application.

Directory Structure
You do not need to have this exact structure setup. Any deviations will need to be accounted for in your configuration files.

/www `--sites `--localhost |--default |--phpmyadmin |--mediawiki-1.20.localhost.example.org `--mediawiki-master.localhost.example.org
 * --bin
 * `--stylize.php
 * --conf
 * `--httpd
 * |--sites
 * | |--default.conf
 * | |--phpmyadmin.conf
 * | |--mediawiki.1.20.localhost.example.org.conf
 * | `--mediawiki.master.localhost.example.org.conf
 * `--httpd.conf
 * --github
 * |--phpunit
 * |--qunit
 * `--symfony
 * --library
 * `--phpunit
 * |--dbunit
 * |--php-code-coverage
 * |--php-file-iterator
 * |--php-text-template
 * |--php-timer
 * |--php-token-stream
 * |--phpunit
 * |--phpunit-mock-objects
 * `--phpunit-selenium
 * |--php-token-stream
 * |--phpunit
 * |--phpunit-mock-objects
 * `--phpunit-selenium

To create this structure on Linux or UNIX:

mkdir -p /www/bin

mkdir -p /www/conf/httpd/sites

mkdir /www/github

mkdir -p /www/library/phpunit-3.6 ln -sn /www/library/phpunit-3.6 /www/library/phpunit

mkdir /www/library/seleniumHQ

mkdir -p /www/sites/localhost/default/public

mkdir /www/sites/localhost/mediawiki-1.20.localhost.wikimedia.org mkdir /www/sites/localhost/mediawiki-master.localhost.wikimedia.org

PHPUnit
Information about phpunit may be found here:

https://github.com/sebastianbergmann/phpunit/

Install PHPUnit
cd /www/library/phpunit-3.6

git clone git://github.com/sebastianbergmann/phpunit.git git clone git://github.com/sebastianbergmann/dbunit.git git clone git://github.com/sebastianbergmann/php-file-iterator.git git clone git://github.com/sebastianbergmann/php-text-template.git git clone git://github.com/sebastianbergmann/php-code-coverage.git git clone git://github.com/sebastianbergmann/php-token-stream.git git clone git://github.com/sebastianbergmann/php-timer.git git clone git://github.com/sebastianbergmann/phpunit-mock-objects.git git clone git://github.com/sebastianbergmann/phpunit-selenium.git git clone git://github.com/sebastianbergmann/phpunit-story.git git clone git://github.com/sebastianbergmann/php-invoker.git

Set your checkout to 3.6:

cd phpunit && git checkout 3.6 && cd .. cd dbunit && git checkout 1.1 && cd .. cd php-code-coverage && git checkout 1.1 && cd .. cd phpunit-mock-objects && git checkout 1.1 && cd .. cd phpunit-selenium && git checkout 1.1 && cd ..

Selenium Server
Selenium Server is used for automated browser testing.

Install Selenium Server
You may download the file from this link:

http://selenium.googlecode.com/files/selenium-server-standalone-2.21.0.jar

cd /www/library/seleniumHQ

wget http://selenium.googlecode.com/files/selenium-server-standalone-2.21.0.jar

ln -sn /www/library/seleniumHQ/selenium-server-standalone-2.21.0.jar /www/library/seleniumHQ/selenium-server.jar

Controlling Selenium Server
When you are ready to test with Selenium Server, you will need to start it from the command line. This will take over the terminal in which you run it, so you will need a second terminal in order to execute the phpunit test.

There is a lot of useful information that will be dumped from selenium-server.jar.

You will see information about browsers being opened and what pages are being hit.

java -jar /www/library/seleniumHQ/selenium-server.jar

There are other ways to install and control Selenium Server. This is just one of many!

Virtual Web Hosts
The virtual host configuration file is named after the host name with the file extension .conf

Create the virtual host file
vi /www/conf/httpd/sites/mediawiki-master.localhost.example.org.conf


 * 1) mediawiki-master.localhost.example.org

 ServerName mediawiki-master.localhost.example.org ServerAlias mediawiki-master.localhost DocumentRoot "/www/sites/localhost/mediawiki-master.localhost.example.org"  AllowOverride None Order allow,deny Allow from all  

You will need to add an include to pick up the virtual host files. Apache may need additional configuration to enable virtual hosts.

You may not need the default host set, depending on your configuration.

Append this to your httpd.conf, where ever it lives.

vi /www/conf/httpd/httpd.conf

NameVirtualHost *:80

 DocumentRoot "/www/sites/localhost/default/public"

 			DAV Off  AllowOverride None Order allow,deny Allow from all 



Include /www/conf/httpd/sites/*.conf
 * 1) Virtual Hists