Python的unittest的基本代码如下,请注意要继承类名“unittest.TestCase”
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()
如果要运行,可以使用命令“python3 -m unittest TestStringMethods.TestStringMethods.test_upper”
更多的关于unittest的使用,请参考python官方文档,
https://docs.python.org/3/library/unittest.html#unittest.TestCase