Welcome Guest, Not a member yet? Register   Sign In
How to use Unit Testing v2.1.4
#1

[eluser]a_h_abid[/eluser]
Apparently I see that there is a Unit Testing Library in v2.1.4. It is in user guide as well.
http://ellislab.com/codeigniter /user-gu...sting.html

So how do i use it? Where we put the test codes, In our main Controller or In a separate test file? If separate, how we use them?
#2

[eluser]Otemu[/eluser]
Hi,

The guide explains quite well how to use Unit Test so you should read though it again, however I try make things clearer for you

So how do i use it? You use Unit Testing in your controller to see if a test returns an expected result, so for instance let say in your function you have a calculation to add 2 numbers 1+1 (example in guide) the expected result you would want is 2. So to achieve this follow the guide

Quote:Like most other classes in CodeIgniter, the Unit Test class is initialized in your controller using the
Code:
$this->load->library function:
$this->load->library('unit_test');

Once loaded, the Unit Test object will be available using: $this->unit

Running a test involves supplying a test and an expected result to the following function:
Code:
$this->unit->run( test, expected result, 'test name', 'notes');

Where test is the result of the code you wish to test, expected result is the data type you expect, test name is an optional name you can give your test, and notes are optional notes. Example:
Code:
$test = 1 + 1;

$expected_result = 2;

$test_name = 'Adds one plus one';

$this->unit->run($test, $expected_result, $test_name);

Then to display results use as follows in the guide:

Quote:Generating Reports

You can either display results after each test, or your can run several tests and generate a report at the end. To show a report directly simply echo or return the run function:
Code:
echo $this->unit->run($test, $expected_result);

To run a full report of all tests, use this:
Code:
echo $this->unit->report();

The report will be formatted in an HTML table for viewing. If you prefer the raw data you can retrieve an array using:
Code:
echo $this->unit->result();

Where we put the test codes, In our main Controller or In a separate test file? You could put the tests in any controller but it would say it a much better practice to have a separate controller for tests

Check out these resources below for further information and more advance features:

video - Unit testing a real world Codeigniter application
testing-codeigniter
codeigniter_phpunit_and_netbeans
unit-testing-for-codeigniter-phpunit-and-ciunit
codeigniter-unit-testing
#3

[eluser]a_h_abid[/eluser]
Hey thanks for the explanation & the links. I should check the resources now and try myself.

One another question, is it just limited to test with controllers or can I also test with libraries (and maybe models)?
#4

[eluser]Otemu[/eluser]
Hi,

is it just limited to test with controllers
mainly you would use it in your controllers however it not entirely limited to that

or can I also test with libraries
Since you can load libraries in your own custom created libraries you should be able to unit test them

and maybe models
You could test the returned result from your model within the controller, but I don't think you can unit test within the model itself. I think some of the links I provided have 3rd party tools to test directly within the model




Theme © iAndrew 2016 - Forum software by © MyBB