我编写了以下代码来测试基本的单元测试用例。当我执行以下代码时。我没有得到任何输出。有人能让我知道可能存在什么问题。
import unittest
class test123(unittest.TestCase):
def test1(self):
print "test1"
if __name__ == "main":
x=test123()
x.test1()
unittest.main()
答案 0 :(得分:2)
您的代码应如下所示:
import unittest
class test123(unittest.TestCase):
def test1(self):
print "test1"
if __name__ == "__main__":
unittest.main()
因此它的名称和主要是在开头和结尾有两个下划线,当你更改它并用你的代码运行它然后你会得到一个错误:
x = test123()
x.test1()
ValueError: no such test method in <class '__main__.test123'>: runTest
答案 1 :(得分:1)
在测试中,您需要两件事:
test.py
import unittest
class TestHello(unittest.TestCase):
def test_hello(self): # Your test function usually need define her name with test
str_hello = 'hello'
self.assertEqual(str_hello, 'hello') # you need return a expected result
def test_split(self):
str_hello = 'hello world'
self.assertEqual(str_hello.split(), ['hello', 'world'])
if __name__ == '__main__':
unittest.main()
执行使用:
python -m unittest test
出:
stackoverflow$ python -m unittest test
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK