Testing

From Habari Project

Jump to: navigation, search

Contents

Running Habari Core Tests

Habari supplies tests using the Tests plugin, which adds a test harness, unit tests, and acceptance tests for testing the core Habari package. With the Tests plugin installed, plugin developers can also supply tests for testing their own plugins, the results of which will be reported on the test results page.

To run the core tests:

  1. Install and activate the Tests plugin.
  2. Optionally create a test configuration.
  3. Run the tests.

It is not advised to run the test suit on a production system, because the test suite opens execution points in the code that may allow external users to affect your production database with test functions.

Create a test configuration

If you do not create a test configuration, running the tests will affect the database that Habari normally accesses. Since the tests that are performed are not guaranteed to leave the database in the state that it began (several tests are destructive and will delete all posts), it is advised that you create a test configuration unless the Habari instance is otherwise used only for testing purposes.

Once again: If you do not have a test configuration, the tests will irreparably change your primary database!

To create a test configuration, you must alter your configuration so that when Habari runs in a testing state, a different database is selected. As of this writing, that database needs to be pre-installed with Habari, but in the future, tests may be expanded to test installation procedures and this may not be required.

Consider this sample test configuration, which is stored in the config.php for the Habari site in question:

<?php
if(Utils::env_test()) {
	Config::set( 'db_connection', array(
		'connection_string'=>'sqlite:habari_unittest.db',
		'username'=>'',
		'password'=>'',
		'prefix'=>''
	));
	Config::set('blog_data', array(
		'admin_username' => 'admin',
		'admin_pass1' => 'password',
		'admin_pass2' => 'password',
		'blog_title' => 'Testing Blog',
		'admin_email' => 'test@example.com',
		'option_theme_name' => 'Charcoal',
		'option_theme_dir' => 'charcoal',
	));
}
else {
	Config::set( 'db_connection', array(
		'connection_string'=>'mysql:host=localhost;dbname=habari',
		'username'=>'root',
		'password'=>'',
		'prefix'=>''
	));
}
 
?>

This configuration connects to the SQLite database "habari_unittest.db" when in the testing environment, and uses a MySQL database "habari" when not in the test environment. The configuration also sets some default values for the blog installation via the "blog_data" config setting, so that it is not necessary to run the installer on the first access of the site.

When this file (or one with appropriate db connection values) is present, you must perform an installation of Habari in the test environment. To access the test environment from the browser, connect to your site normally, but pass the _useenv querystring a value of test:

http://example.com/?_useenv=test

Passing the value of 'test' tells Habari to use the test environment. A cookie will be set in your browser so that subsequent requests, even those that do not include the usetest value, are directed to use the test configuration.

Proceed to install Habari as normal in the test configuration. You will need to log in as an administrator and enable the Tests plugin in the test environment for it to report test results. You should also enable any plugins that include tests that should be included in the test process.

When you have completed configuring the test environment, pass 0 to the _useenv value to return to the main configuration settings.

http://example.com/?_useenv=0

When you submit 0 for _useenv your browser cookie will be cleared, and you will see the main site.

Be sure to confirm that you are using the correct sites when you run the tests and configure the testing environment, otherwise you may experience unexpected results - including data loss - when the tests are run!

Configure Selenium

Selenium is used by some of Habari's tests to "autopilot" a browser through the Habari interface and inspect the results. The easiest way to use selenium is to download the Selenium Server from: http://docs.seleniumhq.org/download/

To run selenium on your server, you will want to run a command like:

java -jar selenium-server-standalone-2.31.0.jar

You must leave the Selenium server running on the server where Habari is installed for it to control an instance of the browser there (usually Firefox). During the test run, Selenium will be used to drive a web browser and examine the results. These results will be returned to the test suite which will report its findings.

Run Tests

The easiest way to run the tests is to access the Tests option from the main Habari menu. On the Tests page, a list of all of the available tests will appear. The system will have run a "Dry Run" of all of the tests so that they can be listed. At this point, you are free to click the "Run" button to run all of the tests in the system. You may also choose a specific unit before clicking "Run" to run only the tests in that unit. You may also click a test name directly to run only that test.

Write Tests

A good start is to look at existing tests and copy them.

Needs detail about the specifics of what tests get run, best practices

Producing Output

When writing tests, it's often useful to be able to produce more detailed output. You can do this using the output() method.

public function test_modern_art() 
{
  // some test code
  $this->output('This provides more detail, perhaps describing why the test failed');
}
Personal tools