Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is using PHPUnit better than creating your own testing script?

Looking through the docs, I see that PHPUnit only offers these functions:

http://www.phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.assertions

All of them can be very easily implemented in a custom testing script, within less than 1K lines...

PHPUnit has 2 MB of files (around 200), which include a huge amount of classes. Also, PHPUnit only runs from the command-line :(

Wouldn't creating my own script be a better idea?

like image 920
ellabeauty Avatar asked Aug 08 '12 11:08

ellabeauty


People also ask

Why do we use PHPUnit?

With unit testing we test each component of the code individually. All components can be tested at least once. A major advantage of this approach is that it becomes easier to detect bugs early on. The small scope means it is easier to track down the cause of bugs when they occur.

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.

Where do I put PHPUnit test?

You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file.

Why is it important for unit tests to run quickly?

The speed of detecting non-working code depends on the tools used for continuous integration. Tests can be set to run either a one-time check at a certain time interval or can be run immediately in real-time to review changes. In short, unit tests help developers detect problems immediately, then fix them quickly.


2 Answers

PHPUnit is a beast. It's large, sometimes a little counter-intuitive, and it has it's flaws. Your code would - naturally - be intuitive, and flawless for your direct requirements. I too have often pondered if it wouldn't be a step forward to write my own testing framework, but... it's not. You can finish a rudimentary testing framework in a day, but:

  • PHPUnit is integrated in most modern IDE's;
  • PHPUnit nicely works with XDebug for code-coverage reports;
  • PHPUnit can work together with Selenium for integration tests;
  • PHPUnit is used by many programmers, meaning your tests are instantly clear to a lot of them;
  • PHPUnit can be integrated into a CI setup such as Travis CI.
  • PHPUnit has a mocking library;
  • Most importantly: PHPUnit works.

There are a lot of reasons against writing your own.

like image 97
Berry Langerak Avatar answered Sep 18 '22 08:09

Berry Langerak


Two points that @hakre didn't touch upon:

Code coverage

Doing pretty reporting on code coverage (visualizing how much code got executed) is not all that easy even so xDebug enables you do get going rather quickly there are a couple of edge cases and annoyances that take up quite some time to build.

PHPUnit helps you out with a nice report.

Reporting

The most important thing when testing is the ability to quickly figure out what went wrong.

Building a nice diff yourself for all the stuff in PHP (exceptions, objects, strings, xml, json, etc.) is quite time consuming

Also at some point you will want to move to a continous Integration server like Jenkins and a testing framework like PHPUnit will already produce all the needed artifacts (junit.xml, clover.xml) that enables you to set up CI for your projects in half an hour.


So all in all even if you don't use all the advanced features and helpers (like mocking, process isolation to test legacy code, outputBuffering, exception helpers) you get a base setup that will be able to grow with you when your project grows and get more mature.

CLI only

Btw. there is a web interface to phpunit called Visual PHPUnit that runs in a browser. Even so, to be honest, I don't have any clue as to why anyone would want that. Maybe with out refresh but I'd rather a script loop on a cli terminal than. But to each their own :)

like image 21
edorian Avatar answered Sep 19 '22 08:09

edorian