biznez
biznez

Reputation: 4021

Attribute Error in Python

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?

Upvotes: 0

Views: 1811

Answers (2)

kjfletch
kjfletch

Reputation: 5494

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?

Upvotes: -1

Alex Martelli
Alex Martelli

Reputation: 882531

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?

Upvotes: 3

Related Questions