Page tree
Skip to end of metadata
Go to start of metadata

Unit testing helps create a more solid code base. The purpose of this article is to explain unit testing as it relates to FreePBX.

Location and Naming

Unit tests should be located in a utests folder in the root of the module. All tests should end with Test.php. Example fooAddTest.php.





Sample Code

The actual Test to be run. Leave the top comment intact as it serves a functional purpose in phpunit




* @backupGlobals disabled


class fooAddTest extends PHPUnit_Framework_TestCase{

  //Will be FreePBX BMO object
	protected static $f;
	//Will become your Class object
	protected static $o;
	//Module name used in test output as self::$module. Can be anything unless you want to use this as something more.
	protected static $module = 'Modulename';
	//Change Moduleclass to your class name
	public static function setUpBeforeClass() {
		self::$f = FreePBX::create();
		self::$o = self::$f->Moduleclass;
	//Stuff before the test
	public function setup() {}

	//Leave this alone, it test that PHPUnit is working
	public function testPHPUnit() {
		$this->assertEquals("test", "test", "PHPUnit is broken.");
		$this->assertNotEquals("test", "nottest", "PHPUnit is broken.");

	//This tests that the the BMO object for your class is an object
	public function testCreate() {
		$this->assertTrue(is_object(self::$o), sprintf("Did not get a %s object",self::$module));
	//This can be renamed to anything. Any method starting with "test" will be a test
	public function testOne(){
	  /*do stuff test stuff here*/

Running your test

Run the following command from your module root.

../../devtools/phpunit.php [--skipfreepbxbootstrap] [--moddir=<module directory if not in a module already>]
  • skipfreepbxbootstrap - Skip loading FreePBX Framework for the unit test. Otherwise load FreePBX around the unit test. If this is not set your system has to have a fully functioning FreePBX and Asterisk setup.
  • moddir - If you are not running phpunit.php inside of the same module directory you intend to run it against you need to define the module directory

PHPUnit Docs

  • No labels