UnitTesting

From GnomePHP Docs
Jump to: navigation, search

Contents

Unit testing

A complete unit testing suite is included in GnomePHP's core features. This HowTo will make you write good unit tests for your applications.

What is unit testing?

When we talk about unit testing we talk about testing the core features of your application. The developers of GnomePHP also tests the core framework features with unit testing. Unit testing makes your code much better and you know when you have changed some code that made your application buggy.

Testing GnomePHP's Core

First of, lets not write our own unit test, lets test the GnomePHP core!


We are of course writing unit tests for the framework itself, you can actually test the gnomephp core by using a router rule in your config/routes.conf file in your application.

Add this to your config/routes.conf file.

*      /testgnomephp            \gnomephp\testsuite\TestGnome.index()

Now just simply visit http://yoursite.com/testgnomephp or http://yoursite.com/index.php/testgnomephp.

You should get a screen similar to this:

Gnometest.png


Writing your own unittest

Now that we know how the unit test ui is looking like ( when all tests are passed and successful ) , lets write our own unit test.

The rules


Sample

Controller logic

Start by creating the file myapp/controller/TestSomeThings.php

Here is a sample of testcode:

namespace myapp\controller;
 
// We extend the TestController  - it contains index() method that can be run and more methods.
class TestSomeThings extends \gnomephp\testsuite\TestController{
 
	protected function testOnePlusOne(){
		$this->test(1)->assertEqual(1);
 
	}
	protected function thisTestWillFail(){
		$this->test(4232)->assertString();
	}
 
}

This test will success at the first test but not at "thisTestWillFail".


Router

Now we need to add some router configuration in the comfig/routes.conf file.

*      /testSomeStuff        TestSomeThings.index()


Ok lets try

Now you can go to http://mysite.com/index.php/testSomeStuff and you will see the unit testing ui with one pass and one error! That's it.


Creating test packages

Creating test packages is really simple in GnomePHP. You can create sub packages and a main package that test's your whole application.

Let's say we have 2 Test controllers that we want to add to a package, TestObjects and TestBrowser. We want one entry url /test to load both of these tests in the same test UI.

We need to create a simple 3rd controller, lets call this AllTests.php in the controllers directory.

namespace myapp\controller;
 
class AllTests extends \gnomephp\testsuite\TestController{
	public function setup(){
		$this->addTestController(array('\myapp\controller\TestObjects', '\myapp\controller\TestBrowser'));
	}
}

We're done, just link it into the routes.conf file!

*          /test             AllTests.index()


As a sidenote, remember that packages can contain packages, and so if a package includes another package all the tests will be run.
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox