2016-11-28 55 views
-1

nosetest's API documentation,有一個LazySuite()類,這是nosetest的LazySuite應該如何使用?

class nose.suite.LazySuite(tests=()) 
A suite that may use a generator as its list of tests 

但是,我找不到這個測試發生器應該如何看起來像任何例子。我試着寫一個產生測試用例nosetest的測試發現,當人會使用,它傳遞給LazySuite構造方式如下:

import nose 
import nose.suite 

data = [ 
     ['a', 'b', 'c'], 
     ['1', '2', '3'] 
] 

def check_data_list(data_list): 
    assert data_list[0] == 'a' 


def test_generator(): 
    for data_list in data: 
     yield check_data_list, data_list 


if __name__ == '__main__': 

    suite = nose.suite.LazySuite(tests=test_generator()) 
    nose.run(suite=suite) 

然而,這失敗

Traceback (most recent call last): 
    File "test-with-nose.py", line 43, in <module> 
    nose.run(suite=suite) 
    File "/usr/local/lib/python3.5/dist-packages/nose/core.py", line 301, in run 
    return TestProgram(*arg, **kw).success 
    File "/usr/local/lib/python3.5/dist-packages/nose/core.py", line 121, in __init__ 
    **extra_args) 
    File "/usr/lib/python3.5/unittest/main.py", line 94, in __init__ 
    self.runTests() 
    File "/usr/local/lib/python3.5/dist-packages/nose/core.py", line 207, in runTests 
    result = self.testRunner.run(self.test) 
    File "/usr/local/lib/python3.5/dist-packages/nose/core.py", line 62, in run 
    test(result) 
    File "/usr/local/lib/python3.5/dist-packages/nose/suite.py", line 178, in __call__ 
    return self.run(*arg, **kw) 
    File "/usr/local/lib/python3.5/dist-packages/nose/suite.py", line 225, in run 
    test(orig) 
    File "/usr/lib/python3.5/unittest/suite.py", line 84, in __call__ 
    return self.run(*args, **kwds) 
    File "/usr/local/lib/python3.5/dist-packages/nose/suite.py", line 76, in run 
    test(result) 
TypeError: 'tuple' object is not callable 

什麼是用於創建應該看起來像LazySuite類的實例的生成器嗎?

回答

0

好吧,我不確定這是否是唯一正確的方法,因爲我沒有看到它正式記錄在任何地方,但它涉及the TestLoader class及其loadTestsFromGenerator()方法時正常工作。這個方法獲得了一個生成器函數,該函數產生了一個或多個我已經在問題中提到的元組(test_function, other_args, ...)

完整的示例(打印報表只對正在發生的事情更容易以下):

import nose 
import nose.suite 
import itertools 
from nose.loader import TestLoader 


data = [ 
     ['a', 'b', 'c'], 
     ['1', '2', '3'] 
] 

def first_check(row): 
    print('Running first check on row: {0}'.format(row)) 
    assert row[0] is not None 


def second_check(row): 
    print('Running second check on row: {0}'.format(row)) 
    assert row[1] is not None 

def test_generator(): 
    for row in data: 
     yield first_check, row 
     yield second_check, row 

if __name__ == '__main__': 
    all_tests = TestLoader().loadTestsFromGenerator(test_generator, 'test-with-nose') 
    suite = nose.suite.LazySuite(all_tests) 
    nose.main(suite=suite) 

--nocapture選項(否則打印將被隱藏)運行這給:

Running first check on row: ['a', 'b', 'c'] 
.Running second check on row: ['a', 'b', 'c'] 
.Running first check on row: ['1', '2', '3'] 
.Running second check on row: ['1', '2', '3'] 
. 
---------------------------------------------------------------------- 
Ran 4 tests in 0.002s 

OK 

這樣做對於大量被測試的行(例如,通過從csv讀取器而不是嵌套列表中產生它們)來說並不是很快,但是,它將內存消耗保持得很低,因爲只有一個行和測試組合被加載到內存中 同時。

編輯:當我爲一個非常大的生成器(例如具有數百萬行的csv文件)執行腳本時,仍然會一直吃下我珍貴的內存。那可能是因爲在所有測試運行之前,測試結果仍然存儲在內部某處?