Unit Test Tutorial 2: Difference between revisions
From Joomla! Documentation
Some markup changes. |
|||
| (3 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
== Looking at the Unit Test Class == | == Looking at the Unit Test Class == | ||
In our first unit test, we created 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 part of a test class. | |||
Unit Test classes generally inherit from the class ''PHPUnit_Framework_TestCase''. | |||
Unit Test classes generally inherit from the class 'PHPUnit_Framework_TestCase'. | |||
You can see a basic test class below: | You can see a basic test class below: | ||
< | <syntaxhighlight lang="php"> | ||
<?php | <?php | ||
/** | /** | ||
| Line 13: | Line 12: | ||
* @license GNU General Public License version 2 or later; see LICENSE.txt | * @license GNU General Public License version 2 or later; see LICENSE.txt | ||
*/ | */ | ||
require_once JPATH_BASE.'/libraries/joomla/utilities/arrayhelper.php'; | require_once JPATH_BASE.'/libraries/joomla/utilities/arrayhelper.php'; | ||
/** | /** | ||
* Test class for JArrayHelper. | * Test class for JArrayHelper. | ||
*/ | */ | ||
class JArrayHelperTest extends PHPUnit_Framework_TestCase { | class JArrayHelperTest extends PHPUnit_Framework_TestCase { | ||
/** | /** | ||
* Sets up the fixture, for example, opens a network connection. | * Sets up the fixture, for example, opens a network connection. | ||
| Line 28: | Line 24: | ||
*/ | */ | ||
protected function setUp() { | protected function setUp() { | ||
} | } | ||
/** | /** | ||
* Tears down the fixture, for example, closes a network connection. | * Tears down the fixture, for example, closes a network connection. | ||
| Line 38: | Line 32: | ||
*/ | */ | ||
protected function tearDown() { | protected function tearDown() { | ||
} | } | ||
/** | /** | ||
* Simple Test for getColumn method. | * Simple Test for getColumn method. | ||
| Line 63: | Line 55: | ||
) | ) | ||
); | ); | ||
$result_array = JArrayHelper::getColumn($test_array, 'country'); | $result_array = JArrayHelper::getColumn($test_array, 'country'); | ||
$this->assertThat( | $this->assertThat( | ||
$result_array, | $result_array, | ||
| Line 75: | Line 65: | ||
} | } | ||
} | } | ||
</ | ?> | ||
</syntaxhighlight> | |||
=== 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 ten 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 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. | |||
[[Category:Bug Squad]] [[Category:Development]] [[Category:Testing]] [[Category:Automated Testing]] | |||
Latest revision as of 23:26, 2 December 2022
Looking at the Unit Test Class
In our first unit test, we created 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 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'
);
}
}
?>
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 ten 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 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.