夜鸣猪 发表于 2013-2-7 19:55:17

使用cucumber测试过程中de-bug

最近,Rails工程中,做测试和集成测试的时候,Cucumber很热.然而,就像任何测试的工具使用的时候一样,测试用例可能出现以外的失败.

这个时候,就需要一些技巧来调试和发现这些没有覆盖到的用例,以及失败的原因.
使用ruby-debug之前,需要配置文件.

在env.rb文件中,添加:
require 'ruby-debug'

然后,你就可以放置breakpoint标识在任何你需要调试的代码处.例如,在application或者其它,需要step调试的地方.

Then /^the process should exit cleanly$/ dobreakpointassert exited_cleanly</span><span class="p">,</span> <span class="s2">"Process did not exit cleanly: </span><span class="si">#{</span><span class="vi">stdout}"end

提示: 如果,你把breakpoint标识放到了最后,那么你需要添加一个语句标识在breakpoint后面,否则,ruby-debug将会进入调用阻塞.如下:
Then /^the process should exit cleanly$/ doassert exited_cleanly</span><span class="p">,</span> <span class="s2">"Process did not exit cleanly: </span><span class="si">#{</span><span class="vi">stdout}"breakpoint; 0end
那么进行调试:
如果有一个步骤类似“Then I debug”,那么会相当方便,这个文件应该放到
features/step_definitions/debug_steps.rb:

Then /^I debug$/ dobreakpoint0end


Webrat插件

Webrat是一个默认的工具用来debugcucumber测试.它的功能包括,点击连接,输入和提交表单等.例如,如果,你试图点击一个不存在的链接.你就会得到一对的html.


save_and_open_page

Webrat 提供一个save_and_open_page方法用来捕获当前的HTML保存并打开浏览器,十分强大.

When /^I follow "(.*)"$/ do |link|save_and_open_pageclick_link(link)end

在上面的情况下,即使没有失败,也会有很多页面被打开
When /^I follow "(.*)"$/ do |link|begin    click_link(link)rescue    save_and_open_page    raiseendend

页面只有在出现错误的时候,才被打开.
页: [1]
查看完整版本: 使用cucumber测试过程中de-bug