Welcome Guest, Not a member yet? Register   Sign In
How to use Unit Testing v2.1.4
#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


Messages In This Thread
How to use Unit Testing v2.1.4 - by El Forum - 11-06-2013, 08:10 PM
How to use Unit Testing v2.1.4 - by El Forum - 11-07-2013, 10:14 AM
How to use Unit Testing v2.1.4 - by El Forum - 11-07-2013, 12:49 PM
How to use Unit Testing v2.1.4 - by El Forum - 11-08-2013, 04:16 AM



Theme © iAndrew 2016 - Forum software by © MyBB