2014-10-16 183 views
6

我有一些要求多次重試摩卡失敗測試。有沒有簡單的方法/解決方法來做到這一點?重試失敗摩卡測試

我試過https://github.com/giggio/mocha-retry,但它似乎不爲我用摩卡1.21.3工作:

it (2, 'sample test', function(done) { 
     expect(1).to.equal(2); 
     done(); 
    }); 

mocha test/retry.js -g 'sample test' --ui mocha-retry

+1

這有[XY問題]的氣味(http://meta.stackexchange.com/a/66378/241526)。爲什麼你需要重試你的測試? (好吧,有一個「需求需求」,但爲什麼要求?)即使是摩卡重試的文檔(其中有很多錯別字;並不能激發人們的信心)有一段關於你的測試應該如何工作的段落一致。 – Louis 2014-10-16 22:09:50

+1

好吧,團隊希望首先關注一致性故障,通過減少由於我們面臨的不穩定但失控的環境設置造成的間歇性故障的噪音。 – ccy 2014-10-16 22:26:34

回答

0

try{}catch和遞歸

var tries_threshold = 5; 
it(2, 'sample test', function(done) { 
    var tries = 0; 
    function actual_test() { 
     expect(1).to.equal(2); 
    } 
    function test() { 
     try { 
      actual_test(); 
     } catch (err) { 
      if (err && tries++ < tries_threshold) 
       test(); 
      else done(err); 
     } 
    } 
    test(); 
}); 

try{}catch將有助於防止冒着錯誤直到你想要它,所以讓你遞歸地繼續嘗試。

4
it (2, 'sample test', function(done) { 
     this.retries(2); pass the maximum no of retries 
     expect(1).to.equal(2); 
     done(); 
    }); 

// this.retries(Max no of retries);

如果您的測試用例失敗,它將再次重新執行相同的測試用例,直到達到最大重試次數或測試用例通過。 一旦您的測試用例通過,它將跳轉到下一個測試用例。

+0

注意:重試現在是摩卡核心的一部分:http://mochajs.org/#retry-tests – redgeoff 2017-04-26 17:49:21