首页 > 解决方案 > 如何在量角器中的套件之间共享相同的测试用例

问题描述

我有一些测试用例可以在测试套件之间共享

假设套件 x 和套件 y 共享同一组测试用例(它起作用)。

我制作了一个单独的 .js 文件,其中包含看起来像这样的共享代码。

module.exports = function(a,b){
//...
test cases..
//....
}

我正在尝试在 x 和 y 中使用这个模块

这就是 x 的样子

var common = require('./module');

describe("description", module(a,b);

可以这样做吗?还有其他方法吗?

我的代码中常见的js看起来像

module.exports = function(a,b) {

beforeAll(function(){
//some code
}
afterAll(function(){
//some code
}

It(‘ads’, function(){
code
}

it(‘ads’, function(){
code
}

it(‘ads’, function(){
code
}


}

我想在另外两个套件中将其用作带有可传递参数的 describe 函数的函数参数。

套房1

var common = ('./common');
describe('this is a test case', common(a,b);

这可能吗?

标签: javascriptangularseleniumjasmineprotractor

解决方案


如果您的 common.js 文件类似于...

module.exports = function(a,b){
//...
test cases..
//....
}

还有你的 test.js 文件:

var common = require('./common'); // <-- note the change

describe("description", common); // <-- you were calling module*

这是假设您的 common.js 导出函数是格式正确的描述函数。

您还可以导出单个测试用例,例如 (other.js)...

module.exports = {
    testOne: function(something) { return false; },
    testTwo: function(whatever) { return true; }
}

而你的测试...

var other = require('./other');

describe("description", function() {
    it('should pass', function() {
        expect(other.testOne()).toEqual(false);
    });
});

推荐阅读