Ant Scripting – Introduction: Slides from CodeIgniter Meeting on 10-27-2010
Hey peeps, bellow you will find last night's CodeIgniter Slides. It was a good event, many people many new faces. I myself still stumbled over the presentation, I really should try to make more of an effort to practice my presentation. I do hope to also.
Next Months meeting will be catered by Your's Truly
it's about time we give our loyal members some feed
.. and I am planning on making a true CodeIgniter NYC home I'll add it to the multiple task I have given myself.
without further ado here ya go:
So Here’s A link to help you Mac OS Users with no VM do some Web Testing ;-)
http://www.newmediacampaigns.com/page/install-pear-phpunit-xdebug-on-macosx-snow-leopard
I was looking to do some quick debugging for my quick work and long and behold got screwed in the train again lol. But not any more... hope you all find this useful and helpful as it was for me
Testing CodeIgniter.
Unit testing with Code Igniter can be done with a real time development pattern. There are a few things to consider when thinking about testing a controller and action. There are two ways to do things. we can create the test with the controllers we want to test or we can create a test controller to test all the controllers and functions on their own.
Testing:
In order to run the test(s) we will need to load the testing the library, in order to load it we will need to load the library as follows:
$this->load->library('unit_test');
To run a test we will be using the following function:
$this->unit->run( test, expected result, 'test name' );
There are may wasy to get the result of the test(s), if we want it per test we can do as follows:
echo $this->unit->run( test, expected result, 'test name' );
This will create a table and display it on the screen once the test is ran but if we want to have a special placement in our html we can can store the result in an variable in which we are able to pass into the view. But lets say we are running multiple test we can use the following to obtain the results of all the unit test that was runned during the call of the controller.
$data['testing'] = $this->unit->report();
we can pass the results to the view as usual, the result is a formatted tabled result.
Bellow you will find my current code sample for testing with Code Igniters Built in testing:
class Landing extends Controller
{
public function Landing(){
parent::Controller();
}
//TESTING GENERAL
public function index()
{
$data['title'] = 'CI DEMO';
$this->load->library('unit_test');
$this->unit->run($data, array('title'=>'CI DEMO'), 'title');
$this->unit->run(1, 2, 'error');
$this->getData();
$data['testing'] = $this->unit->report();
$this->load->view('landing/index', $data);
}
//TESTING MODELS
private function getData()
{
$this->load->model('demo');
$result = $this->demo->getList();
$expectedFormat = '<h1>CodeIgniter Demo</h1>';
$this->unit->run($this->demo->format($result[0]),$expectedFormat, 'MODEL FORMATTING');
}
}
?>
Result
The results from the test are in table format as follows:
| Test Name | title |
|---|---|
| Test Datatype | Array |
| Expected Datatype | Array |
| Result | Passed |
| File Name | /var/www/codeigniter/system/application/controllers/landing.php |
| Line Number | 22 |
Each test that is runned has the same table display but the results vary as follows:
- Test Name:
- The Title provided for the test
- Test Datatype
- The Test data type, what kind of data was tested
- Expected Datatype
- The expected data type that was expected to be tested
- Result
- The result of the test, Passed or Failed
- File Name
- The file location of the test
- Line Number
- The line number of where the test was called
Testing with Toast
Toast is another unit testing suite for Code Igniter Specifically. It was designed to intergrade perfectly. Allowing more points of testing and a more defined way of testing closely related to jUnit.
You can get more information at http://jensroland.com/projects/toast/.
Gettting Started:
INSTRUCTIONS
- Download and unzip the files
- Create two new folders:* /app/controllers/test
* /app/views/test
- Move the two controller files and the example test class to the first folder, and the three view files to the second
Now that we have set up our Toast Testing system lets get to some testing
yay!!
Let's create a new controller, but first we need to include the toast class and extend it as well.
Now in oder to test correctly we need to create the constructor and implement the toast constructor. Each test function must be prefixed with 'test_'. The file should look as follows:
require_once(APPPATH . '/controllers/test/Toast.php');
class My_test_class extends Toast
{
function My_test_class()
{
parent::Toast(__FILE__); // Remember this
}
function test_some_action()
{
// Test code goes here
$my_var = 2 + 2;
$this->_assert_equals($my_var, 4);
}
function test_some_other_action()
{
// Test code goes here
$my_var = true;
$this->_assert_false($my_var);
}
}
Place your test classes INSIDE the /test/ folder (Toast_all can't find them otherwise), and you're ready to rock 'n' roll.
To run all the tests in a test class, simply point your browser to the class name:
http://www.example.com/test/my_test_class
To run an individual test, point your browser to the test function, WITHOUT the 'test_' prefix:
http://www.example.com/test/my_test_class/some_action
And to run all the tests classes in the /test/ folder at once, point your browser to the Toast_all controller:
http://www.example.com/test/toast_all
That's all! If you need more flexibility, check the feature list below, but there's really not much more to it. All the dirty work is handled behind the scenes.
Different kinds of test/assertions are as follows:
_assert_true()
_assert_false()
_assert_equals()
_assert_not_equals()
_assert_empty()
_assert_not_empty()
Plus the strict (===) versions:
_assert_true_strict()
_assert_false_strict()
_assert_equals_strict()
_assert_not_equals_strict()
Testing Essentials
As you may already notice testing is nothing more than checking to make sure that you are getting the response you are looking for. There may be custom classes, functions, and responses you expect from a function call or object creation.
Testing a model is just as easy with Toast just create the model as we normally would and then we can add a new controller to test the model on its own or have it join another test. Naturally, we would create the controller so we can seperate the failure points and focus on a specific model if we need to. To test a model with Toast you would want to do as follows:
require_once APPPATH . '/controllers/test/Toast.php';
class Simple extends Toast{
public function Simple()
{
parent::Toast(__FILE__);
$this->load->model('model');
}
public function test_model(){
$result = $this->mta->getList();
$this->_assert_not_empty($result);
$this->_assert_equals($this->mta->format($result[0]),'<h1>1>/h1<');
}
}