Skip to main content
 首页 » 编程设计

Python:制作全局单元测试功能

2024年10月01日7sky-heaven

我想制作一个可以在脚本中的其他单元测试中使用的函数。这就是我的意思:

class TestSomething(unittest.TestCase): 
    def __init__(self, title, description): 
        self.title = title 
        self.description = description 
        self.tag = 'ts' 
 
class TestSomething2(unittest.TestCase): 
    def __init__(self, title, description): 
        self.title = title 
        self.description = description 
        self.tag = 'ts2' 
 
class TestWhatever(unittest.TestCase): 
    def test_whatever(self): 
        blah = TestSomething('Happy Gilmore', 'golf movie') 
        AttributeCheck(blah, 'Happy Gilmore', 'golf movie', 'ts') 
 
class TestWhatever2(unittest.TestCase): 
    def test_whatever(self): 
        blah = TestSomething2('Toy Story', 'kids movie') 
        AttributeCheck(blah, 'Toy Story', 'kids movie', 'ts2') 
 
class AttributeCheck(unittest.TestCase): 
    def __init__(self, element, title, description, tag): 
        super(AttributeCheck, self).__init__() 
        self.assertEqual(element.title, title) 
        self.assertEqual(element.description, description) 
        self.assertEqual(element.tag, tag) 
 
    def runTest(self):  # this is what causes problems 
        print 'ok' 
 
 
if __name__ == '__main__': 
    unittest.main() 

我得到的错误是:TypeError: __init__() takes at least 3 arguments (2 given)它基本上尝试运行 AttributeCheck,我认为它运行 runTest 就好像它是一个测试一样。但是,我需要 def runTest(self):因为如果我没有它,那么我会得到:ValueError: no such test method in <class 'AttributeCheck'>: runTest

请您参考如下方法:

您以我以前从未见过的方式使用 unittest.TestCase,我认为这与 documentation 不一致.我的回答使用了我通常使用的 TestCase,希望它能回答您的问题。

至于在脚本中有一个可以在多个测试中使用的函数,你可以在你的测试类中添加一个函数来为你做检查。如果名称中没有“test”,则不会作为测试运行:

class TestWhatever(unittest.TestCase): 
    def test_whatever_does_something(self): 
        instance = Whatever('Happy Gilmore', 'golf movie', 'ts') 
        self._check_attributes(instance, 'Happy Gilmore', 'golf movie', 'ts') 
 
    def _check_attributes(self, element, title, description, tag): 
        self.assertEqual(element.title, title) 
        self.assertEqual(element.description, description) 
        self.assertEqual(element.tag, tag) 

这不是很有用,因为您的检查方法仅限于此类。如果愿意,您可以将它导入另一个测试类,但就职责分离而言,这有点困惑。

我通常尝试让每个测试文件有 1 个测试类,对应于恰好一个生产类。每个“测试”都是测试类中的一个方法。然后,如果有一个函数我想从很多测试类中运行,我将它放在一个单独的文件中,我称之为“test_helpers.py”。你不应该让你的测试助手继承自 TestCase。您可以定义一个失败异常并从您的测试辅助方法中引发它。

以下代码将在同一目录中拆分为 5 个单独的文件。文件名在注释中。请注意,Blah 类位于“blah.py”中,对应于 test_blah.py 中的测试类 TestBlah。这就是您测试与 Blah 相关的所有内容的地方。

我直接从 source code for unittest 中窃取了 test_helpers.py 中 FailureException 的代码.

#blah.py 
class Blah(object): 
    def __init__(self, title, description, tag): 
        self.title = title 
        self.description = description 
        self.tag = tag 
 
#test_blah.py 
from test_helpers import check_attributes 
 
class TestBlah(unittest.TestCase): 
    def test_constructor(self): 
        blah = Blah('Happy Gilmore', 'golf movie', 'ts') 
        check_attributes(blah, 'Happy Gilmore', 'golf movie', 'ts') 
 
#sub_blah.py 
from blah import Blah 
 
class SubBlah(Blah): 
    def __init__(self, title, description, tag): 
        super(SubBlah, self).__init__() 
        self.different_attribute = "I'm Different!" 
 
#test_sub_blah.py 
from test_helpers import check_attributes 
 
class TestSubBlah(unittest.TestCase): 
    def test_constructor(self): 
        sub_blah = SubBlah('Toy Story', 'kids movie', 'sb') 
        check_attributes(blah, 'Toy Story', 'kids movie', 'sb') 
        self.assertEqual("I'm Different!", sub_blah.different_attribute) 
 
#test_helpers.py 
import Exception 
 
def check_attributes(element, title, description, tag): 
    if not title == element.title: 
            raise FailureException(msg or '%r != %r' % (title, element.title)) 
        if not description == element.description : 
            raise FailureException(msg or '%r != %r' % (description, element.description)) 
        if not tag == element.tag: 
            raise FailureException(msg or '%r != %r' % (tag, element.tag)) 
 
class FailureException(Exception): 
    #pass here to make it a basic exception 
    pass 
 
    #If you  need custom code, you can override __init__ 
    #def __init__(self, message, errors): 
    # 
    #   super(FailureException, self).__init__(message)