Unit Testing in PHP [closed]
Do you guys do any开发者_Go百科 unit testing in PHP? I'm not sure if I've ever done it... what exactly is it? Can you provide a simple example?
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:
- You are not allowed to write any production code unless it is to make a failing unit test pass.
- You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
- 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.
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
Do you mean what is unit testing? Or how do i unit test effectively in PHP? If it is the former, then http://en.wikipedia.org/wiki/Unit_testing if it is the latter then https://phpunit.de/
I guess the best answer is to point to the most popular unit testing tool in PHP - PHPUnit: http://www.phpunit.de/manual/current/en/automating-tests.html
In the PHPUnit's manual you'll find everything you need to start.
There is another unit testing framework for PHP5 - SimpleTest. The name doesn't lie - you'll be up and running in minutes with it.
There is also a nice tutorial on Nettuts.
I think the manual phpunit
(a php unit testing framework) has the adequate manual to get you started into unit testing.
You can start with the automatic testing chapter (which is a fine introduction) and move onward.
I'm not doing it yet, but will start probably next week. :)
Unit testing is, as the name implies, testing units of code. These tests are performed automatically. You will write a number of tests and write a job to run these tests on regular intervals (ie, when you check in new code, or just daily at midnight).
You write tests for isolated pieces of code. You provide the code with a fixed input and check the output. The test will know what the output should be and can test the results. That way, when you change code and break existing functionality that has a unit test, the test will fail, and you will know you broke something.
It is a lot of effort to write these tests, and you shouldn't use them for everything, but they're especially useful for testing core functionality, framework, libraries etcetera.
It is important that your code is flexible and doesn't depend on a whole bunch of other code. If it does, all that code needs to be included in the test too, making it very hard to write and maintain those tests.
It is best to start writing unit tests for new code. You write the tests first and then the code. That will automatically force you to write independant code. :)
Since the other guys mentioned what unit testing is, i'll say why it's good :)
Unit testing (especially for dynamic languages like PHP) is good for several reasons and i'll point out few:
- Because PHP is dynamically typed, you can turn a boolean variable into string, array into integer and so on. If you have unit tests that covers your pre- and postconditions, you will avoid errors like that, which are not very pleasant to search for
- When you write tests (and it's good to do it after you've wrote the method) you think of how you are going to use this method in a early stage and by that - you can catch bugs or see design flaws very early in your project development.
- When you have your code covered by unit tests and you start making changes, adding features or refactoring your code, when you break something from the old code - the unit tests will tell you right away. Like that, you can be sure that all the new functionality is not breaking the old one (and bugs like that can show up later in time and can be very hard to find) and your code is in stable state.
I hope that my explanations will be useful :)
Unit testing is a simple way to make others believe your software works just as it must. Within unit testing you make sure that your add(2, 3)
function, defined as function add($a, $b) { return $a + $b; }
will return exactly 5
. This is a very useful tool when you're developing complicated software - it helps you and other find minor (hidden, logic) errors. The benefits of unit testing software is to standardize and automatize testing processes so you would not have to write function assert($whatever) { return $whatever === true; }
code and make possible human mistakes.
精彩评论