11

好吧,作为一个C#NUnit的家伙,这可能很奇怪。茉莉花参数化单元测试

但茉莉允许参数化单元测试吗?

我不确定它是否违背了“声明”和“它”使非程序员可读的东西。

我已经看到一些第三方插件,但他们有点老,不知道它是否已被添加到茉莉花。 如果我需要使用插件

只是为了帮助任何在未来发现此问题的人,我已被告知jasmine论坛Jasmine本身没有对参数化测试的一流支持。

+0

你可以发布你在Jasmine论坛上找到的答案,作为你自己问题的答案并接受它。 – TrueWill

回答

3

我没有因为长时间用茉莉工作,但它是很容易添加参数测试:

['abc', 3, 
    'ab', 4, 
    '', 0]. 
it('should contain string length', function(string, expected){ 
    expect(string.length).toBe(expected); 
}); 

与基础结构代码,只需几行:

Array.prototype.it = function(description, testCaseFunction) { 
    _(this) 
     .chunk(testCaseFunction.length) 
     .each(function(innerArray){ 
       it(description + ' ' + JSON.stringify(innerArray), function(){ 
        testCaseFunction.apply(this, innerArray); 
       });  
     }) 
     .value(); 
}; 

视您希望更改默认js对象的语法和意愿,您有很多选择:http://blog.piotrturski.net/2015/04/jasmine-parameterized-tests.html

12

基于piotrek's answer和文章Parameterized testing in Javascript,你也可以使用下面的方法,它使用ES6语法:

[ 
    ['abc', 3], 
    ['ab', 2], 
    ['', 0], 
].forEach(([string, expectedLength]) => { 
    it(`should return length ${expectedLength} for string "${string}"`,() => { 
    expect(string.length).toBe(expectedLength); 
    }); 
}); 

我已经用玩笑测试框架测试,但它应该与茉莉花正常工作。

+2

这确实与茉莉花一起工作。 – Shadow