Cucumber 从入门到放弃
- 建立一个存放project的文件夹: mkdir my-first-project ,个人建议先建立一个cucumber-test文件夹,之后的project都在这个文件夹下
- 在my-first-project目录下,初始化当前project: cucumber –init
- 在my-first-project目录下,执行cucumber
执行结果:
0 scenarios
0 steps
0m0.000s
在features目录下,创建feature文件
Feature: Adding Scenario: Add two numbers Given the input “2+2” When the calculator is run Then the output should be “4”
- 在my-first-project目录下,执行cucumber
执行结果:
Feature: AddingScenario: Add two numbers # features/adding.feature:2
Given the input “2+2” # features/adding.feature:3
When the calculator is run # features/adding.feature:4
Then the output should be “4” # features/adding.feature:51 scenario (1 undefined) 3 steps (3 undefined) 0m0.013s
You can implement step definitions for undefined steps with these
snippets:Given(“the input {string}”) do |string| pending # Write code here
that turns the phrase above into concrete actions endWhen(“the calculator is run”) do pending # Write code here that
turns the phrase above into concrete actions endThen(“the output should be {string}”) do |string| pending # Write
code here that turns the phrase above into concrete actions end
运行cucumber命令时,会扫描features中的文件,每个feature中包含多个Scenario, Scenario中的step会调用step definitions中的编写的测试代码,以上错误,就是因为step definitions中没有feature文件对应的定义代码
- 在step_definitions目录下创建一个ruby文件,内容如下:
Given(/^the input “([^”]*)”$/) do |input|
@input = input
endWhen(/^the calculator is run$/) do
@output =ruby features/support/calc.rb #{@input}
raise(‘Command failed’) unless $?.success?
endThen(/^the output should be “([^”]*)”$/) do |expected_output|
puts @expected_output
未完待续。。。