系统:Windows 7
语言版本:Anaconda3-4.3.0.1-Windows-x86_64
编辑器:pycharm-community-2016.3.2
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:
open for writing, appending to the end of the file if it exists
,追加的意思,表示在原有内容后面追加with open('TestResult.txt', 'w') as f:
,在本示例中也是可以的,表示清空上次的数据,重新写入不同打开方式解读(来自源码)
本文为原创作品,欢迎分享朋友圈