【问题标题】:Can you run a test within a test using the Intern?您可以使用实习生在测试中运行测试吗?
【发布时间】:2015-12-02 23:56:25
【问题描述】:

为了证明我的问题,这里对我正在尝试做的事情进行了过度简化:

我有两个功能测试:loginlogout。我创建了一个 UI 以允许用户(阅读:其他开发人员)在给定的浏览器上选择和运行给定的测试。

很明显,如果 login 没有先运行,logout 将会失败。为了解决这个问题,我在login 测试中设置了一个全局变量loggedIn,并将以下设置函数添加到我的logout 测试中:

setup: function() {
    if (!globals.get('loggedIn')) {
        return require('./login')();
    }
},

require 语句失败,并带有以下错误消息:

SUITE ERROR
Error: Attempt to require unloaded module tests/functional/login

我怎样才能让我的设置函数运行login 测试,而不将测试复制粘贴到我的每个测试文件中?


编辑

完整的测试套件:

login.js

define([
    'intern!object',
    'intern/chai!assert',
    '../../var/globals'
], function(registerSuite, assert, globals) {
    registerSuite({
        name: 'logout',
        'logout': function() {
            return this.remote
                .get('www.example.com/login')
                .findById('status')
                    .getVisibleText()
                .then(function(text) {
                    var loggedIn = text === 'logged-in';
                    if (loggedIn) globals.set('loggedIn', true);
                    assert.equal(loggedIn, true);
                });
        }
    });
});

注销.js

define([
    'intern!object',
    'intern/chai!assert',
    'require',
    '../../var/globals'
], function(registerSuite, assert, require, globals) {
    registerSuite({
        name: 'logout',
        setup: function() {
            if (!globals.get('loggedIn')) {
                return require('./login')();
            }
        },
        'logout': function() {
            return this.remote
                .get('www.example.com/logout')
                .findById('status')
                    .getVisibleText()
                .then(function(text) {
                    assert.equal(text, 'logged-out');
                });
        }
    });
});

all.js

define([
    './login.js',
    './logout.js'
    // other tests that also depend on login.js
], function() {});

我想运行logout 测试以运行login,并运行all 测试以运行login 测试一次,不管有多少其他测试依赖于@987654339 @。

【问题讨论】:

  • 请提供整个模块的代码,包括define 包装器。您可能正在使用 2 参数 define 并且未在依赖项列表中包含 ./login 模块,但无法使用您提供的信息来验证这一点。
  • @CSnover 这确实是我正在做的事情。我已经编辑了我的问题以显示完整的模块。我假设将 ./login 模块添加到我的依赖项列表中每次都会运行它,无论它是否已经运行?
  • 没关系;好像我可以将./login 模块粘贴在define 的依赖数组中,就像你说的那样。我什至不需要使用require 或设置功能。

标签: javascript functional-testing intern


【解决方案1】:

编辑:问题是由require 同步引起的。使用 define 中使用的依赖数组加载模块只会运行一次测试。这意味着您可以将 any 测试添加为依赖项,而无需更改任何测试组(例如我的 all 测试)。

这会将我的模块更改为以下内容:

login.js

define([
    'intern!object',
    'intern/chai!assert',
    // globals var removed
], function(registerSuite, assert) {
    registerSuite({
        name: 'logout',
        'logout': function() {
            return this.remote
                .get('www.example.com/login')
                .findById('status')
                    .getVisibleText()
                .then(function(text) {
                    assert.equal(text, 'logged-in');
                });
        }
    });
});

注销.js

define([
    'intern!object',
    'intern/chai!assert',
    // require removed
    // globals var removed
    './login' // login module added
], function(registerSuite, assert) {
    registerSuite({
        name: 'logout',
        // setup method removed
        'logout': function() {
            return this.remote
                .get('www.example.com/logout')
                .findById('status')
                    .getVisibleText()
                .then(function(text) {
                    assert.equal(text, 'logged-out');
                });
        }
    });
});

all.js(保持不变)

define([
    './login.js',
    './logout.js'
    // other tests that also depend on login.js
], function() {});

【讨论】:

  • 您可以使用require 来进行测试,但不能以您尝试的方式进行。 AMD require 是异步的,因此要在设置函数中使用它来加载以前卸载的模块,您需要将 require 包装在一个 Promise 中,该 Promise 在 require 完成加载模块后解析。
  • 感谢您的解释。更新了我的答案以反映这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 2015-12-10
  • 2020-08-29
相关资源
最近更新 更多