Problem's running unittest test suite OO

2019-08-14 19:04发布

问题:

I have a test suite to perform smoke tests. I have all my script stored in various classes but when I try and run the test suite I can't seem to get it working if it is in a class. The code is below: (a class to call the tests)

from alltests import SmokeTests

class CallTests(SmokeTests):

    def integration(self):

        self.suite()

if __name__ == '__main__':
    run = CallTests()
    run.integration()

And the test suite:

class SmokeTests():

    def suite(self): #Function stores all the modules to be tested 
        modules_to_test = ('external_sanity', 'internal_sanity')
        alltests = unittest.TestSuite()
        for module in map(__import__, modules_to_test):
            alltests.addTest(unittest.findTestCases(module))
        return alltests
if __name__ == '__main__':
    unittest.main(defaultTest='suite')

This output's an error: Attribute Error: 'module' object has no attribute 'suite'

So I can see how to call a normal function defined but I'm finding it difficult calling in the suite. In one of the tests the suite is set up like so:

class InternalSanityTestSuite(unittest.TestSuite):

# Tests to be tested by test suite
def makeInternalSanityTestSuite():
    suite = unittest.TestSuite()
    suite.addTest(TestInternalSanity("BasicInternalSanity"))
    suite.addTest(TestInternalSanity("VerifyInternalSanityTestFail"))
    return suite

def suite():
    return unittest.makeSuite(TestInternalSanity)

If I have someSuite() inside the class SmokeTests python cannot find the attribute suite but if I remove the class it work's. I run this as a script and call in variables into the tests. I do not want to have to run the tests by os.system('python tests.py'). I was hoping to call the tests through the class I have like any other function

Can anyone help me with getting this running?

Thanks for any help in advance.

回答1:

I know this is not the answer, but I'd suggest using library that can use test discovery, like nose or unittest capability from Python 2.7+.

Possibility to do

nosetests module.submodule

or

nosetests module.submodule:TestCase.test_method

is priceless :)



回答2:

This can't work:

class SmokeTests():

    def suite(self): #Function stores all the modules to be tested 
        modules_to_test = ('external_sanity', 'internal_sanity')
        alltests = unittest.TestSuite()
        for module in map(__import__, modules_to_test):
            alltests.addTest(unittest.findTestCases(module))
        return alltests

if __name__ == '__main__':
    unittest.main(defaultTest='suite')

This output's an error: Attribute Error: 'module' object has no attribute 'suite'.

Your suite the value of the SmokeTests().suite() method. Note a variable named suite, since you have no such variable.

It's easier to use a simple function for your suite.

def someSuite():
    modules_to_test
    ...
    return alltests

if __name__ == "__main__":
    unittest.main( defaultTest= someSuite() )

Something like that would be closer to correct.