SteveAzz's avatar

When to use PHPSpec, Behat and PHPUnit

So I went trough the Testing Collection and the new series that Jeffery have done on his awesome package. I am understanding what he is doing and why is he doing it,

But when I start trying making my own tests I get very confused.

When should I use PHPUnit or Intergrated or PHPSpec or Behat?

I know that PHPunit is more function testing and PHPSpec is for the behavior of the test and Behat for acceptance testing.

But what kind of examples is there, for example lets say I have a form with a file upload functionality, and all it has a simple move to specific folder and store the name inside of the database.

Also what is a good example of an acceptance test?

Sorry for a lot of question but it is kind of overwhelming and I am not sure where to continue after I have seen the videos.

0 likes
9 replies
JohnRivs's avatar

This is one way to go about it:

If the project is small, and I don't really need a very detailed test suite, I'd go with PHPUnit. It's a jack of all trades. You can use it for your unit tests, Laravel provides helpers. Also, thanks to @JeffreyWay's Integrated, you'll be able to hook into Laravel and run functional/acceptance tests.

For a larger project, I'd go with PHPSpec for my unit testing, and Behat + Laravel extension for my func/acc testing. The ability to define in Gherkin the scenario, and then run 2 suites of tests at the same time is very handy.

4 likes
olimorris's avatar

I know exactly how you feel @Colossus. I was in that position only a few weeks ago.

PHPUnit and unit testing in general is great for when you want to test a small part of your application like a method. A week ago I designed a form whereby a user can add tags to a job they're posting. Those tags are separated by comma's and I wanted to convert the input into an array to insert into a table. I wrote my method within PHPUnit first to assert that the input would be filtered, sanitised and returned as an array. Then I could begin refactoring and extracting the code into a trait. You could have quite easily done this with PHP Spec as well - that is another tool for Unit testing.

My personal favourite though is Codeception. You can use it for Integrated, Acceptance and Unit tests. It hooks into PHPUnit (meaning all PHPUnit methods are available to it) and I find the syntax and simplicity of it all really easy and I can run all of my different types of test all at once.

Regarding your example:

"...for example lets say I have a form with a file upload functionality, and all it has a simple move to specific folder and store the name inside of the database."

Well for that, you could always use PHPUnit or PHPSpec with Mockery to determine that when the file upload class is mocked and a method is called, the result is as you expected (i.e. true, signifying the file was uploaded). And for a more integrated test, you could use Codeception with its Filesystem module to check that an actual file is created ($I->seeFileFound or $I->seeInThisFile).

Jeff had a very good tutorial on Mocking with PHPUnit on tuts+ some time ago and it's quite related to file uploads: http://code.tutsplus.com/tutorials/easier-testing-with-mockery--pre-69600

4 likes
CiaranMcNulty's avatar
Level 1

So to start with, PHPUnit is something you can use to test anything.

If you feel like you only want to learn one tool right now, but you want to do lots of different types of testing, it might be the best one to start with. There's certainly a lot more resources around as it's been going strong for 11 years.

However, because it's so universal you have to figure out how to do each type of testing well. There are more specialised tools that can be more efficient for their target areas…

  1. PhpSpec, which I maintain, is a tool for designing classes that work well. It's best used to drive the design of a clean, decoupled, isolated domain model without getting too involved in infrastructure.
  2. Behat is a tool for validating your system fulfils the business requirements it needs to, as expressed in English via conversations with stakeholders. It's best used when you have humans you can talk to about requirements as part of a BDD process.
  3. Codeception addresses a similar 'acceptance' level as Behat, but the scenarios are PHP so is easier for a dev to write, but harder for stakeholders to validate requirements (you have to have a dev read them out). It's a good tool to use if you want to write lots of acceptance tests quickly.
  4. Pho is a tool I've not really used, but it addresses the same sort of testing PhpSpec does. Its syntax is more similar to tools in the Javascript world, so might be of some use

There are probably some tools I've forgotten, the main thing to do is fine ones that work well for you and make sure you're using them in a way that adds value for your project

21 likes
CiaranMcNulty's avatar

BTW on most projects I use PhpSpec + Behat, then PHPUnit for any other test I want to write (e.g. integration tests)

7 likes
SteveAzz's avatar

OH wow guys! Thank you so much for your explanations, very informative I take each comment and keep working on it, Thank You once again!

1 like

Please or to participate in this conversation.