gpt4 book ai didi

javascript - Mocha : create and finalize test programmatically

转载 作者:IT老高 更新时间:2023-10-28 23:03:38 31 4
gpt4 key购买 nike

我想实时代理在其他环境中运行的测试结果。

下面是一些我想要实现的伪代码:

  var test = proxy.getCurrentTest(); 
// => {slow: 200, timeout: 2000, duration: 235, result: 'error'};

var tmpIt = it('test1', function(){
this.slow(test.slow);
this.timeout(test.timeout);
});
tmpIt.close({
duration: test.duration,
result: test.result
});
// this should make this test red in the output,
// because the `result` is not 'success'

是否有可能在不“真正”运行它的情况下设置测试的结果和持续时间?并将所有视觉 mocha 输出到终端?

编辑:这个问题不是关于如何将带有测试结果的变量从子进程传递到主进程。它已经为我工作了。

最佳答案

希望我正确理解了这些要求。我实现的是一个集成到 mocha 中的 mocha 测试结果转发器。

为了与 mocha 集成,此实现描述了 custom mocha interface代理在另一个环境中执行的测试的测试结果。

要使用此接口(interface),必须在运行 mocha 时将 -u 参数传递给 mocha

> mocha -u ./path/to/proxy-interface ...

请注意,./path/to/proxy-interface 是 mocha 在 require 调用中使用接口(interface)模块的路径。

代理接口(interface)负责将 proxyTest 函数暴露给全局上下文,如 mocha 的 BDD 接口(interface)对 it 所做的那样,调用传递的函数以获取测试结果并转发测试结果,同时保留测试运行器显示的执行测试数量。

var Mocha = require('mocha');

var Suite = Mocha.Suite;
var Test = Mocha.Test;
var escapeRe = require('escape-string-regexp');

module.exports = function(suite) {
var suites = [suite];

suite.on('pre-require', function(context, file, mocha) {
// A bit hacky since we require mocha internal common interface module
var common = require('mocha/lib/interfaces/common')(suites, context);

context.run = mocha.options.delay && common.runWithSuite(suite);

context.proxyTest = function(title, fn) {
var suite = suites[0];
if (suite.pending) {
fn = null;
}
var test = new ProxyTest(title, fn);
test.file = file;
suite.addTest(test);
return test;
};
});
};

var Runnable = Mocha.Runnable;
var inherits = require('util').inherits;

function ProxyTest(title, fn) {
Runnable.call(this, title, null);
this.pending = !fn;
this.type = 'test';
this.body = (fn || '').toString();

this.fn = fn;
}

inherits(ProxyTest, Runnable);

ProxyTest.prototype.run = function(done) {
var proxiedTestResult = this.fn();

this.duration = proxiedTestResult.duration;
this.timedOut = this.timeout() > proxiedTestResult.timeout;

done(proxiedTestResult.result);
};

ProxyTest.prototype.clone = function() {
var test = new ProxyTest(this.title, this.fn);
test.timeout(this.timeout());
test.slow(this.slow());
test.enableTimeouts(this.enableTimeouts());
test.retries(this.retries());
test.currentRetry(this.currentRetry());
test.globals(this.globals());
test.parent = this.parent;
test.file = this.file;
test.ctx = this.ctx;
return test;
};

上面的代码覆盖了 Mocha 的 Runnable 运行实现,并运行传递的函数来获取测试结果,并在 ProxyTest 接口(interface)中设置必填字段以兼容 mocha 测试。

用法

在你的测试中使用 proxyTest 全局来注册一个新的 mocha 测试

var proxy = {
getErrorTestResult() {
return {slow: 200, timeout: 2000, duration: 50, result: 'error'};
},

getTimeoutTestResult() {
return {slow: 200, timeout: 2000, duration: 3000 };
},

getSlowTestResult() {
return {slow: 200, timeout: 2000, duration: 235 };
},

getSuccessTestResult() {
return {slow: 200, timeout: 2000, duration: 50 };
}
}

proxyTest('error', proxy.getErrorTestResult);
proxyTest('timeout', proxy.getTimeoutTestResult);
proxyTest('slow', proxy.getSlowTestResult);
proxyTest('success', proxy.getSuccessTestResult);

输出

Mocha Output

含义

这种方法的缺点是必须将自定义接口(interface)传递给 mocha AND,您不能使用 mocha BDD 词汇,如 describe。如果您“扩展”(在这种情况下:复制一些代码)mocha 的 BDD 接口(interface),则可以消除第二个缺点:

var Mocha = require('mocha');
/**
* Module dependencies.
*/

var Suite = Mocha.Suite;
var Test = Mocha.Test;
var escapeRe = require('escape-string-regexp');

/**
* BDD-style interface - extended with proxy functionality:
*
* describe('Array', function() {
* describe('#indexOf()', function() {
* it('should return -1 when not present', function() {
* // ...
* });
*
* it('should return the index when present', function() {
* // ...
* });
* });
* });
*
* @param {Suite} suite Root suite.
*/
module.exports = function(suite) {
var suites = [suite];

suite.on('pre-require', function(context, file, mocha) {
// A bit hacky since we require mocha internal common interface module
var common = require('mocha/lib/interfaces/common')(suites, context);

context.before = common.before;
context.after = common.after;
context.beforeEach = common.beforeEach;
context.afterEach = common.afterEach;
context.run = mocha.options.delay && common.runWithSuite(suite);
/**
* Describe a "suite" with the given `title`
* and callback `fn` containing nested suites
* and/or tests.
*/

context.describe = context.context = function(title, fn) {
var suite = Suite.create(suites[0], title);
suite.file = file;
suites.unshift(suite);
fn.call(suite);
suites.shift();
return suite;
};

/**
* Pending describe.
*/

context.xdescribe = context.xcontext = context.describe.skip = function(title, fn) {
var suite = Suite.create(suites[0], title);
suite.pending = true;
suites.unshift(suite);
fn.call(suite);
suites.shift();
};

/**
* Exclusive suite.
*/

context.describe.only = function(title, fn) {
var suite = context.describe(title, fn);
mocha.grep(suite.fullTitle());
return suite;
};

/**
* Describe a specification or test-case
* with the given `title` and callback `fn`
* acting as a thunk.
*/

var it = context.it = context.specify = function(title, fn) {
var suite = suites[0];
if (suite.pending) {
fn = null;
}
var test = new Test(title, fn);
test.file = file;
suite.addTest(test);
return test;
};

/**
* Exclusive test-case.
*/

context.it.only = function(title, fn) {
var test = it(title, fn);
var reString = '^' + escapeRe(test.fullTitle()) + '$';
mocha.grep(new RegExp(reString));
return test;
};

/**
* Pending test case.
*/

context.xit = context.xspecify = context.it.skip = function(title) {
context.it(title);
};

/**
* Number of attempts to retry.
*/
context.it.retries = function(n) {
context.retries(n);
};

context.proxyTest = function(title, fn) {
var suite = suites[0];
if (suite.pending) {
fn = null;
}
var test = new ProxyTest(title, fn);
test.file = file;
suite.addTest(test);
return test;
};
});
};

var Runnable = Mocha.Runnable;
var inherits = require('util').inherits;

function ProxyTest(title, fn) {
Runnable.call(this, title, null);
this.pending = !fn;
this.type = 'test';
this.body = (fn || '').toString();

this.fn = fn;
}

inherits(ProxyTest, Runnable);

ProxyTest.prototype.run = function(done) {
var proxiedTestResult = this.fn();

this.duration = proxiedTestResult.duration;
this.timedOut = this.timeout() > proxiedTestResult.timeout;

done(proxiedTestResult.result);
};

ProxyTest.prototype.clone = function() {
var test = new ProxyTest(this.title, this.fn);
test.timeout(this.timeout());
test.slow(this.slow());
test.enableTimeouts(this.enableTimeouts());
test.retries(this.retries());
test.currentRetry(this.currentRetry());
test.globals(this.globals());
test.parent = this.parent;
test.file = this.file;
test.ctx = this.ctx;
return test;
};

关于javascript - Mocha : create and finalize test programmatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34873556/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com