我正在使用TestNothing
从课程unittest.TestSuite()
运行所有测试。
我的__init__.py
是:
import unittest
from .test_nothing import TestNothing
def suite():
"""
Define suite
"""
test_suite = unittest.TestSuite()
test_suite.addTests([
unittest.TestLoader().loadTestsFromTestCase(TestNothing),
])
return test_suite
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite())
我的test_nothing.py
是:
import unittest
class TestNothing:
def test_0010_test_nothing(self):
self.assertEqual(200, 200)
def suite():
"Test suite"
test_suite = unittest.TestSuite()
test_suite.addTests(
unittest.TestLoader().loadTestsFromTestCase(TestNothing)
)
return test_suite
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite())
我在运行python test_nothong.py
时遇到以下错误:
Traceback (most recent call last):
File "test_nothing.py", line 19, in <module>
unittest.TextTestRunner(verbosity=2).run(suite())
File "test_nothing.py", line 13, in suite
unittest.TestLoader().loadTestsFromTestCase(TestNothing)
File
"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 56, in loadTestsFromTestCase
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
TypeError: this constructor takes no arguments
答案 0 :(得分:0)
实际上,实际上TestNothing的构造函数并没有引用参数......而且它必须。 我建议你让你的测试类TestNothing继承自unittest.TestCase。像这样,你的类的Ctor将从构造器unittest.TestCase继承,它接受参数。
import unittest
class TestNothing(unittest.TestCase):
def test_0010_test_nothing(self):
self.assertEqual(200, 200)
您还可以点击此链接python unittest 25.3.1