2015-11-05 66 views
1

我正在使用摩卡并尝试构建一个单独报告测试的测试系统。目标是在项目和单元测试的要求中定义的测试之间具有可追溯性。因此,例如,测试'必须能够创建新窗口小部件'位于ID为'43'的需求数据库中,我希望测试该标准的单元测试报告类似Test 43, Must be able to create new widgets, pass的内容,然后更新相应的数据库条目(另一项服务可能对此负责)。将测试ID添加到单元测试报告

这可以在摩卡完成吗?到目前为止,我发现的唯一一件事是用测试ID替换it()函数中的文本,然后使用json记者来处理结果(但之后我没有得到正在测试的文本,除非我结合他们并做一些解析)。注意:并非所有的测试都会有一个ID。

下面是我希望的

describe("Widget" function() { 
    it("should allow creation of widgets", function() { 
    this.id = 43; 
    result = widget.create(); 
    expect.result.to.exist; 
    }); 
}); 

,然后或者钩,像

afterEach(function(test) { 
    if (test.hasOwnProperty('id')) { 
    report(test.result); 
    } 
}); 

或自定义报告,或某些类型的适配器的那种功能的一个例子。

runner.on('test end', function(test) { 
    console.log(test.id); //doesn't exist, but i want it to 
    report(test); 
}); 

回答

0

我想要的东西和存在的东西如此接近!我能够使用报告中测试的ctx属性来解决这个问题,例如, test.ctx.id

test.js

describe("Widget" function() { 
    it("should allow creation of widgets", function() { 
    this.id = 43; 
    result = widget.create(); 
    expect.result.to.exist; 
    }); 
}); 

reporter.js

runner.on('test end', function(test) { 
    console.log(test.ctx.id); 
    report(test); 
}); 
0

这取决于你的断言库。 与Chai,你有文字的可选字段。

assert.should.exist(result, 'expect Result to exist (Id 43)'); 

随着Jasmine,你可以测试引用添加到它():

describe("Widget" function() { 
    it("should allow creation of widgets (Id 43)", function() { 

要使用Mocha custom reporters,你可以尝试定义一个在您的测试套件。

module.exports = MyReporter; 

function MyReporter(runner) { 
    var passes = 0; 
    var failures = 0; 

    runner.on('pass', function(test){ 
    passes++; 
    console.log('pass: %s', test.fullTitle()); 
    }); 

    runner.on('fail', function(test, err){ 
    failures++; 
    console.log('fail: %s -- error: %s', test.fullTitle(), err.message); 
    }); 

    runner.on('end', function(){ 
    console.log('end: %d/%d', passes, passes + failures); 
    process.exit(failures); 
    }); 
} 

这里真的有两点建议。第一个是最简单的,只是简单地将你的id添加到it()的描述中,然后显示你已经通过和失败的内容。这将是达到目标的最快途径。

但是,如果您想拥有更好的方法,并且可以测试以确保设置,那么您可以使用自定义记者,如果未设置ID,则可以让您通过测试。

+0

所以你的建议去解析路线? – rwilson04