2012-06-25 84 views
8

鼻子有bug - 由生成器創建的測試名稱未緩存,因此錯誤看起來像是發生在上次測試中,而不是實際測試失敗的地方。我下面的錯誤報告討論solution得到周圍,但它僅適用於在標準輸出上顯示的名字,而不是在XML報告(--with-的xUnit)鼻子測試生成器創建的測試更改名稱

from functools import partial, update_wrapper 
def testGenerator(): 
    for i in range(10): 
     func = partial(test) 
     # make decorator with_setup() work again 
     update_wrapper(func, test) 
     func.description = "nice test name %s" % i 
     yield func 

def test(): 
    pass 

不出所料鼻子的輸出,像

nice test name 0 ... ok 
nice test name 1 ... ok 
nice test name 2 ... ok 
... 

但是XML中的測試名稱只是'testGenerator'。

...<testcase classname="example" name="testGenerator" time="0.000" />... 

我怎樣才能改變這種做法,在標準輸出和XML輸出顯示的個性化測試的名字呢?

我使用nosetests 1.1.2版和Python 2.6.6

+0

更改 'FUNC .__ name__' 或 'FUNC .__ doc__會給出' 也不管用。 –

+0

如何創建一個測試套件? – Apalala

+1

@Apalala謝謝,這似乎可以解決它。雖然我無法找到關於如何在鼻子中使用這些信息的任何信息,它如何與鼻子的測試發現相結合。你能舉個例子嗎? –

回答

4

你可以改變的方式,通過adding a plugin鼻子名測試實現describeTest

from nose.plugins import Plugin 
class CustomName(Plugin): 
    "Change the printed description/name of the test." 
    def describeTest(self, test): 
     return "%s:%s" % (test.test.__module__, test.test.description) 

你便要install this plugin ,並在鼻子調用中啓用它。

1

您可以添加以下行。

testGenerator.__name__ = "nice test name %s" % i

例子:

from functools import partial, update_wrapper 
def testGenerator(): 
    for i in range(10): 
     func = partial(test) 
     # make decorator with_setup() work again 
     update_wrapper(func, test) 
     func.description = "nice test name %s" % i 
     testGenerator.__name__ = "nice test name %s" % i 
     yield func 

def test(): 
    pass 

這將導致你想要的名稱。

<testsuite name="nosetests" tests="11" errors="0" failures="0" skip="0"><testcase classname="sample" name="nice test name 0" time="0.000" /> 
+0

這幾乎工作,除了所有的測試得到同樣的名字爲我(鼻子1.2.1,蟒蛇2.7.3) – Rog

1

正如Ananth提到的那樣,您可以使用它。

testGenerator.__name__ 

你也可以用這個代替

testGenerator.compat_func_name 

如果您的測試類有爭論,我建議他們討好,以及鑽營with_setup。使用lambda保存導入,我認爲它有點乾淨。例如,

from nose.tools import with_setup 

def testGenerator(): 
    for i in range(10): 
     func = with_setup(set_up, tear_down)(lambda: test(i)) 

     func.description = "nice test name %s" % i 
     testGenerator.compat_func_name = func.description 

     yield func 

def test(i): 
    pass 

def set_up(): 
    pass 

def tear_down(): 
    pass 
+0

良好的電話呼叫 –

0

如果使用鼻的Eclipe的PyUnit中:

import nose 

class Test(object): 
    CURRENT_TEST_NAME = None 

    def test_generator(self): 
     def the_test(*args,**kwargs): 
      pass 

     for i in range(10): 
      # Set test name 
      Test.CURRENT_TEST_NAME = "TestGenerated_%i"%i 
      the_test.description = Test.CURRENT_TEST_NAME 

      # Yield generated test 
      yield the_test,i 

    # Set the name of each test generated 
    test_generator.address = lambda arg=None:(__file__, Test, Test.CURRENT_TEST_NAME) 

這將導致名稱中的PyUnit很好地顯示爲好。

Generated test names