bitUnitPackage

Unit test package for bitweaver

Created by: Jan LindÃ¥ker, Last modification: 01 Oct 2005 (20:21 UTC)
The bitUnit package is an integration of simpletest into bitweaver. This package should only be active during development, since runnings testsuites will may take a lot of resources - once adding test code to the packages becomes common practice.

In order to make it as easy as possible to add test suites to bitweaver bitUnit adds features to bitweaver
  1. It searches the bitweaver installation for directories named test
  2. In these directories it finds .php files that start with test (case insensitive).
  3. These files are then added to simpletest datastructures for execution.
  4. After the test are executed the results are extracted and presented in a smarty template.

The bottom line is that if you whant to add a test for a package (or any other code in bitweaver) you only need to put properly named files in properly named directories.

Simple example

To start writing test the best way is to look in the simple test manual. This is however a quick intro for writing tests:

Some points to remember for test classes:
  1. The test scripts need to include the script they will be testing (obviously).
  2. Create one test class per test script, the class name being the same as the file name.
  3. All test classes must extend class UnitTestCase (directly or indirectly).
  4. All methods within the test class beginning with the letters "test" will be executed by

So, for our class, this is what the test class would look like:
{CODE(colors=>php)}<?php
// Get the class you're going to test
require_once('MyClass.php');
// Always extend the UnitTestCase class. There is no need to include/require the UnitTestCase, since this is taken care of by the test framework
// Always prefix the class name with 'Test'
class TestMyClass extends Test {
var $test; // this variable will hold our test class
var $testString; // this variable will hold a testValue

// Make initializations that will not change during the test
function TestMyClass() {
// setting up constant values will prevent failiures due to typos.
$this->testString ="testValue";
}

// This function is run before every test function
// Make the test case initialiaztion here.
setUp() {
$this->test = new MyClass();
}

// This function is run after every test function
// Release any dynamic resource here.
tearDown () {
$this->test = NULL;
}

// Testcases the methods all starts with test.

// Check if the initial value is 'not-set'
function testGetInitialValue() {
$this->assertTrue($this->test->getText() == "not-set");
}

// Set value and test that it sets
function testSetValue() {
$this->test->setText($this->testString);
$this->assertTrue($this->test->getText() == $this->testString);
}
}
?>{CODE}

Note that by the fact that we have a setUp and tearDown method there is no dependancies between the testcases.