我想要一个单元测试属性在Python添加到对象

class Boy:

    def run(self, args):
        print("Hello")

class BoyTest(unittest.TestCase)

    def test(self)
         self.assertEqual('2' , '2')

def self_test():
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    suite.addTest(loader.loadTestsFromTestCase(Boy.BoyTest))
    return suite

不过,我不断收到"AttributeError: class Boy has no attribute 'BoyTest'"每当我打电话self_test()。为什么呢?

有帮助吗?

解决方案

由于loadTestsFromTestCase的说法,你要访问Boy.BoyTest,即类对象BoyTest,这根本就不存在,因为错误味精是告诉你的Boy属性。你为什么不只是使用BoyTest那里呢?

其他提示

如亚历阐明你正在试图使用作为BoyTest男孩的attibute:

class Boy:

    def run(self, args):
        print("Hello")

class BoyTest(unittest.TestCase)

    def test(self)
         self.assertEqual('2' , '2')

def self_test():
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    suite.addTest(loader.loadTestsFromTestCase(BoyTest))
    return suite

请注意的变化:

suite.addTest(loader.loadTestsFromTestCase(Boy.BoyTest))

为:

suite.addTest(loader.loadTestsFromTestCase(BoyTest))

这是不是解决问题了吗?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top