r/PHP•
andyzweb

The Big Unit Testing Discussion Thread™

My background is mainly in C (with C++ or ObjC where necessary). I did a lot of work with signal processing and audio.
In my experience practicing multiple testing styles provides the best results. I found having unit tests to be the most helpful. With two months of PHP under my belt I am ready to dig into more formalized testing for PHP.

Use this thread to share links/thoughts/insights/discussion/questions about testing and PHP with a focus unit testing.

Posted Mar 28, 2012, 9:50 PM UTC

913
jmalloc•

If you're going to be using PHPUnit (a de facto standard?) then I strongly recommend looking into using Phake for creating any test doubles (as opposed to the built-in PHP mock objects).

It may not be apparent at first but the increased flexibility of Phake is near enough to a necessity once your test library grows. The most obvious example that springs to mind is the fact that you can change the behavior of an already-stubbed-method (not possible with PHPUnit mocks). This allows you to build up collections of "canned" test doubles that can be reused across tests.

2
Veonik•

Thanks for this. I find PHPUnit's mocking to be a bit lacking a lot of the time and I'm never really happy with the syntax. Looking forward to giving Phake a try.

2
headzoo•

There's two primary purpose I unit test my code:

  • Code that's written to be testable is often better code. When you're having a hard time building a test for a class, then you should reconsider how you've written it. A class that can't be tested as a single logic unit probably has dependency issues that should be removed.

  • Automated testing finds bugs where you wouldn't expect to find them. Sometimes making changes to one part of your system has unexpected consequences in other parts of your system. Parts of your system you didn't think would be affected, so you didn't bother to give them a quick test. Running a test suite on your entire system after a big change will help find those bugs, and help identify coupling in your system that shouldn't exist.

2
negativeview•

I recently did a tech talk at my job about unit testing, with a focus on PHPUnit. I can answer any specific questions. I may even be able to share the slides if I find them at home.

1
[deleted]•

[deleted]

1
jere_jones•

It's been a few days. If you remember, I would love to see the slides.

1
Veonik•

PHPUnit is extremely good at unit testing and is considered basically a standard. However, recently someone linked a thing called Codeception that caters towards a more behavior driven testing (Codeception actually calls itself a BDD framework) which is actually very nice, especially for writing functional and acceptance tests.

Currently, I'm still playing with Codeception in my free time but I feel strongly that I'll soon be moving my functional testing away from native PHPUnit tests in my professional projects. It's worth noting that Codeception actually uses PHPUnit so the two kinds of tests can, in theory, be combined... though there are apparently some issues with doing that.

All that said, I will probably not use Codeception for straight-up unit testing. While the syntax provided by Codeception is helpful, I prefer a more standard unit test, where a Test one-to-one mirrors a class and most of the testable methods and behaviors, rather than 'scenario' based unit testing that Codeception provides.

1
KayRice•
 pear run-tests # phpt

Spend time writing unit tests not fighting your (probably oversized) unit testing framework. I run unit tests to guard against regressions

1
wvenable•

I didn't put much faith in unit testing but I recently started doing unit testing on a new project (after building my own unit testing / code coverage framework) and I've found it helpful enough to continue doing it.

0
Veonik•

Out of curiosity, why did you opt to build your own framework?

3
wvenable•

First of all, I wanted something more integrated with my other code. But, more importantly, because PHPUnit and PHPCodeCoverage are huge and complicated for what you get. I created something with similar functionality (in some cases more, in some cases less) but literally a fraction of the size.

0
Veonik•

Very interesting. For me, size was not an issue because not only do we not ship PHPUnit with our product, but we don't ship our tests either. If you're willing, I would be interested at taking a look at your framework to see some of the differences. I bet making your own testing framework was a great exercise of ability. Props to you for that :)

3
wvenable•

The "size" itself isn't so much the issue but rather the complexity behind that size (which comes out even in usage). I'm currently building a simple (but not small) framework and this is part of it -- which sort of explains why I'm doing it in the first place.

For unit testing, I wanted something that got to essence of PHPUnit but without the cruft. I also wanted less of a Java-like design and more of PHP-like design. For example, I avoid annotations because they aren't natively part of PHP but then I make use of other PHP-specific language features. But mostly the usage is very similar to PHPUnit.

The trickiest part was generating the code-coverage metrics with all the features of PHPCodeCoverage and PHPUnit. There was a lot of trial and error and studying PHPCodeCoverage to get what I wanted from xdebug. My entire code coverage feature is done in one 500-line class.

I still have to do Mock objects; but my implementation is going to be dead-simple. Instead of the whole fluent API thing that PHPUnit has going on; my mock objects will just allow you to replace any stubbed method with a closure. I'll get all the same functionality (and more) but it'll be much smaller and more straight forward. Of course, all this requires PHP 5.3.

1
[deleted]•

[deleted]

1
wvenable•

Reusing other aspects of the framework. One exmaple: for years I've had my own class for runtime assertions -- so I wanted to reuse that. All the code could go through PHPUnit if I wanted.

Yes, I'm reinventing the wheel but sometimes you get better wheels that way. See this post for more information.

1
[deleted]•

[deleted]

-4