使用QUnit的非阻塞异步测试 [英] Non-blocking asynchronous tests using QUnit

查看:101
本文介绍了使用QUnit的非阻塞异步测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎QUnit函数 stop() start()允许等待用于异步测试,但在等待期间,整个测试套件都会挂起。有没有办法使用QUnit以非阻塞方式运行异步测试?

解决方案

查看 asyncTest 停止,有两个原因,我可以看到它的设置就是这样。


  1. 所以你不是意外地一次运行两个可能与某些东西冲突的测试(即修改DOM,从而改变彼此的测试结果)。

  2. 这样QUnit知道测试何时完成。如果它结束了所有的同步测试,那么它会写出结果,如果在后台仍然发生异步测试,你真的不想这样做。

所以这些都是好事,你可能实际上并不希望异步测试在运行时不会阻塞。您可以通过在异步测试开始后立即调用 start 来实现,但请记住,JavaScript实际上是单线程的(即使它有时会出现多线程) ),这可能会导致意外的结果,因为你不能保证你的异步测试何时会继续运行...它可能不会(可能不会)在其他测试完成并且结果发布之后。 / p>

It seems that the QUnit functions stop() and start() allow to wait for asynchronous tests, but during that waiting period the whole test suite hangs. Is there a way to run asynchronous tests in a non-blocking fashion using QUnit?

解决方案

Looking at the docs for asyncTest and stop, there's two reason's I can see that it's set up like that.

  1. So that you aren't accidentally running two tests at a time which might conflict with something (ie, modifying the DOM and so changing each others' test results).
  2. So that QUnit knows when the tests have finished. If it comes to the end of all the synchronous tests, then it'll write up the results, which you don't really want it to do if there are still async tests happening in the background.

So these are a good thing, and you probably don't actually want the async tests to not block as they run. You could probably do it by calling start immediately after the start of your async tests, but remember, JavaScript is actually single threaded (even though it sometimes gives the appearance of multi-threading), so this might cause unexpected results, as you can't guarantee when your async test will continue running... it might not (probably won't) be until after the other tests have finished and the results have been published.

这篇关于使用QUnit的非阻塞异步测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆