Page 1 of 1

Unit Testing

Posted: Thu May 15, 2008 5:49 am
by Bruno De Barros
I have been wondering about Unit Testing long enough. I went to Wikipedia, and I saw Zend Framework provides some kind of method for unit testing. But I wonder... What is it, in PHP terms, and what is it for? I use MVC, so couldn't I just test the function on the model, the execution of the controller, and stuff like that? I mean, what is the purpose of Unit Testing, and any basic example on how to do it?
EDIT: From the PHP point of view.

Re: Unit Testing

Posted: Thu May 15, 2008 6:06 am
by onion2k
All unit testing is is a method of automating the testing process. If your site does 500 different things you don't want to have to test all 500 things every time you change something, so most developers don't bother and bugs sneak in, especially where objects interact. If you write a suite of unit tests you can see at a glance whether a change has broken anything in your code, not just the bit you've changed and (maybe) tested properly.

Re: Unit Testing

Posted: Thu May 15, 2008 1:46 pm
by Bruno De Barros
Yeah, if you have a function that is used in several places, a change in its code can solve a problem in one place, but create problems in other places. But doesn't Unit Testing require a massive load of extra code?

EDIT: I currently log all PHP errors and use a debug() function to add a debug message, and save all the variables at that scope, at that time. That way, I can see if any variable is giving problems, being incorrectly assigned, read my messages to understand what went right/wrong, and read the PHP errors for improper code, or any problem that might have come up. It took me long to build the whole library but now it saves everything, one file per request, full HTML and all, to make it look good and easy to understand :D. It hasn't failed me, so far. But what advantages would you see on Unit Testing? I mean, from what I've read on the web, it's far more complicated than my simple debug() function.

Re: Unit Testing

Posted: Thu May 15, 2008 5:05 pm
by Chris Corbyn
It may be "more complicated" to learn, but I'd rather decipher the results of a unit test than try to make sense of the results of your debug function ;)

Yes, it does require you write a lot of extra code; but not in the production sense, only for the tests. The tests work well because they are coded to do what you tell them to do. It's probably just something you should try and get a feel for yourself :)

Re: Unit Testing

Posted: Thu May 15, 2008 5:33 pm
by onion2k
Bruno De Barros wrote:Yeah, if you have a function that is used in several places, a change in its code can solve a problem in one place, but create problems in other places. But doesn't Unit Testing require a massive load of extra code?
It certainly feels like a lot of extra code but that isn't really a problem. I'd rather spend time writing tests in the first place than debugging things later. Especially because you write the unit tests before you write the proper code, which actually makes you think more about the code and what it needs to return in each circumstance, and consequently actually makes writing the proper code easier because your specification is better.

Another absolutely huge benefit that comes from unit testing is that when you're sharing the development of a project with other people you have a standardised system for making sure the code works.
Bruno De Barros wrote:EDIT: I currently log all PHP errors and use a debug() function to add a debug message, and save all the variables at that scope, at that time. That way, I can see if any variable is giving problems, being incorrectly assigned, read my messages to understand what went right/wrong, and read the PHP errors for improper code, or any problem that might have come up. It took me long to build the whole library but now it saves everything, one file per request, full HTML and all, to make it look good and easy to understand :D. It hasn't failed me, so far. But what advantages would you see on Unit Testing? I mean, from what I've read on the web, it's far more complicated than my simple debug() function.
Unit testing naturally breaks down into, well, units. If a problem occurs anywhere it's very obvious what it is and where it happened. Your approach of having a monolithic report for each script sounds like it would be tricky to track down a problem in a very large and complex script. When you have things that use 10 different objects, each one a thousand lines long, extending other objects ... then some sort of finely granular testing system is lovely. I've only just started on unit testing and TDD really (using it on personal stuff at the moment, only for the last month or so) and already I can utterly see the benefit. The only problem with it that I can see is persuading my boss that it's a good thing when he can't see any more than a green bar on a screen that says "3/3 test cases complete: 32 passes, 0 fails and 0 exceptions.". :)

Re: Unit Testing

Posted: Fri May 16, 2008 2:41 am
by Bruno De Barros
wow the maker of swiftmailer just spoke to me. :lol:

I understand what you mean, onion2k, and it looks good. I sure hate it when a bug comes up that ruins everything and I don't know where it came from, and unit testing makes sure the code works, before testing (which on big applications with tons of different pages, that might be very hard to do). I have had the embarrassment of once going to a meeting with my customer and a small bug comes up. He didn't notice it, but I did.

@Chris: Yeah, I am going to try it, as soon as I finish this job I'm working on.

Any recommendations on what Unit Testing framework I should use? I found a list of them, but would like to hear from people that use Unit Testing.

Re: Unit Testing

Posted: Fri May 16, 2008 3:13 am
by onion2k
I use SimpleTest.

Re: Unit Testing

Posted: Fri May 16, 2008 4:45 am
by Bruno De Barros
I saw Simple Test, and it did give me a lot of ideas for when I am testing. I am going to try using it while making a simple blog script, and I'll see how it feels. So far, I feel I'd be better off with a mixture between SimpleTest and my testing library. But it all might change when I use it.