如何用Jasmine编写参数化测试?

时间:2014-09-26 09:54:52

标签: javascript unit-testing jasmine

如何使用Jasmine单元测试框架(或任何其他其他BDD JavaScript测试框架)编写参数化测试?

2 个答案:

答案 0 :(得分:1)

AFAIK Jasmine没有参数化测试支持。所以我提出了这个简单的方法:

describe('Testing module', function() {
      var testCases = [
          { param1: 'testcase1Param1', param2: 'testCase1Param2'}, 
          { param1: 'testcase2Param1', param2: 'testCase2Param2'}, 
      ];

      /*jshint -W083 */ //Disable warning for function created inside loop
      //this is parametrized test and it's better readable this way.
      testCases.forEach(function(testCase) {
        describe('for test case: param1" ' + testCase.param1 +
          ' and param2: "' + testCase.param2 + '"', function() {
            //do your testing
        }
      }
});

答案 1 :(得分:0)

受这些启发,

我写了一些东西来获得这个(参数不是位置的):

describe('sum()', function() {        
    helper.executeTestCases({
        description: 'given values #param1 and #param2 should return #result',
        values: [
             { param1: 1, param2: 2, result: 3}
            ,{ param2: 5, result: 7, param1: 2}            
        ],
        test: function(param1, param2, result){
            expect( param1 + param2 /*your code here*/ ).toEqual(result);
        }        
    });
});

这是WIP,但它在这里:

var helper = (function Helper(){          

    var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
    var ARGUMENT_NAMES = /([^\s,]+)/g;
    function getParamNames(func) {
        var fnStr = func.toString().replace(STRIP_COMMENTS, '');
        var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
        return result || [];
    };

    /*
    given the string "this is a #one" an the values {one:"test"} it returns "this is a test"
    */
    function createDescription(description, values){  
        var placeHolders = description.match(/(#\w+)/g);
        if(!placeHolders) throw Error("Fail to recognize placeHolders in test description (" + description + ").");
        placeHolders.forEach(function(placeHolder, index){
            var newValue = values[placeHolder.substr(1)];  // remove the "#"
            description = description.replace(placeHolder, newValue);                    
        });    
        return description;
    };


    return {

        executeTestCases: function(params){
            params.values.forEach( function(element, index){
                var description = createDescription(params.description, element);                      

                var paramNames = getParamNames(params.test);
                var parameters = [];
                paramNames.forEach(function(name){
                    if(!element.hasOwnProperty(name)) throw new Error('Element "' + name +'" not found in row #' + (index+1) + '" of test case values."');
                    parameters.push(element[name]);                        
                });

                return it(description, function(){
                    params.test.apply(null, parameters);
                });                    
            });
        } 
    };

})();     

的Alessandro