我是unittest
的新手,我不知道为什么会收到此错误:
runTest (__main__.TestTimeInterval)
No test ... Traceback (most recent call last):
File "/Users/bli1/Development/Trinity/qa-trinity/python_lib/qe/tests/test_timestamp_interval.py", line 122, in <module>
sys.exit(main(sys.argv))
File "/Users/bli1/Development/Trinity/qa-trinity/python_lib/qe/tests/test_timestamp_interval.py", line 110, in main
result_set = runner.run(suite)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/runner.py", line 168, in run
test(result)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/suite.py", line 87, in __call__
return self.run(*args, **kwds)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/suite.py", line 125, in run
test(result)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 625, in __call__
return self.run(*args, **kwds)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 555, in run
testMethod = getattr(self, self._testMethodName)
AttributeError: 'TestTimeInterval' object has no attribute 'runTest'
我想创建一个简单的测试,看看是否一切正常,但我得到了上面的错误。我将测试添加到套件中,然后使用.run()
class TestTimeInterval(unittest.TestCase):
def __init__(self, log, runtag, interval, path_file):
super(TestTimeInterval, self).__init__()
self.interval = interval
self.path_file = path_file
self.log = log
self.runtag = runtag
def test_record(self):
self.assertTrue(1 > 0)
##############################################################################
def main(argv):
exit_code = 0
global me; me = os.path.basename(argv[0]) # name of this program
global mydir; mydir = os.path.dirname(os.path.abspath(__file__))
parser = argparse.ArgumentParser(description=main.__doc__)
parser.add_argument("file", metavar="FILE",
help="File filled with hdfs paths separated by newlines")
parser.add_argument("runtag", metavar="RUNTAG", help="tag for the test run")
parser.add_argument("-t", "--time", default="10", dest="interval",
help="Time interval (minutes) between server_timestamp and interval given by HDFS folder name")
args = parser.parse_args(args=argv[1:])
log = logging.getLogger(me)
logfile = args.runtag + ".log"
if os.path.exists(logfile):
os.remove(logfile)
log.addHandler(logging.FileHandler(logfile))
console = logging.StreamHandler(sys.stderr); console.setLevel(logging.WARNING); log.addHandler(console)
if exit_code == 0:
runner = unittest.TextTestRunner(stream=sys.stdout, descriptions=True, verbosity=2)
suite = unittest.TestSuite()
print(args)
suite.addTest(TestTimeInterval(log, args.runtag, args.interval, args.file))
try:
log.info("{0}: START: {1}".format(me, datetime.datetime.now().ctime()))
result_set = runner.run(suite)
except KeyboardInterrupt as e:
log.info("{0}: exit on keyboard interrupt".format(me))
exit_code = 1
else:
exit_code = len(result_set.errors) + len(result_set.failures)
finally:
log.info("{0}: FINISH: {1}".format(me, datetime.datetime.now().ctime()))
return exit_code
##############################################################################
# The following code calls main only if this program is invoked standalone
if __name__ == "__main__":
sys.exit(main(sys.argv))
答案 0 :(得分:0)
您几乎不需要自己创建TestSuite和TestRunner。通常,您可以执行以下操作:
# my_test.py
import unittest
class Something(object):
def __init__(self):
self.foo = 1
class TestSomething(unittest.TestCase):
def setUp(self):
super(TestSomething, self).setUp()
self.something = Something()
def test_record(self):
self.assertTrue(1 > 0)
def test_something_foo_equals_1(self):
self.assertEqual(self.something.foo, 1)
if __name__ == '__main__':
unittest.main()
现在,要运行测试,只需执行脚本。
python my_test.py