QUnit Async Tests with setup And teardown

时间秒杀一切 提交于 2019-12-30 10:27:09

问题


I need a little help understanding QUnit internas. I read its source from time to time, but i'm still writing weird test when it comes to asynchronous tests. I understand the concept of asynchronous tests, and the stop() and start() methods (and why they are needed), but when i combine them with setup and teardown i get a lot of weired situations.

Here is my Testcode:

use(['Psc.Exception','Psc.Code'], function () {
  module("async", {
    setup: function () {
      console.log('setup');
    }, teardown: function () {
      console.log('teardown');
    }
  });

  asyncTest("test1", function () {
    expect(0);

    console.log('test1');
    start();
  });

  asyncTest("test2", function () {
    expect(0);

     console.log('test2');
     start();
  });

  asyncTest("test3", function () {
    expect(0);

    console.log('test3');
    start();
  });

  asyncTest("test4", function () {
    expect(0);

    console.log('test4');
    start();
  });

  asyncTest("test5", function () {
    expect(0);

    console.log('test5');
    start();
  });
});

Allthough these are asynchron tests, i thought i would get something like this in the console:

setup
test1
teardown
setup
test2
teardown
setup
test3
teardown
...

because i thought qunit would call setup and teardown around the test bodys?

but I get everything mixed up, from request to request in another way shuffled.

setup
test1
teardown
setup
setup
setup
setup
test5
teardown 
test4
teardown
test3
teardown
test2
teardown

is someone able to explain it step by step?


回答1:


It was a documented issue:

http://api.qunitjs.com/QUnit.config/

its is recommended to set QUnit.config.autostart to false, when loading tests asynchronously. This is my case because "use" is doing it asynchronously.

The head looks like this:

QUnit.config.autostart = false;
use(['Psc.Exception','Psc.Code'], function () {

  QUnit.start();
  module("async", {

So its basically like doing stop() and start() but for loading the tests itself. I tested it and the teardown / setup / test now get correctly executed in the right order



来源:https://stackoverflow.com/questions/11036514/qunit-async-tests-with-setup-and-teardown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!