How much should one test?
Posted: Wed Nov 09, 2005 11:21 am
I am trying SimpleTest right now (after following the nice introductory tutorial on lastcarft.com) and I picked a simple (I thought so) class to write - MySQL DB class. I haven't coded much at all and a question came up - how extensive the tests should be? Should I test ALL possible variations of method usage? Here are my tests:
The class itself so far:
You can see the tests are trivial and extremely simple... I imagine all possible variations when running a query (query() method), which would need quite some new tests:
Do I have to write all these variations? For example, I test a case when a db object is created and disconnect() method is run immediately. This type of usage will never happen since it's dumb, but still, let's leave it there. Now how do I know that disconnect() will behave correctly when used together with query() method and that I will get the correct and not overwritten error message (if there is any) at the end? That's another test with db object creation, and running query() and disconnect() methods. You can think of many variations. Should one test all?
For example, McGruff does not test the disconnect() method in his MySQL class at all and seems fine with it. viewtopic.php?t=36162&start=15
So I am not exactly sure about testing all these variations... maybe some tests are redundant?
Well, would like to find out general guidelines on what tests should be picked.
Code: Select all
define('SIMPLE_TEST', '../simpletest/');
require_once(SIMPLE_TEST . 'unit_tester.php');
require_once(SIMPLE_TEST . 'reporter.php');
require_once('../classes/class.Database.php');
//Existing (correct) DB info
define('db_host', 'localhost');
define('db_name', 'techno_db');
define('db_user', 'root');
define('db_pass', '');
class TestOfDatabase extends UnitTestCase
{
function TestOfDatabase()
{
$this->UnitTestCase('Test of Database');
}
function testSuccessfullConnection()
{
$db = &new Database(db_host, db_name, db_user, db_pass);
$this->assertIdentical($db->getError(), '');
}
function testUnsuccessfullConnection()
{
$db = &new Database('foo', db_name, db_user, db_pass);
$this->assertIdentical($db->getError(), 'UnableToConnect');
}
function testSuccessfullConnectionButWrongDB()
{
$db = &new Database(db_host, 'foo', db_user, db_pass);
$this->assertIdentical($db->getError(), 'NoDatabase');
}
function testDisconnectAfterSuccessfullConnection()
{
$db = &new Database(db_host, db_name, db_user, db_pass);
$db->disconnect();
$this->assertIdentical($db->getError(), '');
}
function testDisconnectAfterUnsuccessfullConnection()
{
$db = &new Database('foobar', db_name, db_user, db_pass);
$db->disconnect();
$this->assertIdentical($db->getError(), 'UnableToDisconnect');
}
}
$test = &new TestOfDatabase();
$test->run(new HtmlReporter());Code: Select all
class Database
{
var $connection;
var $error;
function Database($host, $name, $user, $password)
{
$this->connection = @mysql_connect($host, $user, $password, true);
if ($this->connection)
{
$select = mysql_select_db($name, $this->connection);
if ($select)
{
$this->error = '';
} else
{
$this->error = 'NoDatabase';
}
} else
{
$this->error = 'UnableToConnect';
}
}
function disconnect()
{
if (!$this->connection)
{
$this->error = 'UnableToDisconnect';
return;
}
mysql_close($this->connection);
}
function getError()
{
return $this->error;
}
}Code: Select all
function testSuccessfullConnectionGoodQuery()
{
}
function testSuccessfullConnectionBadQuery()
{
}
function testUnsuccessfullConnectionAndQuery()
{
}
function testSuccessfullConnectionWrongDBGoodQuery()
{
}
function testSuccessfullConnectionWrongDBBadQuery()
{
}
/* ... */For example, McGruff does not test the disconnect() method in his MySQL class at all and seems fine with it. viewtopic.php?t=36162&start=15
So I am not exactly sure about testing all these variations... maybe some tests are redundant?
Well, would like to find out general guidelines on what tests should be picked.