Yogesh Suthar
Yogesh Suthar

Reputation: 30488

Unit testing in plain PHP

I am curious to know how we can test our code using unit test in plain PHP.

Is their any guideline for this.

And how we can do unit testing when we use MVC architecture to create website.

Upvotes: 3

Views: 2457

Answers (5)

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

Try this tutorial

http://pear.php.net/manual/en/package.php.phpunit.intro.php

And the best is to use PHPUnit for unit testing.

Upvotes: 1

Fenton
Fenton

Reputation: 251262

You can write unit tests without any framework at all, but you'll find that you are writing a lot of the concepts yourself that you could get for free from PHPUnit or EnhancePHP.

In particular, the framework will have helpers that allow you to assert the test, helpers to give you fakes to use in your tests (especially useful in your case if you are using an MVC pattern) and a method of running and displaying the results.

If you look at the source code of a unit testing framework, you can see how all of these helpers work and it will ultimately be quicker to learn a framework than to write one.

Here is an example of a manual test - we fail the test if the result isn't right, or if there is an exception:

$passed = true;
try {
    $result = $target->addTwoNumbers(5, 2);
    $passed = 7 === $result;
} catch (Exception $e) { 
    $passed = false;
}

return $passed;

When you use a framework, this becomes much more readable:

$result = $target->addTwoNumbers(5, 2);
Assert::areIdentical(7, $result);

In both examples, I have omitted the code to set up the test, i.e. create $target as it doesn't add any meaning to the comparison.

Upvotes: 3

JvdBerg
JvdBerg

Reputation: 21866

Of course you can use PHPUnit or other unit testing frameworks. I did not like the idea of adding a lot of extra code to my projects, so I wrote a unit tester myself.

For basic unit testing you only need 2 PHP files/classes. One that performs the actual tests, and one that is the base class for a unittest.

I wrote a blog with code to demonstrate how this can be done. The unit tester can be seen in action here.

I my perception, when a question at SO pops up like this, most answers are : do not do it yourself use xxx. However this comes with a penalty: it will bloat your code, and make the completed project bigger, more difficult to maintain and dependent on third party code.

Upvotes: 1

Brian
Brian

Reputation: 8626

There is also SimpleTest ( http://www.simpletest.org/ ) - which is a very light. without the baggage of a lot of the other test-suites.

You can also use automation tools such as Selenium which essentially mimics a user sitting browsing with a mouse too - obviously you have to train it and assign tests etc.

Upvotes: 2

Sherlock
Sherlock

Reputation: 7597

There's basically only one answer to this question: PHPUnit.

It's the biggest testing framework in PHP. Best documented, most users, most capabilities, industry-standard. All you're looking for. The manual is here.

As for MVC: writing testable code doesn't have anything to do with the design pattern you're programming by.

Upvotes: 5

Related Questions