8

Noseにはバグがあります。ジェネレーターによって作成されたテスト名はキャッシュされないため、エラーは、失敗した実際のテストではなく、最後のテストで発生したように見えます。バグレポートの説明の解決策に従って回避しましたが、XMLレポート(--with-xunit)ではなく、stdoutに表示されている名前に対してのみ機能します。

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" />...

パーソナライズされたテスト名がstdoutとXML出力の両方に表示されるようにこれを変更するにはどうすればよいですか?

nosetestsバージョン1.1.2とPython2.6.6を使用しています

4

4 に答える 4

4

describeTest を実装するプラグインを追加することで、Nose 名のテスト方法を変更できます。

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)

次に、このプラグインをインストールし、Nose 呼び出しで有効にする必要があります。

于 2012-12-15T11:54:47.060 に答える
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" />
于 2013-05-16T08:24:17.053 に答える
1

アナントが言及しているように、これを使用できます。

testGenerator.__name__

代わりにこれを使用することもできます

testGenerator.compat_func_name

テスト クラスに引数がある場合は、with_setup だけでなく、それらをカリー化することをお勧めします。ラムダを使用するとインポートが節約され、少しすっきりしたと思います。例えば、

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
于 2014-02-14T22:42:40.660 に答える
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 でも名前が適切に表示されます。

生成されたテスト名

于 2014-05-15T17:18:24.403 に答える