ccy
ccy

Reputation: 1415

retry failed mocha test

I have some requirement that demands to retry mocha failure tests multiple times. Is there any easy way/workaround to do this?

I tried https://github.com/giggio/mocha-retry, but it doesn't seem to work for me with Mocha 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

Upvotes: 13

Views: 13124

Answers (3)

Devdutta Goyal
Devdutta Goyal

Reputation: 1135

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

If your test case fail than it will re-execute the same test case again until the max no of retries reached or test case get passed. Once your test case get passed it will jump to the next test case.

Upvotes: 12

gawi
gawi

Reputation: 2962

There is possibility to ask mocha to retry failed tests in the console:

mocha test/retry.js -g 'sample test' --retries 2

Upvotes: 11

laggingreflex
laggingreflex

Reputation: 34667

try{}catch and recursion

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 will help prevent bubbling up the error until you want it to and so allow you to recursively keep trying.

Upvotes: -2

Related Questions