简体   繁体   中英

Cucumber: How to write snippets/step definitions in PHP?

I am trying to get Cucumber working to test a PHP application. So far I am able to get the feature to run by simplying running cucumber in my features directory. At the bottom of the results, it says:

If you want snippets in a different programming language, just make sure a file with the appropriate file extension exists where cucumber looks for step definitions.

What does this mean exactly? I'm fine with writing most of my step definitions in Ruby, but at some point, I'm going to need to create some setup data (ideally creating it in PHP). How can I write step definitions in PHP as this statement suggests?

FYI: I am new to Ruby and Cucumber as well.


Solution: Cuke4PHP

Cuke4PHP implements the Cucumber wire protocol, making it possible to write step definitions in PHP.

看看Behat,一个PHP版的Cucumber: http//everzet.com/Behat/

If you didn't find a solution, this is what I found today! This guy spared me the work I had to do to use PHP lol

https://github.com/olbrich/cuke4php

He has implemented the wire protocol for PHP. This in a nutshell is another server that runs PHP steps when Cucumber finds any. Also passes back the results.

Ah no need to configure env.rb any more.

Step definitions are stored in features/step_definitions and are what tells Cucumber what to do when it encounters statements like "Given I have 3 cucumbers in my belly":

Given /^I have (\d+) cucumbers in my belly$/ do |cukes|
  # Some Ruby code here
end

http://github.com/aslakhellesoy/cucumber/wiki/Step-Definitions

To write your steps in PHP, just put them in features/step_definitions and configure your environment ( features/support/env.rb ) accordingly. There's good information in the form of a Cucumber feature for this in the Cucumber docs:

http://github.com/aslakhellesoy/cucumber/wiki/php

A quick Google search also brought up the following article on PHP testing with Cucumber, Webrat and Selenium which looks pretty useful:

http://dev.af83.com/testing/acceptance-tests-php-project-cucumber-webrat-selenium-trio/2010/06/03

Other languages that are not directly supported can use Cucumber's wire protocol, which is eg what Clucumber (Common Lisp) does:

http://github.com/aslakhellesoy/cucumber/wiki/wire-protocol

I don't do PHP, but I hope my answer still helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM