1

在 Python 2.5(实际上是 Jython)中,对于 UnitTest TestCase 类 - 没有 SetUpClass 方法,并且__init__不是真正可以接受的(没有参考自我)。当我尝试更改 TestCase 中的文档字符串时:

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

我试图通过将文档字符串实例化为一个对象(在哪里self.__doc__是可写的)来更改文档字符串。

更新:但我想避免在子类中进行额外的编码(即继承超类函数来设置子类的文档字符串),例如:

文件 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__

但是,unittest 的运行结果仍然是:

MyTestCase Docstring = DynamicTestCase

当我期望MyTestCase Docstring = MyTestCase

4

2 回答 2

1

Updated -是加载当前模块__file__的路径名,所以在 DynamicTestCase.py 中使用自然会产生路径 DynamicTestCase.py。但是,您可以像这样将路径传递给子类:__file__setDocstring()

动态测试案例.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 的文档字符串吗?例如,您可以继承 TestCase 并提供您自己的文档字符串:

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

不确定您是否尝试过,但unittest2 包的TestCase 具有setUpClass, tearDownClass类方法。它是 Python 2.7 对 Python 2.6 及更早版本的改进的向后移植。

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 回答