1

Python 2.5 (実際には Jython) では、UnitTest TestCase クラスの場合、SetUpClass メソッド__init__がなく、実際には受け入れられません (自己への参照はありません)。TestCase 内で docstring を変更しようとすると:

import os
fileName = os.path.split(__file__)[1]
testCaseName = os.path.splitext(fileName)[0]
setattr(__name__, '__doc__', testCaseName)

私は得ています:

setattr(__name__, '__doc__', testCaseName)
TypeError: readonly attribute

オブジェクトにインスタンス化してdocstringを変更しようとしました(self.__doc__書き込み可能)。

更新:しかし、サブクラスでの追加のコーディング (つまり、サブクラスの docstring を設定するためのスーパークラス関数の継承) は避けたいと思います。次に例を示します。

ファイル DynamicTestCase.py には以下が含まれます。

class DynamicTestCase(unittest.TestCase):
    def setDocstring(self, testCaseDocstring=None):
        if not testCaseDocstring:
            fileName = os.path.split(__file__)[1]
            testCaseDocstring = os.path.splitext(fileName)[0]
        setattr(self, '__doc__', testCaseDocstring)

ファイル MyTestCase.py には以下が含まれます。

class MyTestCase(DynamicTestCase):
    def test_print_docstring(self):
        self.setDocstring()
        print 'MyTestCase Docstring = ', self.__doc__

それでも、単体テストの実行結果は次のとおりです。

MyTestCase Docstring = DynamicTestCase

MyTestCase Docstring = MyTestCaseを期待したとき

4

2 に答える 2

1

Updated -現在のモジュールがロードされ__file__たパス名であるため、当然のことながらDynamicTestCase.py 内で使用すると、DynamicTestCase.py というパスになります。ただし、次のようにサブクラスからパスを渡すことができます。__file__setDocstring()

DynamicTestCase.py :

class DynamicTestCase(unittest.TestCase):
    def setDocstring(self, docstring=None):
        if docstring is None:
            docstring = __file__
        if os.path.exists(docstring):
            name = os.path.split(docstring)[1]
            docstring = os.path.splitext(name)[0]
        setattr(self, '__doc__', docstring)

MyTestCase.py :

class MyTestCase(DynamicTestCase):
    def __init__(self, *args, **kwargs):
        DynamicTestCase.__init__(self, *args, **kwargs)
        self.setDocstring(__file__)

    def test_print_docstring(self):
        print 'MyTestCase Docstring = ', self.__doc__

    def test_new_docstring(self):
        self.setDocstring('hello')
        print 'MyTestCase Docstring = ', self.__doc__

出力:

MyTestCase Docstring =  MyTestCase
MyTestCase Docstring =  hello

残りの回答

上記の元のコード__name__では、クラスではなく文字列です。Jython は__doc__、型の属性を変更することを正当に拒否しますstr

TestCase の docstring を変更したい理由について少し説明していただけますか? たとえば、TestCase をサブクラス化し、独自の docstring を与えることができます。

class MyTestCase(unittest.TestCase):
    "Docstring of MyTestCase"

まだ試したかどうかわかりませんが、unittest2 パッケージのTestCase にはsetUpClass, tearDownClassクラス メソッドがあります。これは、Python 2.6 以前で動作するように Python 2.7 の改善をバックポートしたものです。

Jython では__doc__新しいスタイルのクラスを設定できますが、CPython では設定できません。そのため、コードを移植可能にしたい場合は、目標を達成するための別の方法を見つけたいと思うかもしれません:

Jython 2.2.1 on java1.6.0_24
>>> unittest.TestCase.__doc__ = 'foo bar'
>>> unittest.TestCase.__doc__
'foo bar'

Python 2.6.6 (r266:84292, Feb 12 2011, 01:07:21)
>>> unittest.TestCase.__doc__ = 'foo bar'
AttributeError: attribute '__doc__' of 'type' objects is not writable
于 2011-05-01T23:28:10.227 に答える
0

関数を呼び出しているファイルではなく、DynamicTestCase ファイルのファイル名を取得しています。それを取得するには、スタック フレームに移動する必要があります。

  import inspect

  class DynamicTestCase(unittest.TestCase):
    def setDocstring(self, testCaseDocstring=None):
        if not testCaseDocstring:
            fileName = 'unknown.py'

            # Go up one stack frame and grab the file name
            stack = inspect.stack()
            try:
                frame = stack[1][0]
                fileName = frame.f_code.co_filename        
            finally:
                del stack

            testCaseDocstring = os.path.splitext(fileName)[0]
于 2011-05-03T09:06:49.587 に答える