2016-10-04 49 views
1

動態測試我有,所以我想使用循環 我會嘗試像測試的動態量:鼻子產生與裝飾

from nose.tools import istest, nottest 
from nose.tools import eq_ 
import nose 
nose.run() 
@istest 
def test_1(): 
    for i in range(100): 
     @istest 
     def test_1_1(): 
      eq_(randint(1,1),1) 



--------------------- 
Ran 1 test in 0.001s 

OK 

但鼻子顯示它像只有一個測試。我如何將它改進到100次測試? 在此先感謝。

回答

2

對於鼻子中的數據驅動測試,請查看nose_parameterized

實例:

from nose_parameterized import parameterized 

@parameterized.expand([(1, 1, 2), (2, 2, 4)]) 
def test_add(self, a, b, sum): 
    self.assertEqual(sum, a + b) 

在這裏,兩個測試將通過澆道產生的。它測試1+1==22+2==4。裝飾者也與其他測試跑步者兼容,如unittest

+0

謝謝! 但對於我來說,仍然不清楚如何使用「for」循環。 –

+0

因爲你不用for循環 - 這不是接口。讓裝飾者爲你做循環,它更好。 – wim