Difference between revisions of "Unit Test Tutorial 2"

From Joomla! Documentation

Line 86: Line 86:
  
  
[[Category:Testing]]
+
[[Category:Bug Squad]] [[Category:Development]] [[Category:Testing]] [[Category:Automated Testing]]

Revision as of 09:36, 24 May 2010

Looking at the Unit Test Class[edit]

In our first unit test, we create a single test. This test was a part of a presumed preexisting test class. Although an individual test can be contained in a single method, these methods have to be a part of a test class.

Unit Test classes generally inherit from the class 'PHPUnit_Framework_TestCase'.

You can see a basic test class below:

<?php
/**
 * @version		$Id$
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

require_once JPATH_BASE.'/libraries/joomla/utilities/arrayhelper.php';

/**
 * Test class for JArrayHelper.
 */
class JArrayHelperTest extends PHPUnit_Framework_TestCase {

	/**
	 * Sets up the fixture, for example, opens a network connection.
	 * This method is called before a test is executed.
	 *
	 * @access protected
	 */
	protected function setUp() {

	}

	/**
	 * Tears down the fixture, for example, closes a network connection.
	 * This method is called after a test is executed.
	 *
	 * @access protected
	 */
	protected function tearDown() {

	}

	/**
	 * Simple Test for getColumn method.
	 */
	public function testGetColumn()
	{
		$test_array = array(
			array(
				'sport' => 'football',
				'teams' => '16',
				'country' => 'United States'
			),
			array(
				'sport' => 'badminton',
				'teams' => '12',
				'country' => 'Germany'
			),
			array(
				'sport' => 'basketball',
				'teams' => '20',
				'country' => 'Canada'
			)
		);

		$result_array = JArrayHelper::getColumn($test_array, 'country');

		$this->assertThat(
			$result_array,
			$this->equalTo(
				array('United States', 'Germany', 'Canada')
			),
			'We did not get the proper column data back'
		);
	}
}

Okay, so some notes:

  • As noted, the class extends from PHPUnit_Framework_TestCase
  • Outside of our class, we do a require for the class that we want to test
  • Our class name starts with the name of the class we want to test and has 'Test' appended to the end. i.e. we are testing JArrayHelper, so our test class is JArrayHelperTest.
  • We have two methods that are not part of our tests: setUp() and tearDown(). As noted in the comments for these methods, setUp() is called before each test and tearDown() is called after each test. To clarify, if there are 10 tests in a class, they are each called ten times. There are two other methods available that you can supply: setUpBeforeClass() and tearDownAfterClass(). As the names imply, these are each called before and after all the tests from the class are performed, thus getting executed once each.
  • Test methods start with the word test and generally end with the name of the method that is under test. We are testing the getColumn() method, therefore, our method is called testGetColumn(). As methods get more complicated, more than one method is required for testing. In this situation, method names that describe the general purpose of the method should be used.