Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test-Driven Development in CakePHP

I'm using CakePHP 2.3 and would like to know how to properly go about building a CakePHP website using test-driven development (TDD). I've read the official documentation on testing, read Mark Story's Testing CakePHP Controllers the hard way, and watched Mark Story's Win at life with Unit testing (PDF of slides) but am still confused. I should note that I've never been very good about writing tests in any language and don't have a lot of experience with it, and that is likely contributing to my confusion.

I'd like to see a step by step walkthrough with code examples on how to build a CakePHP website using TDD. There are articles on TDD, there are articles on testing with CakePHP, but I have yet to find an in-depth article that is about both. I want something that holds my hand through the whole process. I realize this is somewhat of a tall order because, unless my Google-fu is failing me, I'm pretty sure such an article hasn't yet been published, so I'm basically asking you to write an article (or a long Stack Overflow answer), which takes time. Because this is a tall order, I plan to start a bounty on this question worth a lot of points once I'm able to in order to better reward somebody for their efforts, should anybody be willing to do this. I thank you for your time.

like image 671
Nick Avatar asked May 01 '13 03:05

Nick


1 Answers

TDD is a bit of falacy in that it's essentially just writing tests before you code to ensure that you are writing tests.

All you need to do is create your tests for a thing before you go create it. This requires thought and analysis of your use cases in order to write tests.

So if you want someone to view data, you'll want to write a test for a controller. It'll probably be something like testViewSingleItem(), you'll probably want to assertContains() some data that you want.

Once this is written, it should fail, then you go write your controller method in order to make the test pass.

That's it. Just rinse and repeat for each use case. This is Unit Testing.

Other tests such as Functional tests and Integration tests are just testing different aspects of your application. It's up to you to think and decide which of these tests are usefull to your project.

Most of the time Unit Testing is the way to go as you can test individual parts of the application. Usually parts which will impact on the functionality the most, the "Critical path".

This is an incredibly useful TDD tutorial. http://net.tutsplus.com/sessions/test-driven-php/

like image 105
David Yell Avatar answered Oct 16 '22 19:10

David Yell