简体   繁体   English

摩卡单元测试:超时后如何强制停止执行测试

[英]Mocha unit test: how to forcefully stop execution of test after timeout exceeded

I am running unit test using mocha, I want to forcefully timeout, if the function exceeds 30000, so I have set this.timeout(30000);我正在使用 mocha 运行单元测试,我想强制超时,如果 function 超过 30000,所以我设置了 this.timeout(30000);

to verify its working I put infinite loop inside test case, even timeout exceeds test doesn't return back.为了验证它的工作我在测试用例中放置了无限循环,即使超时超过测试也不会返回。 I want this test to stop executing after timeout.我希望这个测试在超时后停止执行。

Here is the test这是测试

    it('should forcefully timeout',function(done) {
    this.timeout(30000);
     while(1) {
        for(var i = 0; i < 10000; i++) {
            if( i % 2 == 0 ) {
                console.log("here");
                for(var i=0; i < 10000; i++) {
                    if( i % 2 == 0 ) {
                        console.log("here1");  
                    }
                }
            }
        }

      }
      done();         
  })


  1. async run those functions which may take long time. async 运行那些可能需要很长时间的功能。
  2. add exit flag, mocha will terminate process when test finished even async function still running.添加退出标志,当测试完成时,即使异步 function 仍在运行,mocha 也会终止进程。 ex: mocha \"test/**/*.js\" --exit例如: mocha \"test/**/*.js\" --exit

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

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