在类中运行单元测试
我有一个测试套件用来进行冒烟测试。我把所有的脚本都存放在不同的类里,但当我尝试运行测试套件时,如果它在一个类里面,我就无法让它正常工作。下面是代码:(一个用来调用测试的类)
from alltests import SmokeTests
class CallTests(SmokeTests):
def integration(self):
self.suite()
if __name__ == '__main__':
run = CallTests()
run.integration()
还有测试套件:
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')
我能看到怎么调用一个普通的函数,但在调用测试套件时我遇到了困难。在其中一个测试中,套件是这样设置的:
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)
如果我在 class SmokeTests
里面有 def suite()
,脚本会执行,但测试却不运行;而如果我把类去掉,测试就能运行。我是把这个当作脚本来运行,并把变量传入测试中。我不想通过 os.system('python tests.py') 来运行测试。我希望能像调用其他函数一样通过我定义的类来调用测试。因为我调用它的脚本是面向对象的,所以必须从一个类中调用。如果有人能让代码通过 Call Tests 来运行,我会非常感激。
这个能工作:
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')
这个不行:
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')
我似乎无法在类中让这个运行,有人能看到解决办法吗?
谢谢
2 个回答
1
看起来你把单元测试搞得比实际要复杂多了。也许你的实现可以更简单一些,像这样:
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()
2
我搞定了,抱歉浪费了大家的时间,答案是要更改默认的测试名称。
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')
谢谢大家的帮助。