将if __name__
测试移动到底部(将import unittest
部分留在顶部):
import unittest
class TestInversion(unittest.TestCase):
def setUp(self):
pass
def test_1(self):
self.assertEqual(1, 2)
def test_2(self):
self.assertEqual(2, 2)
def test_3(self):
self.assertEqual(3, 3)
def test_4(self):
self.assertEqual(4, 4)
if __name__ == '__main__':
unittest.main()
然后它会工作:
$ python test.py
F...
======================================================================
FAIL: test_1 (__main__.TestInversion)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 9, in test_1
self.assertEqual(1, 2)
AssertionError: 1 != 2
----------------------------------------------------------------------
Ran 4 tests in 0.000s
FAILED (failures=1)
在main()
执行的时候,你需要定义你的测试用例;如果您main()
在顶部调用,则尚未定义任何测试。