0

I'm trying to add a unittest attribute to an object in 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

However, I keep getting "AttributeError: class Boy has no attribute 'BoyTest'" whenever I call self_test(). Why?

SilentGhost
322k67 gold badges312 silver badges294 bronze badges
asked Aug 27, 2009 at 4:28

2 Answers 2

3

As the argument of loadTestsFromTestCase, you're trying to access Boy.BoyTest, i.e., the BoyTest attribute of class object Boy, which just doesn't exist, as the error msg is telling you. Why don't you just use BoyTest there instead?

answered Aug 27, 2009 at 4:33
Sign up to request clarification or add additional context in comments.

Comments

-1

As Alex has stated you are trying to use BoyTest as an attibute of Boy:

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

Note the change:

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

to:

suite.addTest(loader.loadTestsFromTestCase(BoyTest))

Does this solve your problem?

answered Aug 27, 2009 at 10:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.