Page 1 of 1

Unit Testing libraries in PHP

Posted: Fri May 26, 2006 11:26 am
by Maugrim_The_Reaper
Since its about a tool and not a question on writing them I put it here...

I've spent the last few months tinkering with SimpleTest, and recently thought about looking at PHPUnit2 - what I'm not clear on is where they differ, and which one people prefer. PHPUnit2 (outwardly at least) seems to see a lot more development, and the 3.0.0 alpha looks like its adding some new features. (and well, PHPUnit looks kind of nicer what with the wealth of documentation, the flashy output, and such).

Is there a significant difference between the two? Which do you prefer using? Do both integrate well into Eclipse (IDE)?

Posted: Fri May 26, 2006 4:50 pm
by feyd
I'm still not sold on Pear being used for everything, so I go with SimpleTest. Not that they are all that different, but since the author actually posts here, it gets more weight. Beyond that, it was the most straight forward. Granted, it took me awhile to actually sit down and force myself to figure it out. Marcus' tutorials and documentation helped a lot once I had the patience to read it.

Posted: Sat May 27, 2006 7:49 pm
by Nathaniel
McGruff uses SimpleTest, so I use it too. :)

I've never actually took a close look at PHPUnit2.

Posted: Sat May 27, 2006 9:45 pm
by Ambush Commander
Same here. Didn't really know there was a PHPUnit2. I really like SimpleTest's system.

Posted: Sun May 28, 2006 3:12 am
by Ree
Simpletest is good indeed. It's available as Eclipse plugin as well.

Posted: Sun May 28, 2006 6:14 am
by Chris Corbyn
I still need to jump on this bandwagon :)

Most likely I'll go with simpletest because it look like a few people around here can help me out with that one - but someone mentioned the examples in the docs don't even pss the tests with PHP5, is that right?

Can I unit test something once it's already been written or is that completely besides the point? Most people seem to write test, and then write code the pass the test.

Posted: Sun May 28, 2006 7:28 am
by Maugrim_The_Reaper
Most likely I'll go with simpletest because it look like a few people around here can help me out with that one - but someone mentioned the examples in the docs don't even pss the tests with PHP5, is that right?
That was me ;). The example on the site fails because of a minor error or two - not sure whether that has been fixed yet. The remaining example is pretty good, and very concise. Might want to look for other tutorials to fill in some of the blanks you'll get left with from a quick overview. I would say it's not hard to pickup - it's like a mini-language, you need some practice but not much. Can measure the early learning curve in hours...
Can I unit test something once it's already been written or is that completely besides the point? Most people seem to write test, and then write code the pass the test.
Yes, you can. Though I would suggest writing the tests first-off without referencing the class too much (even if the class exists it might offer a few new insights). Then amend them, or the class to pass them. It's also a good idea to write tests before making any changes to an existing class - so you can change, test, change, test, in a way that let's you catch bugs fast. One thing I found hard originally is that in writing tests you may find some classes need some additional methods to even allow testing. For example, it you read my thread on ServiceLocator/Singleton/Registry (last 2 days) I needed a hasService/hasSearchLocation methods so my tests could check if such existed (basic getters for a private property). This extra bits and pieces are used solely for testing. I've seen a few people mention classes should also be written to facilitate easier testing...so that falls under this heading.

PHPUnit has received 0 votes so far...hmmm. Another factor I though of - PHPUnit requires PEAR I think, but SimpleTest can be bundled with a distributed application easily.

Posted: Sun May 28, 2006 7:39 am
by Chris Corbyn
Maugrim_The_Reaper wrote:
Can I unit test something once it's already been written or is that completely besides the point? Most people seem to write test, and then write code the pass the test.
Yes, you can. Though I would suggest writing the tests first-off without referencing the class too much (even if the class exists it might offer a few new insights).
Well then, if I'm gonna jump on the bandwagon it might as well be for something useful and practical. I'll try to include some tests with the next release of swift :)

/goes off to get simpletest

Posted: Sun May 28, 2006 8:47 am
by sweatje
d11wtq wrote: Well then, if I'm gonna jump on the bandwagon it might as well be for something useful and practical. I'll try to include some tests with the next release of swift :)

/goes off to get simpletest
You might also have to look into one of Marcus's other projects fakemail to help out in your testing.