8

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?

Fluffeh
  • 31,925
  • 16
  • 62
  • 77
ellabeauty
  • 2,199
  • 3
  • 19
  • 24
  • 1
    Actually it is slightly below 1M without self-tests. But I suggest you go along and implement your own unit testing framework. By the time you are done with it (or, more likely, give up), you will have your answer... – Tgr Aug 08 '12 at 11:47
  • 1
    Looking through the docs? The docs start here: [PHPUInit Manual](http://www.phpunit.de/manual/3.6/en/index.html) - take a look on the left side. There is a *little* bit more than assertions in PHPUnit ;) But you are right, from a testers perspective you interface with the assertions first, however an assertion function alone does not make a testing framework. – hakre Aug 08 '12 at 12:03
  • Because PHPUnit it's for "Test Driven Development", not for "Test After Development". TDD comes from Agile Methods. If you need to test legacy code, take a look to [PHP Pro Refactoring](http://www.apress.com/9781430227274/) – Fabio Mora Aug 08 '12 at 12:25
  • 1
    PHPUnit works just fine for non-TDD too. As should be clear from the answers, the assertions make up a tiny fraction of its capabilities. – David Harkness Aug 08 '12 at 15:44
  • Also Related: **[Simple test vs PHPunit](http://stackoverflow.com/questions/34312/simple-test-vs-phpunit)** – hakre Aug 28 '12 at 08:17

4 Answers4

10

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.

Berry Langerak
  • 17,465
  • 2
  • 39
  • 53
8

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

Community
  • 1
  • 1
edorian
  • 36,948
  • 13
  • 119
  • 141
6

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.

hakre
  • 178,314
  • 47
  • 389
  • 754
0

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.

jsteinmann
  • 3,494
  • 2
  • 15
  • 20