def mount_details(): """ 打印挂载详细信息 """ if os.path.exists('/proc/mounts'): fd = open('/proc/mounts') for line in fd: line = line.strip() words = line.split() print('{} on {} type {}'.format(words[0],words[1],words[2]), end=' ') if len(words) > 5: print('({})'.format(' '.join(words[3:-2]))) else: print() fd.close()
def parse_mounts(): """ 分析 /proc/mounts 并 返回元祖的列表 """ result = [] if os.path.exists('/proc/mounts'): fd = open('/proc/mounts') for line in fd: line = line.strip() words = line.split() if len(words) > 5: res = (words[0],words[1],words[2],'({})'.format(' '.join(words[3:-2]))) else: res = (words[0],words[1],words[2]) result.append(res) fd.close() return result
def mount_details(): """ 打印挂载详细信息 """ result = parse_mounts() for line in result: if len(line) == 4: print('{} on {} type {} {}'.format(*line)) else: print('{} on {} type {}'.format(*line))
def test_rootext4(self): """ 测试找出根文件系统 """ result = parse_mounts() for line in result: if line[1] == '/' and line[2] != 'rootfs': self.assertEqual(line[2], 'ext4')
if __name__ == '__main__': unittest.main()
运行程序
1 2 3 4 5 6
$ python3 mounttest.py .. ---------------------------------------------------------------------- Ran 2 tests in 0.001s
OK
测试覆盖率
测试覆盖率是找到代码库未经测试的部分的简单方法。它并不会告诉你的测试好不好。
在 Python 中我们已经有了一个不错的覆盖率工具来帮助我们。你可以在实验楼环境中安装它:
1
$ sudo pip3 install coverage
覆盖率示例
1 2 3 4 5 6 7 8 9 10 11 12 13
$ coverage3 run mounttest.py .. ---------------------------------------------------------------------- Ran 2 tests in 0.013s
OK $ coverage3 report -m Name Stmts Miss Cover Missing -------------------------------------------- mounttab2.py 22 7 68% 16, 25-30, 34 mounttest.py 14 0 100% -------------------------------------------- TOTAL 36 7 81%