系统:Windows 7
语言版本:Anaconda3-4.3.0.1-Windows-x86_64
编辑器:pycharm-community-2016.3.2
- 这个系列讲讲Python的测试版块,江湖有说,没有经过测试的代码就直接投入生产环境使用,是不地道的,基于此,还是学习测试吧
- 今天继续讲讲单元测试,将测试的结果输出到一个文本文件
Part 1:代码
import unittest
from python_test_example.be_tested_02 import *
class TestClass(unittest.TestCase):
def test_e_to_list(self):
list_ = str_to_list("0-2-8-9-6-3")
self.assertIn(6, list_)
self.assertNotIn("6", list_)
def test_f(self):
self.assertEqual("1", True)
def test_d_islower(self):
self.assertTrue("CASE".islower())
self.assertFalse("CASE".islower())
if __name__ == '__main__':
suite = unittest.TestSuite()
tests = [TestClass("test_e_to_list"), TestClass("test_d_islower"), TestClass("test_f")]
suite.addTests(tests)
with open('TestResult.txt', 'a') as f:
runner = unittest.TextTestRunner(stream=f, verbosity=2)
runner.run(suite)
代码截图

对比上一篇

Part 2:核心区别
with open('TestResult.txt', 'a') as f:
runner = unittest.TextTestRunner(stream=f, verbosity=2)
runner.run(suite)
输出结果文件

Part 3:部分代码解读
-
with open('TestResult.txt', 'a') as f:
- 其中 'a' 为操作方式,表示
open for writing, appending to the end of the file if it exists
,追加的意思,表示在原有内容后面追加 - 改为:
with open('TestResult.txt', 'w') as f:
,在本示例中也是可以的,表示清空上次的数据,重新写入
- 其中 'a' 为操作方式,表示
不同打开方式解读(来自源码)

本文为原创作品,欢迎分享朋友圈
长按图片识别二维码,关注本公众号
Python 优雅 帅气

网友评论