2016-03-28 75 views
0

我想通過一些選項循環,並根據這些選項在摩卡生成測試。我已經建立了一個簡單的動態測試概念證明,基於這個要點:https://gist.github.com/cybertk/fff8992e12a7655157ed在摩卡動態構建測試 - TypeError:test.retries不是函數

當我運行dynamicSuite.addTest()時,我總是收到錯誤:「TypeError:test.retries不是函數」 。我無法弄清楚是什麼導致了錯誤。似乎沒有太多關於摩卡建設測試方法的文檔。

下面的代碼:

var dynamicSuite = describe('dynamic suite', function() { 
this.timeout(10000); 

before(function (done) { 
    var a = ['a', 'b', 'c']; 
    for(let item of a){ 
     dynamicSuite.addTest(new common.Mocha.Test('test' + item, function(done){ 
     done(); 
     })); 
    } 
    done(); 
}); 

it('this is needed to make sure tests run', function (done) { 
    done(); 
}); 

after(function(done) { 
    done(); 
}); 
});//end describe test block 

回答

1

測試可讀性是很重要的。考慮編程式生成測試是否會影響測試的可讀性和/或增加測試包含錯誤的機率。

那說,這應該工作:

describe('dynamic suite', function(){ 
    ['a','b','c'].forEach(function(letter, index){ 
     it('should make a test for letter' + letter, function(done){ 
     // do something with letter 
     done(); 
     }); 
    }); 
    }); 

您目前在beforeEach塊,將文件中的每個測試執行一次加入測試。所以如果你在文件中添加了另一個測試,所有的測試都會得到重複。

上面的代碼工作是因爲聲明一個測試只是執行一個函數(it(name, test)),所以我們可以迭代每個輸入並執行it函數。

+0

謝謝。我猜人們不會使用TDD風格? – mags