Translator's Preface:
Recently I began to learn PHPUnit ( xUnit family framework ) and was surprised to find that there are no articles about automated tests for the best teapots in Russian.
In the first chapter of the PHPUnit documentation, the examples tell you very well what automatic testing is.
Even good programmers make mistakes. The difference between a good programmer and a bad programmer is that a good programmer uses tests as often as possible to find his mistakes.
The sooner you start testing, the higher your chances of finding a bug, and the lower the price of corrections.
This explains why postponing testing until the transfer of the program to the customer is a very bad practice. Most of the errors will not be found, and the price of the correction will be so high that you will have to draw up a large work schedule, because Immediately you can not fix them all.
Testing with PHPUnit is not fundamentally different from the testing that you are already doing. This is just a different approach to work. The difference is as follows: in one case, you simply
check that your program works as expected, in the other — you
run a series of tests that are executable code fragments for automatically checking the correctness of software parts (modules).
These executable code fragments are called unit tests.
In this article, we will make the path from the elementary test, which simply displays the result of its work to a fully automated test.
Suppose we were asked to test the array built into PHP (
array
). At one of the testing stages, it is necessary to check the operation of the
count()
function. We expect that for the newly created array, the
count()
function returns 0. After adding an element to the array,
count()
should return
1
.
Example 1.1 demonstrates what we want to check.
')
Example 1.1: Testing array operators<?php<br>$fixture = array();<br>
// $fixture is expected to be empty. <br> <br>$fixture[] = 'element' ;<br>
// $fixture is expected to contain one element. <br>?> <br><br>
* This source code was highlighted with Source Code Highlighter .
The easiest way to check if we got what we wanted was to display the result of the
count()
function on the screen.
The output must be made before and after adding the element, see Example 1.2.
If we first get
0
and then
1
, then
array
and
count()
work as expected.
Example 1.2: Using screen output to validate array operators<?php<br>$fixture = array();<br>
print count($fixture) . "\n" ;<br> <br>$fixture[] = 'element' ;<br>
print count($fixture) . "\n" ;<br>?> <br><br>
* This source code was highlighted with Source Code Highlighter .
Test output:
0
one
We would like to move from tests that require manual processing of results to tests that can be performed automatically.
In Example 1.3, we add to the test a comparison of the expected result and the actual value, we will output
ok
if the expected and actual results coincided.
If the output is
not ok
, then an error has occurred somewhere.
Example 1.3: Testing array operators, comparing expected result and actual value<?php<br>$fixture = array();<br>
print count($fixture) == 0 ? "ok\n" : "not ok\n" ;<br> <br>
$fixture[] = 'element' ;<br>print count($fixture) == 1 ?
"ok\n" : "not ok\n" ;<br>?> <br><br>
* This source code was highlighted with Source Code Highlighter .
Test output:
ok
ok
And now we make a comparison of the expected and actual results into a special function that throws an exception if the condition is not met, see Example 1.4.
This approach gives us two advantages: writing tests is considerably simplified, the test generates a message only in case of an error.
Example 1.4: Using the approval function to test array operators<?php<br>$fixture = array();<br>
assertTrue(count($fixture) == 0);<br> <br>$fixture[] = 'element' ;<br>
assertTrue(count($fixture) == 1);<br> <br>function assertTrue($condition)<br>
{<br> if (!$condition) {<br>
throw new Exception( 'Assertion failed.' );<br>
}<br>}<br>?> <br><br>
* This source code was highlighted with Source Code Highlighter .
The test is fully automated. Instead of just
testing that we performed in the first version of the test, we got an
automatic test .
The purpose of using automatic tests is to make fewer errors in the code.
Even if your code is still not perfect and you are using excellent tests, start practicing automated tests and you will find a significant reduction in errors.
Automatic tests will provide you with confidence in your code. Based on this confidence, you can make more bold and decisive changes to your program (Refactoring), you can improve relationships with colleagues and clients.
Every day, going home, you will know that the program has become much better than it was in the morning.
Continued:
Part 2 ,
Part 3 ,
Part 4