rspec:第一次失败后如何继续测试 [英] rspec: how can I continue test after first failure

查看:96
本文介绍了rspec:第一次失败后如何继续测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rspec对设备进行系统测试.该设备是模块化的,因此可以将任意数量的子设备连接到测试台.我想在许多地方编写测试,这些测试将在子设备的连接上循环并在每个设备上运行相同的测试.

I am using rspec to system test a device. The device is modular so that any number of sub-devices may be connected to a test rig. There are many places where I want to write tests that will loop over the connected of sub-devices and run the same test on each device.

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

Basically, this is what I am trying to do:

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)块运行之前对测试进行解析.

Unfortunately this does not work. I get errors saying @tool is nill and does not contain a class sub_devices before the tests even run. So the tests are being parsed before the before(:all) block runs.

使它起作用的一种方法是将循环放在it块内部.像这样:

One way I can make it work is by putting the loop inside of the it block. Like this:

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

这样做的问题在于,即使第一个子设备出现故障,我也确实要测试所有子设备.我想查看有关到底有多少个子设备发生故障的报告.一旦一个失败,此代码将立即中断,而其余的将不进行测试.

The problem with doing it this way is that I really want to test all the sub-devices even if the first one fails. I want to see a report of exactly how many sub devices had failures. This code will break out as soon as one fails and not test the rest.

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

I realize this is probably not a normal use case for rspec but it would be very convenient for our testing situation if I could make this work.

有什么建议吗?

推荐答案

以下是一些用于编写此代码的技术.

Here are some techniques for writing this.

最好避免使用before :all.最好也避免使对象超出示例范围.

It is best to avoid using before :all. It is also best to avoid making objects outside of examples.

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

这篇关于rspec:第一次失败后如何继续测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆