Unit Testing with PHPunit on Windows
I'm currently working as a teacher again temporarily, so to enable every student to be able to get into unit testing, I had to take a look at PHPunit and how to run it on Windows.
Take a look at my github repository for phpunit-demo to get the code for this article.
Installing PHP5.x on Windows
To install PHP on Windows, you can simply download one of the binary packages from the official website. Remember to pick the right architecture, x86 if you're running 32 bit Windows, x64 if you're on 64 bit Windows.After unpacking the ZIP, you can simply move the directory to the root of your C:
drive. Believe me, it will make things easier to have a short path to type.
Installing PHPunit on Windows
Installing PHPunit basically requires downloading one single.phar
file, which can be executed with PHP. You can find it on the PHPunit Getting Started section of their page, or simply save the following link: https://phar.phpunit.de/phpunit.phar.
Writing PHPunit Tests
There is an elaborate example in the PHPunit Getting Started section with OOP code, but I will quickly go through a very simple one below.:
my_class.php my_functions.php phpunit.phar README.md tests
.\tests:
my_class_test.php my_function_test.php
````php
<ul>
<li><code>my_functions.php</code> is the procedural part of code we want to test</li>
<li><code>my_class.php</code> is the OOP part of code we want to test</li>
<li><code>phpunit.phar</code> is the PHPunit test suite</li>
<li><code>tests</code> is the directory that contains our tests</li>
<li><code>tests/my_function_test.php</code> is the file that contains the tests we write</li>
<li><code>tests/my_function_test.php</code> is the file that contains the tests we write</li>
</ul>
The content of my <code>my_functions.php</code> file is the following:
```php
<?php
function my_addition($arg1, $arg2){
return $arg1 + $arg2;
}
?>
To thest this function, I can simply use the following code in tests/my_function_test.php
:
<?php
class MyProceduralTest extends PHPunit_Framework_Testcase {
/*
* Testing the addition function
*/
public function testAddition(){
include('my_functions.php'); // must include if tests are for non OOP code
$result = my_addition(1,1);
$this->assertEquals(2, $result);
}
}
?>
Running the PHPunit Tests on Windows
To run the unit tests, you need to open the command line interface on Windows. Most easily you can do that by opening your start menu and typingcmd
in the search box.
Firstly we will need to switch to the directory where the code, that is supposed to be tested and the tests themselves are placed.
If you're working with WAMP, your path could be something like this: C:\wamp\www\yourproject
. To get to that path we simply type in the cmd: cd C:\wamp\www\yourproject
.
Now we can start running our tests:
C:\php5\php.exe phpunit.phar tests\my_function_test.php
If you want to test a class, you don't need to do any include()
in your test file and can run a command like the following:
C:\php5\php.exe phpunit.phar --bootstrap my_class.php tests\my_class_test.php
This for example is the output of running the my_function_test.php
:
phpunit-demo (master) $ php phpunit.phar tests\my_function_test.php
PHPUnit 3.7.32 by Sebastian Bergmann.
.
Time: 83 ms, Memory: 2.50Mb
OK (1 test, 1 assertion)
OK! Our first test passed! Easy, right? Right! Now if we change the value that we expect to get if we add 1 and 1, we will receive an error message, that tells us which test fails. This is extremely handy if you're not testing one, but 500 functions.
Changing:
$result = my_addition(1,1);
- $this->assertEquals(3, $result);
+ $this->assertEquals(3, $result);
Error Message:
PHPUnit 3.7.32 by Sebastian Bergmann.
F
Time: 272 ms, Memory: 2.75Mb
There was 1 failure:
1) MyProceduralTest::testAddition
Failed asserting that 2 matches expected 3.
/home/geronimo/public_html/phpunit-demo/tests/my_function_test.php:12
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
The most valuable line is probably Failed asserting that 2 matches expected 3.
, because it tells both the return value and which one we thought we would get in the test.
Writing Your Own PHP Tests
To write your own tests, you can have a look at the list of all assertions for PHPUnit. It has a lot of practial functions that can test fortrue
or false
, if a function outputs a number or if a value is lesser or greater than another. All assertion functions come with handy examples, from which you can build your own tests.
The functions may seem very basic, but since it also supports regulary expressions, you can test for many of the string conversions that are typical for web projects, or url escaping, user input validation and so on.