加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – rspec:如何在第一次失败后继续测试

发布时间:2020-12-17 02:05:16 所属栏目:百科 来源:网络整理
导读:我正在使用rspec对系统进行系统测试.该设备是模块化的,因此任何数量的子设备都可以连接到测试台.我想在很多地方编写测试,这些测试将遍历连接的子设备并在每个设备上运行相同的测试. 基本上,这就是我想要做的: before(:all) @tool = discover_sub_devices()
我正在使用rspec对系统进行系统测试.该设备是模块化的,因此任何数量的子设备都可以连接到测试台.我想在很多地方编写测试,这些测试将遍历连接的子设备并在每个设备上运行相同的测试.

基本上,这就是我想要做的:

before(:all)   
  @tool = discover_sub_devices()
  @tool.sub_devices.should == exp_sub_device_list
end

describe "some sub device tests" do
  @tool.sub_devices.each do |dev|   
    it "should succeed at doing things" do
      dev.do_thing.should == success   
    end 
  end
end

不幸的是,这不起作用.我得到错误,说@tool是nill,并且在测试运行之前不包含类sub_devices.因此,在before(:all)块运行之前解析测试.

我可以使其工作的一种方法是将循环放在it块内.像这样:

it "should succeed at doing things" do
  @tool.sub_devices.each do |dev| 
    dev.do_thing.should == success ??
  end
end

这样做的问题是我真的想测试所有子设备,即使第一个子设备出现故障.我想看一个确切的子设备有多少故障的报告.一旦失败,该代码就会爆发,而不会测试其余代码.

我意识到这可能不是rspec的正常用例,但是如果我可以做到这一点,它对于我们的测试情况会非常方便.

有什么建议?

解决方法

这是一些写这个的技巧.

最好避免以前使用:全部.最好避免在示例之外制作对象.

describe "some sub device tests" do

  let(:tool) { discover_sub_devices }

  it "matches the sub device list" do
    tool.sub_devices.should be == expected_sub_devices_list
  end

  it "succeeds with all sub-devices" do
    failures = tool.sub_devices.reject{|d| d.do_thing == success}

    # option 1
    failures.should be_empty # will show just a yes/no result

    # option 2
    failures.size.should be == 0 # will show the number of failures

    # option 3
    failures.should be == [] # will display all sub-devices which fail
  end

end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读