单体测试通过unitest进行。
1.安装单体测试环境
安装PIP
$ sudo python -m ensurepip --default-pip
升级PIP组件
$ python -m pip install --upgrade pip setuptools wheel
安装测试工具
https://pypi.org/project/html-testRunner/#files
安装HtmlTestRunner测试工具
$ sudo pip install https://files.pythonhosted.org/packages/cd/30/79c7891abe90cd8a5711531f5af246671b43015a5900b721a695bfa01a8e/html-testRunner-1.1.2.tar.gz
2. 编写测试程序
测试程序
import HtmlTestRunner
import unittest
class TestStringMethods(unittest.TestCase):
""" Example test for HtmlRunner. """
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)
def test_error(self):
""" This test should be marked as error one. """
raise ValueError
def test_fail(self):
""" This test should fail. """
self.assertEqual(1, 2)
@unittest.skip("This is a skipped test.")
def test_skip(self):
""" This test should be skipped. """
pass
if __name__ == '__main__':
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='example_dir'))