简体   繁体   English

自动运行失败了rspec测试

[英]Autorerun failed rspec tests

I have some code in my tests which works with some external service. 我的测试中有一些代码可用于某些外部服务。 This service is not very stable, so sometimes it crash for no reason. 这项服务不是很稳定,所以有时会无故崩溃。 But in 80% of runs it works well. 但在80%的运行中它运作良好。

So I want a method to automatically rerun all failed rspecs several time (for example 2 or 3 time). 所以我想要一种方法来自动重新运行所有失败的rspecs几次(例如2或3次)。 Is there any way to do it? 有什么办法吗?

Many people would say that your test actually should never hit external services, and that's one of the reasons to do it. 很多人会说你的测试实际上不应该打击外部服务,而这也是其中一个原因。 Your tests should not fail because some external service is down. 您的测试不应该失败,因为某些外部服务已关闭。

TL;DR use mocks and stubs or to replace those external service calls TL; DR使用模拟和存根或替换那些外部服务调用

Instead of re-running failed specs, couldn't you just run the method accessing the service a set number of times and run the expectation on the logical OR of the results? 您是不是只运行访问服务一定次数的方法而不是重新运行失败的规范,并对结果的逻辑OR运行期望?

So instead of: 所以代替:

it "returns expected value for some args" do
   unstable_external_service(<some args>).should == <expected return value>
end

just do something like this: 做这样的事情:

def run_x_times(times, args)
  return nil if times == 0
  unstable_external_service(args) || run_x_times(times-1)
end

it "returns expected value for some args" do
  run_x_times(10, <some args>).should == <expected return value>
end

You can use the same wrapper method throughout your tests anytime you access the service. 您可以在访问服务时随时在整个测试中使用相同的包装器方法。 I'm assuming here that your service returns nil on a failure, but if not you could change this to fit your particular case -- you get the general idea. 我假设您的服务在失败时返回nil ,但如果不是,您可以根据您的具体情况进行更改 - 您会得到一般的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM