phpBB

Development Wiki

Difference between revisions of "Unit Tests"

From phpBB Development Wiki

Redirect page
(Add your test to the system)
(Redirected page to Automated Tests)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
= Unit Tests =
+
#REDIRECT [[Automated_Tests]]
== Running tests on your system ==
+
 
+
This is a job for your command-line/shell.
+
First, you need to install PHPUnit to your server: http://www.phpunit.de/
+
 
+
Once that is done:
+
You need to switch into the tests/-directory inside the git-repo.
+
And then simply run
+
Linux:<pre>phpunit</pre>
+
Windows:<pre>YOUR_DIRECTORY_WHERE_THE_FILE_IS/phpunit.bat</pre>
+
 
+
Afterwards you should see something like:
+
<pre>PHPUnit 3.4.11 by Sebastian Bergmann.
+
 
+
............................................................  60 / 688
+
............................................................ 120 / 688
+
IIIIIIIIIII................................................. 180 / 688
+
............................................................ 240 / 688
+
............................................................ 300 / 688
+
............................................................ 360 / 688
+
............................................................ 420 / 688
+
............................................................ 480 / 688
+
............................................................ 540 / 688
+
............................................................ 600 / 688
+
............................................................ 660 / 688
+
............................
+
 
+
Time: 5 seconds, Memory: 13.00Mb
+
 
+
OK, but incomplete or skipped tests!
+
Tests: 688, Assertions: 878, Incomplete: 11.</pre>
+
If there are no errors, your test was successful. Otherwise either a test was wrong, or the functionality itself is wrong. Then you should go and fix it.
+
 
+
== Writing your own Tests ==
+
 
+
=== demo/all_tests.php file ===
+
<php><?php
+
/**
+
*
+
* @package testing
+
* @copyright (c) 2008 phpBB Group
+
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+
*
+
*/
+
 
+
if (!defined('PHPUnit_MAIN_METHOD'))
+
{
+
define('PHPUnit_MAIN_METHOD', 'phpbb_demo_all_tests::main');
+
}
+
 
+
require_once 'test_framework/framework.php';
+
require_once 'PHPUnit/TextUI/TestRunner.php';
+
 
+
require_once 'demo/demo_suite.php';
+
require_once 'demo/db_demo_suite.php';
+
 
+
class phpbb_demo_all_tests
+
{
+
public static function main()
+
{
+
PHPUnit_TextUI_TestRunner::run(self::suite());
+
}
+
 
+
public static function suite()
+
{
+
// Short description of your test.
+
$suite = new PHPUnit_Framework_TestSuite('phpBB Your First phpBB-Test');
+
 
+
$suite->addTestSuite('phpbb_demo_suite');
+
$suite->addTestSuite('phpbb_db_demo_suite');
+
// You can also split your Test into mutliple files to keep it clear.
+
// You should also split it up, if some tests need the database and some do not.
+
 
+
return $suite;
+
}
+
}
+
 
+
if (PHPUnit_MAIN_METHOD == 'phpbb_demo_all_tests::main')
+
{
+
phpbb_demo_all_tests::main();
+
}
+
</php>
+
=== demo/demo_test.php ===
+
There are two cases of tests: one that needs a database and one that doesn't.
+
'''If a test does not need a database then don't use it!'''
+
==== without database ====
+
<php>
+
<?php
+
/**
+
*
+
* @package testing
+
* @copyright (c) 2008 phpBB Group
+
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+
*
+
*/
+
 
+
require_once 'test_framework/framework.php';
+
 
+
require_once '../phpBB/includes/functions.php';
+
 
+
class phpbb_demo_test extends phpbb_test_case
+
{
+
public static function phpbb_email_hash_data()
+
{
+
return array(
+
array('nickvergessen@gmx.de', 126830126620),
+
array('', 0),
+
);
+
}
+
 
+
/**
+
* @dataProvider phpbb_email_hash_data
+
*/
+
public function test_phpbb_email_hash($email, $expected)
+
{
+
$this->assertEquals($expected, phpbb_email_hash($email));
+
}
+
}
+
</php>
+
You may have noted, that in the test-function there is actually no data.
+
There's a dataProvider-function stated in the comment, which is used for the test.
+
The test is then run with every array in the dataProvider array, having the values as parameters for the test-function-call.
+
==== with database ====
+
<php>
+
<?php
+
/**
+
*
+
* @package testing
+
* @copyright (c) 2008 phpBB Group
+
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+
*
+
*/
+
 
+
require_once 'test_framework/framework.php';
+
 
+
class phpbb_db_demo_test extends phpbb_database_test_case
+
{
+
public function getDataSet()
+
{
+
return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/three_users.xml');
+
}
+
 
+
public static function fetchrow_data()
+
{
+
return array(
+
array('', array(array('username_clean' => 'barfoo'),
+
array('username_clean' => 'foobar'),
+
array('username_clean' => 'bertie'))),
+
array('user_id = 2', array(array('username_clean' => 'foobar'))),
+
array("username_clean = 'bertie'", array(array('username_clean' => 'bertie'))),
+
array("username_clean = 'phpBB'", array()),
+
);
+
}
+
 
+
/**
+
* @dataProvider fetchrow_data
+
*/
+
public function test_fetchrow($where, $expected)
+
{
+
// The function from phpbb_test_case_helpers returns a new db for every test.
+
$db = $this->new_dbal();
+
 
+
$result = $db->sql_query('SELECT username_clean
+
FROM phpbb_users
+
' . (($where) ? ' WHERE ' . $where : '') . '
+
ORDER BY user_id ASC');
+
 
+
$ary = array();
+
while ($row = $db->sql_fetchrow($result))
+
{
+
$ary[] = $row;
+
}
+
$db->sql_freeresult($result);
+
 
+
$this->assertEquals($expected, $ary);
+
}
+
}
+
</php>
+
Most important to know for db-tests is:
+
# All data from the database is truncated first.
+
# The data from the getDataSet function is loaded into the database.  '''No data from any other test is available!'''
+
 
+
=== Code of a DB-DataSet ===
+
<pre>
+
<?xml version="1.0" encoding="UTF-8" ?>
+
<dataset>
+
<table name="table_name">
+
<column>column_name_1</column>
+
<column>column_name_2</column>
+
<row>
+
<value>value for column 1</value>
+
<value>value for column 2</value>
+
</row>
+
<row>
+
<value>another row, value for column 1</value>
+
<value>another row, value for column 2</value>
+
</row>
+
</table>
+
</dataset>
+
</pre>
+
 
+
[[Category:Development]]
+

Latest revision as of 15:35, 21 March 2012