Cucumber入门级测试用例详细分析
上一篇 /
下一篇 2014-07-23 22:27:29
/ 个人分类:网页测试
根据网络上的各位大神给出来的各种例子,看起来各种简单,但是对于小白级的我来说,也废了很多力气,遇到了一些小白无法解决的问题,特此分享到这里,希望其他小白看到了会得到帮助。
一.Cucumber的第一个case:两数相加
1.没错,我们首先要建立文件夹,为了让自己的case跑起来的时候不会遇到其他的文件路径的问题,最好是按照最原始的建立比较有规定的文件夹树,特描述如下:
First_case
|--Features
|--Step_definitions
| |--add.rb
| |--adding.rb
|--adding.feature
按照上述来建立好文件夹。
2.对应的各类文件编写步骤以及代码
add.rb:我这里是描述一个类,描述调用的这个方法,代码如下:
class Add
def adding str1, str2
str1+str2
end
end
adding.feature:是对于要测试的case的描述,这里要测试的是简单的字符串相加。步骤如下:
Feature: To test two string add result
Scenario: Inorder to test two string adding function
Given input the first string "Hello,summer."
And input another string "I'm nick!"
When I press add button
Then the screen will put "Hello,summer.I'm nick!"
adding.rb:这个就是来实现你所要执行的case所写的代码了:
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))#一般进行调试的时候,总是会有加载不到文件或者找不到方法的错误,加上这个命令,就可以减少这个错误了,我当时就是找这个错误找了足足两天,后来高手给我指点了,才知道!同学们要谨慎
require 'cucumber'#下面的Given,and...这些,都是包含在cucumber里面的,所以需要包含进来。
require 'add.rb' #简单的调用,所以要包含进来
require 'rspec' #最后一步所用的.should,需要包含这个文件夹
Given(/^input the first string "(.*?)"$/) do |str1|
@str1=str1
end
And(/^input another string "(.*?)"$/) do |str2|
@str2=str2
end
When(/^I press add button$/) do
@adder=Add.new
@result=@adder.adding(@str1,@str2)
end
Then(/^the screen will put "(.*?)"$/) do |str3|
@result.should==str3
end
3.万事具备,只欠东风了!点击运行adding.feautrue,接着就会报错,没有给Given定义,但是其实我们已经调用了cucumber文件夹,这里的错误就是在于,我们的路径有错!我们找到调试框去选择‘Edit configrations', 把Working directory的路径改为你目前所要测试case的顶层,比如说我们这里的顶层路径就是First_case。
4.最后你的case就会一片大绿, 就会过了,哈哈哈。如果还遇到别的问题,可以在下面留言,我会每天晚上回来看到,就会回复哟~
祝大家能够天天向上!
也许还是有点乱,这里把完整的代码附上:add.rb 中的代码:
class Add
def adding str1, str2
str1+str2
end
end
adding.feature 中的步骤:
Feature: To test two string add result
Scenario: Inorder to test two string adding function
Given input the first string "Hello,summer."
And input another string "I'm nick!"
When I press add button
Then the screen will put "Hello,summer.I'm nick!"
adding.rb中的代码:
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))require 'cucumber'。require 'add.rb'require 'rspec' Given(/^input the first string "(.*?)"$/) do |str1| @str1=str1 endAnd(/^input another string "(.*?)"$/) do |str2|@str2=str2 endWhen(/^I press add button$/) do@adder=Add.new@result=@adder.adding(@str1,@str2)endThen(/^the screen will put "(.*?)"$/) do |str3| @result.should==str3 end
如此一来,再小白应该也是可以搞定的了。
收藏
举报
TAG: