第二周作业 WordCount
github地址: https://github.com/Yulight1401/SoftWareTest/tree/master
PSP2.1 |
PSP阶段 |
预估耗时 (分钟) |
实际耗时 (分钟) |
Planning |
计划 |
5 | 5 |
· Estimate |
· 估计这个任务需要多少时间 |
200 | 250 |
Development |
开发 |
50 | 60 |
· Analysis |
· 需求分析 (包括学习新技术) |
10 | 12 |
· Design Spec |
· 生成设计文档 |
10 | 10 |
· Design Review |
· 设计复审 (和同事审核设计文档) |
5 | 5 |
· Coding Standard |
· 代码规范 (为目前的开发制定合适的规范) |
5 | 5 |
· Design |
· 具体设计 |
5 | 8 |
· Coding |
· 具体编码 |
50 | 50 |
· Code Review |
· 代码复审 |
10 | 10 |
· Test |
· 测试(自我测试,修改代码,提交修改) |
5 | 5 |
Reporting |
报告 |
15 | 20 |
· Test Report |
· 测试报告 |
5 | 5 |
· Size Measurement |
· 计算工作量 |
5 | 5 |
· Postmortem & Process Improvement Plan |
· 事后总结, 并提出过程改进计划 |
5 | 5 |
合计 |
305 | 325 |
解题思路:
1. 首先进行需求分析
2. 设计编码,我打算通过 文件流 -> 文字流 -> 结果,这样来流式处理。最后将各个阶段的结果合并产生最终结果。
程序设计
由于是流式处理,所以我的处理过程主要是函数式的处理,当通过文件读取到一个字符串流后,就通过不同的功能函数,产生不同的需要的输出。
# coding=utf-8 import sys import os RESULT_NAME = 'result.txt' # 统计字符数 def funChar(flow): charLen = len(flow) result = '字符数为:' + str(charLen) + '\n' print(result) return result # 统计单词 def funWord(flow): wordLen = len(flow.replace(',', ' ').split(' ')) result = '单词数为:' + str(wordLen) + '\n' print(result) return result # 统计行数 def funLine(flow): lineLen = flow.count('\n') result = '行数为:' + str(lineLen) + '\n' print(result) return result # 写文件 def funOut(result): fileObj = open(RESULT_NAME, 'w') fileObj.write(result) return 0 # 主函数 def main(): result = '' paramsLen = len(sys.argv) fileName = sys.argv[paramsLen - 1] params = sys.argv[1:-1] fileObj = open(fileName, 'r') wordFlow = fileObj.read() ifOutPut = 0
#根据每个 for func in params: if func == '-c': result += funChar(wordFlow) if func == '-w': result += funWord(wordFlow) if func == '-l': result += funLine(wordFlow) if func == '-o': ifOutPut += 1 if ifOutPut == 1: funOut(result) main()
测试设计过程
所有的功能项集中在函数上,所以对功能函数进行单元测试,可以见得我的功能函数都是纯函数,十分容易单元测试,而主函数为逻辑和流程控制函数,这个地方是最容易出现问题的地方,也是最不好测试的地方。
这里主要通过十个测试用例,来完成对函数的单元测试,对主函数的白盒测试,对系统的回归测试。
测试代码的编写,主要通过白盒测试,自己随便写一些特殊情况的测试用例,首先自己统计出预测的结果,然后用循环把这些跑一遍,看函数的结果是否符合预期,符合则通过测试