Pergunta

Estou tentando adicionar um atributo mais unitter a um objeto em 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

No entanto, eu continuo recebendo "AttributeError: class Boy has no attribute 'BoyTest'" sempre que eu ligo self_test(). Por quê?

Foi útil?

Solução

Como o argumento de loadTestsFromTestCase, você está tentando acessar Boy.BoyTest, ou seja, o BoyTest atributo do objeto de classe Boy, que simplesmente não existe, pois o erro msg está lhe dizendo. Por que você simplesmente não usa BoyTest lá em vez disso?

Outras dicas

Como Alex afirmou que você está tentando usar o Boytest como um garoto de garoto:

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

Observe a alteração:

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

para:

suite.addTest(loader.loadTestsFromTestCase(BoyTest))

Isso resolve seu problema?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top