سؤال

أحاول إضافة سمة Unuttest إلى كائن في بيثون

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 هناك بدلا من ذلك؟

نصائح أخرى

كما ذكر أليكس أنك تحاول استخدام 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