Python:使全局单元测试功能

突袭

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

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,但我认为与文档不一致我的答案使用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相关的所有内容。

我直接从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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章