|
Zoom.Quiet
v10.8.26 |
|
伙同所有同好行者总结而得
Test-driven development:by example
$(document).ready(function() {
$("img[class='attachment']").each(function(){
$(this).attr("usemap","#"+$(this).attr("alt"));
});
});
起意...
# -*- coding: utf-8 -*-
'''判定是否素数
>>> chkPrime(5)
True
>>> chkPrime(4)
False
'''
def chkPrime(N):
pass
if __name__ == '__main__':# this way the module can be
import doctest
doctest.testmod()
败!先...
$ python primenu.py
**********************************************************************
File "primenu.py", line 3, in __main__
Failed example:
chkPrime(5)
Expected:
True
Got nothing
**********************************************************************
File "primenu.py", line 5, in __main__
Failed example:
chkPrime(4)
Expected:
False
Got nothing
**********************************************************************
1 items had failures:
2 of 2 in __main__
***Test Failed*** 2 failures.
IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]: %doctest_mode
*** Pasting of code with ">>>" or "..." has been enabled.
Exception reporting mode: Plain
Doctest mode is: ON
>>> %
%Exit %debug %logstop %prun %runlog
%Pprint %dhist %lsmagic %psearch %save
%Quit %dirs %macro %psource %sc
%alias %doctest_mode %magic %pushd %store
%autocall %ed %p %pwd %sx
%autoindent %edit %page %pycat %system_verbose
%automagic %env %paste %quickref %time
%bg %exit %pdb %quit %timeit
%bookmark %hist %pdef %r %unalias
%cd %history %pdoc %rehash %upgrade
%clear %logoff %pfile %rehashx %who
%color_info %logon %pinfo %rep %who_ls
%colors %logstart %popd %reset %whos
%cpaste %logstate %profile %run %xmode
>>> N=5 >>> N%4 1 >>> N%5 0 >>> range(2,5) [2, 3, 4] >>> [N%d for d in range(2,N)] [1, 2, 1] >>> 0 not in [N%d for d in range(2,N)] True >>> N =4 >>> 0 not in [N%d for d in range(2,N)] False
完善代码
# -*- coding: utf-8 -*-
'''判定是否素数
>>> chkPrime(5)
True
>>> chkPrime(4)
False
'''
def chkPrime(N):
return 0 not in [N%d for d in range(2,N)]
if __name__ == '__main__':# this way the module can be
import doctest
doctest.testmod()
通过测试!
$ python primenu.py
$ python primenu.py -v
Trying:
chkPrime(5)
Expecting:
True
ok
Trying:
chkPrime(4)
Expecting:
False
ok
1 items had no tests:
__main__.chkPrime
1 items passed all tests:
2 tests in __main__
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
需求变更
# -*- coding: utf-8 -*-
'''判定是否素数
>>> chkPrime(5)
True
>>> chkPrime(4)
False
# 找出所有素数
>>> foundPrime(10)
[2, 3, 5, 7]
'''
def foundPrime(N):
pass
def chkPrime(N):
return 0 not in [N%d for d in range(2,N)]
if __name__ == '__main__':# this way the module can be
import doctest
doctest.testmod()
当然要败!
$ python primenu.py
**********************************************************************
File "primenu.py", line 9, in __main__
Failed example:
foundPrime(10)
Expected:
[2, 3, 5, 7]
Got nothing
**********************************************************************
1 items had failures:
1 of 3 in __main__
***Test Failed*** 1 failures.
>>> def chkPrime(N):return 0 not in [N%d for d in range(2,N)] ... >>> N=5 >>> chkPrime(N) True >>> [P for P in range(2,N) if chkPrime(P)] [2, 3] >>> def foundPrime(N): ... return [P for P in range(2,N) if chkPrime(P)] ... >>> foundPrime(5) [2, 3] >>> foundPrime(10) [2, 3, 5, 7]
增补代码
# -*- coding: utf-8 -*-
'''判定是否素数
>>> chkPrime(5)
True
>>> chkPrime(4)
False
# 找出所有素数
>>> foundPrime(10)
[2, 3, 5, 7]
'''
def foundPrime(N):
return [P for P in range(2,N) if chkPrime(P)]
def chkPrime(N):
return 0 not in [N%d for d in range(2,N)]
if __name__ == '__main__':# this way the module can be
import doctest
doctest.testmod()
再次成功!
$ python primenu.py
$ python primenu.py -v
Trying:
chkPrime(5)
Expecting:
True
ok
Trying:
chkPrime(4)
Expecting:
False
ok
Trying:
foundPrime(10)
Expecting:
[2, 3, 5, 7]
ok
2 items had no tests:
__main__.chkPrime
__main__.foundPrime
1 items passed all tests:
3 tests in __main__
3 tests in 3 items.
3 passed and 0 failed.
Test passed.
对应到各个函式的聲明区
# -*- coding: utf-8 -*-
'''素数相关模块
'''
def chkPrime(N):
'''判定是否素数
>>> chkPrime(5)
True
>>> chkPrime(4)
False
'''
return 0 not in [N%d for d in range(2,N)]
def foundPrime(N):
'''找出所有素数
>>> foundPrime(10)
[2, 3, 5, 7]
'''
return [P for P in range(2,N) if chkPrime(P)]
if __name__ == '__main__': # this way the module can be
import doctest
doctest.testmod()
测试相应变化
$ python primenu.py -v
Trying:
chkPrime(5)
Expecting:
True
ok
Trying:
chkPrime(4)
Expecting:
False
ok
Trying:
foundPrime(10)
Expecting:
[2, 3, 5, 7]
ok
1 items had no tests:
__main__
2 items passed all tests:
2 tests in __main__.chkPrime
1 tests in __main__.foundPrime
3 tests in 3 items.
3 passed and 0 failed.
Test passed.
< 专有/>
< 开源/>

文档化测试
doctest写测试非常爽:)
list of:
+------+ +---------+
|module| --DocTestFinder-> | DocTest | --DocTestRunner-> results
+------+ | ^ +---------+ | ^ (printed)
| | | Example | | |
v | | ... | v |
DocTestParser | Example | OutputChecker
+---------+
测试框架
测试的目标忒多:
幸福的给劲模块s!
< Discussing>
< 参考/>
$(document).ready(function() {
$("img[class='attachment']").each(function(){
$(this).attr("usemap","#"+$(this).attr("alt"));
});
});
< 版本 />
$(document).ready(function() {
$("img[class='attachment']").each(function(){
$(this).attr("usemap","#"+$(this).attr("alt"));
});
});