【问题标题】:Does after block still excecute if before or it block fail in mocha test如果之前或它阻止在摩卡测试中失败,之后块是否仍然执行
【发布时间】:2021-12-26 21:50:46
【问题描述】:

如果摩卡测试看起来像这样

describe("create a user") {
    before("login to system") {}
    it("execute assertion") {}
    after("delete the user") {}
}

如果 before 或 it block 失败,after block 是否仍然执行?

【问题讨论】:

    标签: javascript testing automation mocha.js


    【解决方案1】:

    是的,当测试失败时,它将运行after hook。

    这段代码:

    before("login to system", () => {
      console.log("BEFORE")
      throw new Error('error');
    })
    
    it('testing...', (done) => {
      console.log("IT")
      done();
    });
    
    after("delete the user", () => {
      console.log("AFTER")
    })
    

    输出这个:

    BEFORE
        1) "before all" hook: login to system for "testing..."
    AFTER
    
    
      0 passing (14ms)
      1 failing
    
      1) States Get
           "before all" hook: login to system for "testing...":
         Error: error
          at Context.<anonymous> (test\index.js:22:11)
          at processImmediate (node:internal/timers:463:21)
    
    

    你也可以查看这个 github 讨论:#1612 其中here 说:

    请看下面的例子,说明在beforeEach失败后正确调用了after

    代码

    // example.js
    before(function() {
      console.log('before');
    });
    
    after(function() {
      console.log('after');
    });
    
    describe('suite', function() {
      beforeEach(function() {
        throw new Error('In beforeEach');
      });
    
      afterEach(function() {
        console.log('afterEach');
      });
    
      it('test', function() {
        // example
      });
    });
    

    还有输出。

    // Test run
    $ mocha example.js
    
      before
    
      ․afterEach
    after
    
    
      0 passing (7ms)
      1 failing
    
      1) suite "before each" hook:
         Error: In beforeEach
          at Context.<anonymous> (/Users/dstjules/git/mocha/example.js:11:11)
          at callFn (/usr/local/lib/node_modules/mocha/lib/runnable.js:251:21)
          at Hook.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:244:7)
          at next (/usr/local/lib/node_modules/mocha/lib/runner.js:259:10)
          at Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)
          at processImmediate [as _immediateCallback] (timers.js:358:17)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-11
      • 2017-03-28
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多