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を期待したとき