ellabeauty
ellabeauty

Reputation: 2599

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?

Upvotes: 8

Views: 883

Answers (5)

Danon
Danon

Reputation: 2973

The only reason I can think of for using a phpunit over a custom testing, is IDE support and the fact that it can run tests in a separate process. Everything else can be easily achived using a custom script, when it comes to just running tests.

Apart from tests, phpUnit does provide useful features, like support for coverage, support for profiling, support for running tests in multiple processes, support for Xdebug.

Upvotes: 0

jsteinmann
jsteinmann

Reputation: 4752

I think the above covered it, but since there wasn't a correct answer marked let me add this: Do not make the classic mistake made in the industry which is to be driven to reinvent the wheel. If there is already a de facto standard, and community and/or a business backing a any piece of software with a roadmap... use it. The only thing good that comes out of engineering your own solution is learning how the guts work better for other established ones. So to answer your question, can you do it, yes, should you, no, and would it be educational, maybe. Why use PHP, you could just create your own language. Why use framework X, you could just create your own framework. You get the point. Unless you TRULY have a unique approach that clearly demonstrates a better product over the popular existing approaches, it's a bad path to even think about.

Upvotes: 0

Berry Langerak
Berry Langerak

Reputation: 18859

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.

Upvotes: 10

hakre
hakre

Reputation: 197765

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

No, that is not a better idea. If you create your own script, you are programming alone. With PHPUnit you have a large community of users taking care of diverse functionality that is commonly needed for unit-testing, so there is a benefit in copying and sharing the code to reduce the work.

It is software, and it does not get bad only when copied. Additionally it comes with many inputs (e.g. Configurations) and outputs (Test Result Formats, Code Coverage, Reporting) as well as integrations (Commandline Runner, IDEs, CI Servers, ...). Somewhere you would not even come close to when you start today.

However you can start test-driven-development without using PHPUnit and write the tests your own. That is great for learning TDD as well as it is great to better understand why to use a testing framework.

Upvotes: 6

edorian
edorian

Reputation: 38961

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 :)

Upvotes: 8

Related Questions