下面以Python语言为例来说明软件开发流程的安排
对于软件开发,编写实现功能的代码只是软件开发的很小的一部分,其前前后后还要包括很多的内容,下面具体来列出了设计,编码,静态代码分析,测试,调优这四步来说
1)设计
这涉及到系统设计,实现设计,当然这里面涉及到架构,涉及到模式等
2)编码
就是使用编辑器来编写代码,具体不同的语言有着不同的编译器,像python就有pydev+eclipse, pyscripters等等,c++有visual c++, slickedit等。
假设我们使用pydev写出如下的测试代码:
#!/usr/bin/env python
"""
A simple test
"""
import os
import sys
def myprint(start):
"""Print the numbers"""
for item in range(10):
print 'item=', start * 10 + item
def mysum(first, second):
"""Get the sum of a and b"""
return first + second
def main():
"""The main function"""
for i in range(10):
myprint(i)
if __name__ == '__main__':
print 'OS: ', os.name
print 'Arguments: ', sys.argv
main()
3)静态代码分析
编写完代码之后,必须要进行静态代码分析,这包括编码规范、静态代码逻辑分析。python中有静态代码分析pylint, c++代码有非常有名的代码分析工具pc-lint
4)写完代码必须要进行单元测试,python代码可以使用标准库里面提供的unittest来进行测试,c++可以使用google test框架来测试。
下面是对上面test.py编写的单元测试代码:
import unittest
import test
class TestTest(unittest.TestCase):
"""Test Cases"""
def setUp(self):
pass
def tearDown(self):
pass
def testmyprint(self):
test.myprint(1)
def testmysum(self):
self.assertEquals(test.mysum(10, 20),30,'mysum')
为了支持更多的代码自动化单元测试,在测试模块中(主要是__init__文件)添加如下的代码
def suite():
"""
Return the test suite
it should in Module File __init__
"""
tests = ['testmyprint','testmysum']
return unittest.TestSuite(map(TestTest, tests))
然后在最上面的一个python文件,假设为testAll.py文件中添加如下代码:
import unittest
import testTest
suite1 = testTest.suite()
suite = unittest.TestSuite()
suite.addTest(suite1)
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite)
这假设的是引入模块testTest, 还可以引入其他的不同的模块,这样将所有的测试case都包括进来了
最后在命令行中输入python testAll.py就可以测试所有的测试case了
D:\Works\Eclipse\FirstPython\test>python testAll.py
testmyprint (testTest.TestTest) ... item= 10
item= 11
item= 12
item= 13
item= 14
item= 15
item= 16
item= 17
item= 18
item= 19
ok
testmysum (testTest.TestTest) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.016s
OK
5)将单元测试通过的话基本上就没有什么问题了,但是不一定保证写出的代码是最优的,所有有些时候我们需要进行调优,这样我们使用python的profiler来分析程序的执行性能
首先我们执行后python -m cProfile test.py -o test.out
就是获取test.py的执行性能结果,并且将结果输出到test.out文件中
然后使用pstats来获取test.out中的性能数据,可以使用下面的代码来解析数据:
"""Get and analyze the cProfile output file """
import pstats
import sys
if len(sys.argv) != 2:
print '%s <profile_out_file>' % __file__
sys.exit()
else:
FILENAME = sys.argv[1]
PROFILE = pstats.Stats(FILENAME)
PROFILE.sort_stats('time').print_stats()
这样可以看到按执行时间排列的函数调用
下面是解析数据之后的结果
Thu Apr 02 20:39:03 2009 test.out
26 function calls in 0.012 CPU seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
10 0.011 0.001 0.011 0.001 test.py:10(myprint)
1 0.001 0.001 0.012 0.012 {execfile}
1 0.000 0.000 0.011 0.011 test.py:5(<module>)
1 0.000 0.000 0.011 0.011 test.py:16(main)
11 0.000 0.000 0.000 0.000 {range}
1 0.000 0.000 0.012 0.012 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
最后,在确认程序运行无误,并且性能能够达到要求的情况下,将代码提交到版本控制服务器中去,如SVN,这样完成了一个软件开发的过程,进行下一个循环吧!
祝你好运,呵呵