views:

102

answers:

3

I'm looking for a tutorial which explains why and how to write useful unit tests. Specifically I'm interested in PHPUnit, however any more general might be a good one to explain that. Please note that I'm not looking for technical information on how to use PHPunit - rather the tutorial on the way of thinking!

+3  A: 

I do not know of a tutorial. But I can give you my views from what I have gathered over the years.

Writing UnitTests are meant to test the functionality of the application. Thus given this say you write a core class that has 3 functions, getData setData and displaydata (just thinking off the top of my head). You want to write a unit case that goes in set's a Data (you pass in good data and bad data because it is important to know that it errors properly). Then you check the setData, either via a DB call or with the same class using the getData, and you try and get the data with bad data, good data etc. Make sure this handles that properly. Then you write the display data and rinse and repeat.

Basically you want to test that the core class (or application classes) handles data properly (errors / good data etc). If all of the tests come out clean then you can move up and write a test for other classes that use that core class. But there is no point to moving on if your core class does not / has not pass tests or else debugging becomes a night mare. I have never really looked into PHP Unit test, I used to just write all mine given Use Case Scenarios (IE: How the functions would be used / implemented). This is a general way of thinking what to write up a test-case for.

Hope this helps and I hope I am not being too much of a retard with my line of thinking :)

Brad F Jacobs
+1  A: 

I've written a presentation covering just that as an intro. Hopefully it functions without speaker notes:
http://www.scribd.com/full/34941838?access_key=key-1u9c5kmupy1889f4o6tv (this is actually still an early revision, any feedback you could give me would be valued :)

It borrows heavily from the book "xUnit Test Patterns: Refactoring Test Code" http://rcm.amazon.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=alex010-20&o=1&p=8&l=as1&m=amazon&f=ifr&md=10FE9736YVPPT7A0FBG2&asins=0131495054"

Also look at the presentations by Sebastian Bergmann the creator of PHPUnit http://talks.php.net/index.php/Testing

If you can afford the book, it would count as my number one recommendation on learning about Unit testing thought patterns.

Finally (or perhaps as an after thought) your next steps in getting up to 'professional standards' in QA would benefit by going through the

American Society for Quality: http://www.asq.org/

With the purchase of a membership (from about $50 to $150 USD), you can sign up for courses and get certified in QA.

Furthermore, their study course materials (cost unknown) can be sent to your workplace for you to self-study, write a test, get certified and train others in QA.

Really hope that helps. - Alex

Alex C
A: 

Just found easy to start tutorial about unit testing with php, that uses SimpleTest instead of PHPUnit : http://net.tutsplus.com/tutorials/php/the-newbies-guide-to-test-driven-development/

dev-null-dweller