Page 1 of 1

What to TDD and what not?

Posted: Sun Dec 02, 2007 11:39 am
by matthijs
In another thread I read this:
Jcart wrote:Since controllers, models and views generally should not be Unit Tested since they are application specific, this is a tough one to follow.
Not? That's the first time I hear this. So if I'd build something with, say, the zend framework I don't have to worry about testing my app code?

Is this true? I just started with TDD and what I've done so far is write tests for my models and do a bit of web testing and testing of forms.
(didn't want to hijack the other thread so started a new one)

Re: What to TDD and what not?

Posted: Sun Dec 02, 2007 1:40 pm
by alex.barylski
matthijs wrote:In another thread I read this:
Jcart wrote:Since controllers, models and views generally should not be Unit Tested since they are application specific, this is a tough one to follow.
Not? That's the first time I hear this. So if I'd build something with, say, the zend framework I don't have to worry about testing my app code?

Is this true? I just started with TDD and what I've done so far is write tests for my models and do a bit of web testing and testing of forms.
(didn't want to hijack the other thread so started a new one)
Not so much a matter of truth, but rather that of personal opinion.

Personally, I test *everything* manually anyways so once I do start unit testing I will likely test controllers/model at the least. My Views are difficult to test automatically.

In my experience they all change to frequently to ignore unit testing if your going to bother with it at all.

Cheers :)

Re: What to TDD and what not?

Posted: Sun Dec 02, 2007 2:00 pm
by John Cartwright
matthijs wrote:In another thread I read this:
Jcart wrote:Since controllers, models and views generally should not be Unit Tested since they are application specific, this is a tough one to follow.
Not? That's the first time I hear this. So if I'd build something with, say, the zend framework I don't have to worry about testing my app code?

Is this true? I just started with TDD and what I've done so far is write tests for my models and do a bit of web testing and testing of forms.
(didn't want to hijack the other thread so started a new one)
Notice how I said should not be Unit Tested. I still believe applications should be tested :D If you continue to read down the thread you linked, you'll see not everybody agrees with me :D

Posted: Sun Dec 02, 2007 2:30 pm
by matthijs
I still believe applications should be tested Very Happy
you're sure? :D

So even though not everyone agrees (never will), I still am interested to know: what is the reason you don't want to test your application specific code? because almost everything is done in your framework code and nothing can go wrong in your application classes? is it too cumbersome? If you would write, say a widgetsModel class, shouldn't that be done the TDD way?

Posted: Sun Dec 02, 2007 5:17 pm
by Maugrim_The_Reaper
There's a difference between unit testing and TDD. TDD requires you write a test specifying a wanted behaviour, write the code to pass the test (achieve the behaviour), refactor as needed, and repeat. Whether it's library, framework, model, view or controller code is irrelevant - you can apply TDD to them all. I think the biggest problem in reality is that few frameworks (if any) are easy to get TDD applied with. A neat approach is to extend your unit testing framework to takeover the basic steps of getting any controller/model ready for execution (e.g. I use a post() method as a shortcut to routing/dispatching/executing Zend Framework controllers).

As for whether application code is too dynamic to be tested - there's a huge difference between TDD (design the controller; tests are coincidental) and unit testing (test, test, test). Is application code too dynamic? It can be, but I think much of that tends to be present in heavy controllers where the logic has not been refactored into more stable library classes. In essence - it's a code smell.

To further press an argument - have you looked at how prevelant controller/model/view testing is for something like Ruby Rails? Python Django? Or perhaps more interesting - how each has committed test-execution frameworks to make simulating controller runs as simple as post(), is_redirect(), etc? I think it's more a symptom of PHP's current low adoption of TDD that the main PHP frameworks seriously lack any such toolkit - not that TDD or unit testing has no place in MVC.

None of this, of course, is an excuse for not having acceptance tests ;). But not applying TDD to a controller, in my opinion, is just a throwback to pre-TDD practices and encouraged by frameworks like Zend which has, from experience, extremely little code developed using XP/Agile practices.

Posted: Sun Dec 02, 2007 5:32 pm
by Jenk
I wirte tests for everything. Tests are not just for frameworks, they are for any scenario, application specific or not.

Posted: Sun Dec 02, 2007 6:03 pm
by feyd
Yeah, I test just about everything, but I compartmentalize everything so when the application goes away, so does its tests.

Posted: Sun Dec 02, 2007 9:04 pm
by Jenk
feyd wrote:Yeah, I test just about everything, but I compartmentalize everything so when the application goes away, so does its tests.
Ditto :)