blob: 03819b27c14f46ce64ae24208b37b2e8fe4d9ec5 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##################
2Unit Testing Class
3##################
4
5Unit testing is an approach to software development in which tests are
6written for each function in your application. If you are not familiar
7with the concept you might do a little googling on the subject.
8
9CodeIgniter's Unit Test class is quite simple, consisting of an
10evaluation function and two result functions. It's not intended to be a
11full-blown test suite but rather a simple mechanism to evaluate your
12code to determine if it is producing the correct data type and result.
13
14Initializing the Class
15======================
16
17Like most other classes in CodeIgniter, the Unit Test class is
18initialized in your controller using the $this->load->library function::
19
20 $this->load->library('unit_test');
21
22Once loaded, the Unit Test object will be available using: $this->unit
23
24Running Tests
25=============
26
27Running a test involves supplying a test and an expected result to the
28following function:
29
30$this->unit->run( test, expected result, 'test name', 'notes');
31===============================================================
32
33Where test is the result of the code you wish to test, expected result
34is the data type you expect, test name is an optional name you can give
35your test, and notes are optional notes. Example::
36
Derek Jones31853922011-10-05 15:34:21 -050037 $test = 1 + 1;
38
39 $expected_result = 2;
40
41 $test_name = 'Adds one plus one';
42
43 $this->unit->run($test, $expected_result, $test_name);
Derek Jones8ede1a22011-10-05 13:34:52 -050044
45The expected result you supply can either be a literal match, or a data
46type match. Here's an example of a literal::
47
48 $this->unit->run('Foo', 'Foo');
49
50Here is an example of a data type match::
51
52 $this->unit->run('Foo', 'is_string');
53
54Notice the use of "is_string" in the second parameter? This tells the
55function to evaluate whether your test is producing a string as the
56result. Here is a list of allowed comparison types:
57
58- is_object
59- is_string
60- is_bool
61- is_true
62- is_false
63- is_int
64- is_numeric
65- is_float
66- is_double
67- is_array
68- is_null
69
70Generating Reports
71==================
72
73You can either display results after each test, or your can run several
74tests and generate a report at the end. To show a report directly simply
75echo or return the run function::
76
77 echo $this->unit->run($test, $expected_result);
78
79To run a full report of all tests, use this::
80
81 echo $this->unit->report();
82
83The report will be formatted in an HTML table for viewing. If you prefer
84the raw data you can retrieve an array using::
85
86 echo $this->unit->result();
87
88Strict Mode
89===========
90
91By default the unit test class evaluates literal matches loosely.
92Consider this example::
93
94 $this->unit->run(1, TRUE);
95
96The test is evaluating an integer, but the expected result is a boolean.
97PHP, however, due to it's loose data-typing will evaluate the above code
98as TRUE using a normal equality test::
99
100 if (1 == TRUE) echo 'This evaluates as true';
101
102If you prefer, you can put the unit test class in to strict mode, which
103will compare the data type as well as the value::
104
105 if (1 === TRUE) echo 'This evaluates as FALSE';
106
107To enable strict mode use this::
108
109 $this->unit->use_strict(TRUE);
110
111Enabling/Disabling Unit Testing
112===============================
113
114If you would like to leave some testing in place in your scripts, but
115not have it run unless you need it, you can disable unit testing using::
116
117 $this->unit->active(FALSE)
118
119Unit Test Display
120=================
121
122When your unit test results display, the following items show by
123default:
124
125- Test Name (test_name)
126- Test Datatype (test_datatype)
127- Expected Datatype (res_datatype)
128- Result (result)
129- File Name (file)
130- Line Number (line)
131- Any notes you entered for the test (notes)
132
133You can customize which of these items get displayed by using
134$this->unit->set_items(). For example, if you only wanted the test name
135and the result displayed:
Derek Jones31853922011-10-05 15:34:21 -0500136
Derek Jones8ede1a22011-10-05 13:34:52 -0500137Customizing displayed tests
138---------------------------
139
140::
141
Derek Jones31853922011-10-05 15:34:21 -0500142 $this->unit->set_test_items(array('test_name', 'result'));
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
144Creating a Template
145-------------------
146
147If you would like your test results formatted differently then the
148default you can set your own template. Here is an example of a simple
149template. Note the required pseudo-variables::
150
Derek Jones31853922011-10-05 15:34:21 -0500151 $str = '
152 <table border="0" cellpadding="4" cellspacing="1">
153 {rows}
154 <tr>
155 <td>{item}</td>
156 <td>{result}</td>
157 </tr>
158 {/rows}
159 </table>';
160
161 $this->unit->set_template($str);
Derek Jones8ede1a22011-10-05 13:34:52 -0500162
163.. note:: Your template must be declared **before** running the unit
164 test process.