前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python-测试-unittest-04-测试结果文件输出

Python-测试-unittest-04-测试结果文件输出

作者头像
zishendianxia
发布2020-02-24 13:43:47
9660
发布2020-02-24 13:43:47
举报
文章被收录于专栏:Python工程师

系统:Windows 7

语言版本:Anaconda3-4.3.0.1-Windows-x86_64

编辑器:pycharm-community-2016.3.2

  • 这个系列讲讲Python的测试版块,江湖有说,没有经过测试的代码就直接投入生产环境使用,是不地道的,基于此,还是学习测试吧
  • 今天继续讲讲单元测试,将测试的结果输出到一个文本文件

Part 1:代码

代码语言:javascript
复制
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)

代码截图

对比上一篇

Python-测试-unittest-03-测试执行顺序

Part 2:核心区别

代码语言:javascript
复制
    with open('TestResult.txt', 'a') as f:
        runner = unittest.TextTestRunner(stream=f, verbosity=2)
        runner.run(suite)

输出结果文件

Part 3:部分代码解读

  1. 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:,在本示例中也是可以的,表示清空上次的数据,重新写入

不同打开方式解读(来自源码)

本文为原创作品,欢迎分享朋友圈

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-01-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python工程师 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档