質問

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, 、エラーMSGがあなたに伝えているので、それは存在しません。使用してみませんか BoyTest 代わりにそこに?

他のヒント

アレックスが述べたように、あなたはボーイテストを少年の添付として使用しようとしています:

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