Run unittest in a Class
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') # This is the name of the file
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')
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 TestInternalSanity(unittest.TestCase):
def setUp(self):
setUp script ....
def tearDown(self):
script ....
class BasicInternalSanity(TestInternalSanity):
def runTest(self):
test script ....
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 def suite()
inside the class SmokeTests
the script executes but the tests don't run but if I remove the class the tests run. 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. This need's to be called from a class as the script that I'm calling it from is Object Oriented. If anyone can get the code to be run using Call Tests I would appreciate it alot.
This work's:
def suite(): #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 does not 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')
I can't seem to get this to run in the class, can anyone see the solution.
Thanks
Got it working, sorry for wasting everyones time, the answer was to change the default test name.
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__':
Smoke = SmokeTests()
unittest.main(defaultTest='Smoke.suite')
Thanks for any help.
It looks like you are making unittests much more complicated than they actually are. Perhaps your implementation should look more like this:
import unittest
class MyClass(object):
def add(self, val, other):
return val + other
def subtract(self, val, other):
return val - other
class TestClass(unittest.TestCase):
def test_add(self):
myclass = MyClass()
self.assert_(myclass.add(1, 2) == 3)
def test_subtract(self):
myclass = MyClass()
self.assert_(myclass.subtract(2, 1) == 1)
if __name__ == '__main__':
unittest.main()
精彩评论