Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First Shot at Testing Laravel 4 apps (PHPSpec/BDD vs. PHPUnit/TDD)

I have been wrestling with this question for far too long now. I know I need to just jump into one or the other since they are both obviously viable/useful tools, but have been stuck on the fence, researching both, for weeks.

PHPUnit vs. PHPSpec - Which one may lead to better long-term maintainability and programming practices?

I have talked to several seasoned PHPUnit -> PHPspec converts/users who now swear by PHPspec, claiming that it promotes better design thanks to its BDD approach. However, since it is a much newer tool there is a lack of community/tutorials in comparison with PHPUnit.

Currently, my PHP development is focused around Laravel 4 development.

Where PHPUnit is basically baked into the Laravel framework, it seems like that may be the easier choice up front, but I really like the BDD aspect of PHPSpec. However, it seems that PHPSpec doesn't want to play nicely with Laravel's facade setup (which I'm also finding may be a good practice to steer clear of when possible).

My real conundrum is which of these will be more likely to be successfully integrated into an existing Laravel 4 project with zero existing test coverage.

I had an early/brief stab at integrating both PHPUnit and PHPSpec into an existing project, but really just wasn't sure where to begin with writing the initial tests and ended up scrapping them for the time being.

I am really trying to sort out which one may be more worth investing some time into in the long run.

Any resources/insight from anyone who has experienced both would be greatly appreciated.

like image 963
Erik Aybar Avatar asked Apr 29 '14 19:04

Erik Aybar


1 Answers

PhpSpec

PhpSpec is a design tool. It will make your life hard in the beginning if you're not used to TDD (test-first approach).

You'll learn a lot during the process though. You will also swear a lot. Many things, which are considered a bad practice, are not possible with PhpSpec (like partial mocks for example). You'll have to understand it and embrace it. Testing Laravel's facade's is hard, 'cause that's not OOP.

It's much harder to test an existing code with PhpSpec, since you have to make it testable first (which is easier with a test-first approach).

PhpUnit

On the other hand, PhpUnit is a testing tool. You can use it with TDD, BDD or whatever you call it. But you don't have to.

Choice

If your aim is to learn how to design better and you're commited to test-first approach, PhpSpec will help you. If your aim is to test or verify, PhpUnit is your friend.

TDD vs BDD

It's not either TDD or BDD. At least from a code level perspective, they're the same. BDD started as a TDD-redefined, offering a better terminology. Now it goes beyond the code level and it's so much more than that. These days I prefer to use a term proposed by Gojko Adzic - Specification by Example.

like image 193
Jakub Zalas Avatar answered Sep 20 '22 19:09

Jakub Zalas