简体   繁体   English

在拆解之前为每个测试调用QUnit setup()

[英]QUnit setup() called for each test before teardown

I'm using QUnit in conjunction with require.js for unit-testing a backbone.js application. 我将QUnitrequire.js结合使用以对Beta.2应用程序进行单元测试。 All of the tests are asynchronous, using the asyncTest method. 使用asyncTest方法,所有测试都是异步的。

I'm using setup and teardown to build and remove a fixture for each test. 我正在使用setupteardown构建和删除每个测试的夹具。 My problem is that although asyncTest seems to be blocking, setup() gets called for each test before they start, pretty much all setup() calls run at the same time. 我的问题是,尽管asyncTest似乎处于阻塞状态,但每个测试在启动之前都会调用setup() ,但是几乎所有setup()调用都同时运行。 This solution doesn't seem to fix my problem. 此解决方案似乎无法解决我的问题。 Below I have an example of how I set up the module and here's a link to a test repository that illustrates the problem 下面是一个有关如何设置模块的示例,这是一个说明问题测试库的链接

My question is: Am I doing something wrong or is this QUnit's normal behaviour? 我的问题是:我做错什么了吗?还是这个QUnit的正常行为?

module('Module', {
    setup: function(){
        console.log('setup');
    },
    teardown: function(){
        console.log('teardown');
    }
})

asyncTest('Test 1', function() {
    setTimeout(function(){
        equal(2, 2, 'The return should be 2.');
        start();
    }, 400);
});

asyncTest('Test 2', function() {
    setTimeout(function(){
        equal(1, 1, 'The return should be 1.');
        start();
    }, 400);
});

QUnit expects a rather specific page load behaviour. QUnit期望一个相当特定的页面加载行为。 Loading it via requirejs might look like it works, but actually doesn't. 通过requirejs加载它看起来像可行,但实际上不可行。 The recommendation is to load QUnit via a regular script tag. 建议通过常规脚本标记加载QUnit。 Loading the tests itself via requirejs is fine, as long as you set QUnit.config.autostart = false , as you do. 只要您设置QUnit.config.autostart = false ,就可以通过requirejs加载测试本身就可以了。

Blogpost including the solution Jörn Zaefferer specifies above - http://www.jonnyreeves.co.uk/2012/qunit-and-requirejs/ 博客帖子包括溶液JORN Zaefferer指定上述 - http://www.jonnyreeves.co.uk/2012/qunit-and-requirejs/

here's what I implemented- 这是我实现的

Problem I was having: 我遇到的问题:

I included qunit via require. 我通过要求包括了qunit。 This seemed to work fine for all synchronous tests. 这对于所有同步测试似乎都很好。 I was defining a variable in my qunit module's setup and back to null on teardown. 我在qunit模块的设置中定义了一个变量,并在拆卸时恢复为null。 When I included an asyncTest, it seemed to not be reset properly by the teardown and was thus breaking all the tests that ran after that one that expected a fresh variable. 当我包含asyncTest时,它似乎无法通过拆卸正确重置,因此破坏了所有在此之后期望进行新测试的测试。

Key points 关键点

  • Include qunit via its own script tag 通过自己的脚本标签包含qunit
  • set autostart to false 将自动启动设置为false
  • start again after all test modules are required 需要所有测试模块后,重新开始

It's probably not the most elegant, but I also didn't run across any end to end examples, and I just burned a couple hours finding this answer (since I first thought that I was setting my asyncTest up incorrectly). 它可能不是最优雅的,但是我也没有碰到任何端到端的示例,而且我花了几个小时才找到这个答案(因为我首先以为我设置了asyncTest不正确)。

<link rel="stylesheet" href="qunit-1.12.0.css">
<script type="text/javascript" src="qunit-1.12.0.js"></script>
<script>
    QUnit.config.autostart = false
    require(['App'],
    function(){

        //Modules that have been loaded in by require.js
        var loadedModules = [];

        //Locations of test modules to load
        var moduleLocations = [
            'tests/test1',
            'tests/test2',
            'tests/test3'
        ];

        //If all modules have been loaded, run them
        function runTests(){
            if (loadedModules.length == moduleLocations.length){
                QUnit.start();
                _.each(loadedModules,function(test){
                    test();
                });
            }
        }

        _.each(moduleLocations,function(string){
            require([string],
            function(a){
                loadedModules.push(a);
                runTests();
            });
        });

    });
</script>

Separate Test Module Files: 单独的测试模块文件:

define(['array','of','test','requirements'], function() {

    return function(){

        var options = null;

        module('Autocomplete',{
            setup: function() {
                // prepare something for all following tests
                options = new whatever();
            },
            teardown: function() {
                // clean up after each test
                options = null;
            }
        });

        test( "normal tests", function() {
            ok(true,"normal assertions");
        });
    }
});

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

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