From Habari Project

Revision as of 16:07, 23 May 2012 by Ringmaster (Talk | contribs)
Jump to: navigation, search

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:

if(Utils::env_test()) {
	Config::set( 'db_connection', array(
	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(

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:


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.


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!

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.

Personal tools