Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing in PHP [closed]

Tags:

php

Do you guys do any unit testing in PHP? I'm not sure if I've ever done it... what exactly is it? Can you provide a simple example?

like image 308
Webnet Avatar asked Jan 29 '11 21:01

Webnet


2 Answers

Unit testing

Do you guys do any unit testing in PHP? I'm not sure if I've ever done it... what exactly is it?

Unit testing is the practice to test only one single unit(class). For unit test to be any good the should be allowed to run in isolation(alone) and be very fast or else you would probably not run your tests very often.

Integration tests

You could also write integration tests which test the system as a whole and most of the times are much slower to execute. These tests should be written after the unit tests!

TDD

In the past when I coded more (frequently) using PHP language I did not practice TDD(Test-Driven-Development) at all(looking back my code was NOT up the snuff). But now when I do have to do some code in PHP, I really like to test(unit testing) my code properly. In my opinion you should do too, because it makes your convinced of the quality of your code.

Over the years I have come to describe Test Driven Development in terms of three simple rules. They are:

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

You must begin by writing a unit test for the functionality that you intend to write. But by rule 2, you can't write very much of that unit test. As soon as the unit test code fails to compile, or fails an assertion, you must stop and write production code. But by rule 3 you can only write the production code that makes the test compile or pass, and no more.

If you think about this you will realize that you simply cannot write very much code at all without compiling and executing something. Indeed, this is really the point. In everything we do, whether writing tests, writing production code, or refactoring, we keep the system executing at all times. The time between running tests is on the order of seconds, or minutes. Even 10 minutes is too long


Example

Can you provide a simple example?

First a really simple example to get you started from the phpunit website.

<?php
class StackTest extends PHPUnit_Framework_TestCase
{
    public function testPushAndPop()
    {
        $stack = array();
        $this->assertEquals(0, count($stack));

        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertEquals(1, count($stack));

        $this->assertEquals('foo', array_pop($stack));
        $this->assertEquals(0, count($stack));
    }
}
?>

As a more elaborate example I would like to point you out to a snippet of mine on github.

PHPUnit with code coverage

I like to practice something called TDD using a unit testing framework(in PHP that is phpunit).

What I also really like about phpunit is that it also offers code coverage via xdebug. As you can see from the image below my class has 100% test coverage. That means that every line in from my Authentication class has been tested, which gives my the confidence that the code is doing what it should. Keep in mind that coverage does not always mean your code is well tested. You could have 100% coverage without testing a single line of production code.

Netbeans

Personally I like to test my code inside Netbeans(for PHP). with just a simple click(alt+f6) I can test all my code. This means I don't have to leave my IDE, which I really like and helps you save time switching between sessions.

enter image description here

like image 157
Alfred Avatar answered Nov 07 '22 03:11

Alfred


I suggest you to have a look at phpunit. Some simple examples are also provided in the manual: http://www.phpunit.de/manual/3.5/en/writing-tests-for-phpunit.html

like image 44
uloBasEI Avatar answered Nov 07 '22 05:11

uloBasEI