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

1

15

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.

Autocephalous answered 29/4, 2014 at 19:8 Comment(3)
I have been wrestling with the same thing, Are you a member of Laracasts? Jeffrey Way has a few videos on both PHPUnit and PHPSpec. Because I'm brand new to testing, I have decided to start with PHPUnit because there is much more documentation and it seems easier to start with.Wellspoken
Hah @MitchGlenn that is my exact situation. I've been with Laracasts since last December and have learned a ton from Jeffrey Way (he seems to have taken a liking to PHPSpec lately). My developer's curiosity leads me to want to dive into PHPSpec, but it seems the wiser more time/cost effective choice would be PHPUnit for that exact reason. Thanks for the input.Autocephalous
I joined 6 days ago and am 65 videos in, I feel like my brain is going to explode soon from so much great content. I want to go with PHPSpec from how great Jeffrey makes it sound, but as you say, to be more time effective, I'm starting with the easier route to get the job done.Wellspoken
M
34

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.

Mccurdy answered 30/4, 2014 at 12:9 Comment(2)
Perfect. That is pretty much the gist I got from reading about and experimenting with the two... In my case, I probably need to start with PHPUnit, since I really need to get some testing implemented in to some existing projects. And then!... leap into the PHPSpec arena. I've been using rails/rspec (*gasp!) for the last few weeks and have been enjoying that. Kind of split between two worlds at the moment.... Thanks for the insight.Autocephalous
Am I understanding this correctly: Any time one of your functions uses a laravel facade, it is longer testable with phpspec? I think this may be related to another issue I am having (#27974426)Galvani

© 2022 - 2024 — McMap. All rights reserved.