我从其他讨论中看到,重新加载被认为是一种不必要的操作,也是开发程序的一种糟糕方式。人们说使用doctest和unittest。我肯定错过了什么。要开发一个程序,我将它写在一个模块中,将模块加载到eclipse控制台,通过运行少量代码示例在控制台中进行实验,然后我发现模块中的错误或决定更改某些内容。当然,最快的方法是保存模块,重新加载并继续在控制台中工作 有没有更好的办法?
答案 0 :(得分:1)
基本上不是在控制台中尝试,而是通过编写测试来尝试。它的输入量几乎相同,但可重复(您可以检查更改后是否仍然有效)并且它可以作为一种基本的文档形式。
答案 1 :(得分:0)
这是一个人为的例子,说明如何使用doctest和测试驱动开发,通过在控制台输入代码进行测试,并以递归因子函数为例:
控制台:
首次尝试:
def factorial(x)
pass
控制台:
>>> factorial(7)
SyntaxError: invalid syntax
第二次尝试:
def factorial(x):
return x*x-1
控制台:
>>> factorial(7)
42
第三次尝试:
def factorial(x):
return x * factorial(x-1)
控制台:
>>> factorial(7)
RuntimeError: maximum recursion depth reached # StackOverflow! (basically)
第四次尝试:
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x-1)
控制台:
>>> factorial(5)
120
最后,我们得到了正确的答案,但在每个阶段我们都必须回到控制台并编写相同的内容。这对于像这样的短程序来说是好的,但是对于具有许多功能的程序,以及更复杂的交互和可能性进行测试,这将花费很长时间。编程是关于自动执行重复性任务,而测试是一项重复性任务,因此将其自动化是有意义的。在Python中,为您提供了这样做的工具。
看哪 - doctest模块:(来自文档的例子)
首次尝试:
def factorial(n):
"""Return the factorial of n, an exact integer >= 0.
>>> [factorial(n) for n in range(6)]
[1, 1, 2, 6, 24, 120]
>>> factorial(30)
265252859812191058636308480000000
>>> factorial(-1)
Traceback (most recent call last):
...
ValueError: n must be >= 0
Factorials of floats are OK, but the float must be an exact integer:
>>> factorial(30.1)
Traceback (most recent call last):
...
ValueError: n must be exact integer
>>> factorial(30.0)
265252859812191058636308480000000
It must also not be ridiculously large:
>>> factorial(1e100)
Traceback (most recent call last):
...
OverflowError: n too large
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
首先测试(只运行程序):
Trying:
[factorial(n) for n in range(6)]
Expecting:
[1, 1, 2, 6, 24, 120]
ok
Trying:
factorial(30)
Expecting:
265252859812191058636308480000000
ok
Trying:
factorial(-1)
Expecting:
Traceback (most recent call last):
...
ValueError: n must be >= 0
ok
Trying:
factorial(30.1)
Expecting:
Traceqwrqaeqrback (most recent call last):
...
ValueError: n must be exact integer
**********************************************************************
File "C:/Python33/doctestex.py", line 14, in __main__.factorial
Failed example:
factorial(30.1)
Exception raised:
Traceback (most recent call last):
File "C:\Python33\lib\doctest.py", line 1287, in __run
compileflags, 1), test.globs)
File "<doctest __main__.factorial[3]>", line 1, in <module>
factorial(30.1)
File "C:/Python33/doctestex.py", line 32, in factorial
raise ValueError("n must be exact integer")
ValueError: n must be exact integer
Trying:
factorial(30.0)
Expecting:
265252859812191058636308480000000
ok
Trying:
factorial(1e100)
Expecting:
Traceback (most recent call last):
...
OverflowError: n too large
ok
1 items had no tests:
__main__
**********************************************************************
1 items had failures:
1 of 6 in __main__.factorial
6 tests in 2 items.
5 passed and 1 failed.
***Test Failed*** 1 failures.
>>> ================================ RESTART ================================
>>>
Trying:
[factorial(n) for n in range(6)]
Expecting:
[1, 1, 2, 6, 24, 120]
**********************************************************************
File "C:/Python33/doctestex.py", line 4, in __main__.factorial
Failed example:
[factorial(n) for n in range(6)]
Expected:
[1, 1, 2, 6, 24, 120]
Got:
[None, None, None, None, None, None]
Trying:
factorial(30)
Expecting:
265252859812191058636308480000000
**********************************************************************
File "C:/Python33/doctestex.py", line 6, in __main__.factorial
Failed example:
factorial(30)
Expected:
265252859812191058636308480000000
Got nothing
Trying:
factorial(-1)
Expecting:
Traceback (most recent call last):
...
ValueError: n must be >= 0
**********************************************************************
File "C:/Python33/doctestex.py", line 8, in __main__.factorial
Failed example:
factorial(-1)
Expected:
Traceback (most recent call last):
...
ValueError: n must be >= 0
Got nothing
Trying:
factorial(30.1)
Expecting:
Traceback (most recent call last):
...
ValueError: n must be exact integer
**********************************************************************
File "C:/Python33/doctestex.py", line 14, in __main__.factorial
Failed example:
factorial(30.1)
Expected:
Traceback (most recent call last):
...
ValueError: n must be exact integer
Got nothing
Trying:
factorial(30.0)
Expecting:
265252859812191058636308480000000
**********************************************************************
File "C:/Python33/doctestex.py", line 18, in __main__.factorial
Failed example:
factorial(30.0)
Expected:
265252859812191058636308480000000
Got nothing
Trying:
factorial(1e100)
Expecting:
Traceback (most recent call last):
...
OverflowError: n too large
**********************************************************************
File "C:/Python33/doctestex.py", line 22, in __main__.factorial
Failed example:
factorial(1e100)
Expected:
Traceback (most recent call last):
...
OverflowError: n too large
Got nothing
1 items had no tests:
__main__
**********************************************************************
1 items had failures:
6 of 6 in __main__.factorial
6 tests in 2 items.
0 passed and 6 failed.
***Test Failed*** 6 failures.
然后,当我们完成程序(以及之间的所有阶段)时,我们只需按一个按钮即可完成我们的所有测试。如果您正在与一个更大项目的团队合作,您可以将测试数据写入文件并使用失败来查看您需要关注的位置。
Trying:
[factorial(n) for n in range(6)]
Expecting:
[1, 1, 2, 6, 24, 120]
ok
Trying:
factorial(30)
Expecting:
265252859812191058636308480000000
ok
Trying:
factorial(-1)
Expecting:
Traceback (most recent call last):
...
ValueError: n must be >= 0
ok
Trying:
factorial(30.1)
Expecting:
Traceback (most recent call last):
...
ValueError: n must be exact integer
ok
Trying:
factorial(30.0)
Expecting:
265252859812191058636308480000000
ok
Trying:
factorial(1e100)
Expecting:
Traceback (most recent call last):
...
OverflowError: n too large
ok
1 items had no tests:
__main__
1 items passed all tests:
6 tests in __main__.factorial
6 tests in 2 items.
6 passed and 0 failed.
Test passed.
并且(如果您将所有测试用例添加到文档字符串中)现在您可以发送或集成任何程序,并且如果您在每次更改时只测试了您认为重要的事情,那么它将会出现更少的错误事情,而不是你认为在开发过程开始时可能会发生的事情。
更不用说您现在拥有文档的基础!在控制台中运行程序,然后输入help(factorial)
现在将为您提供:
Help on function factorial in module __main__:
factorial(n)
Return the factorial of n, an exact integer >= 0.
>>> [factorial(n) for n in range(6)]
[1, 1, 2, 6, 24, 120]
>>> factorial(30)
265252859812191058636308480000000
>>> factorial(-1)
Traceback (most recent call last):
...
ValueError: n must be >= 0
Factorials of floats are OK, but the float must be an exact integer:
>>> factorial(30.1)
Traceback (most recent call last):
...
ValueError: n must be exact integer
>>> factorial(30.0)
265252859812191058636308480000000
It must also not be ridiculously large:
>>> factorial(1e100)
Traceback (most recent call last):
...
OverflowError: n too large
然后,您可以使用许多工具(pydoc是标准库中的一个)将文档字符串转换为格式化的HTML帮助文件。
当然,这只是可以与Python一起使用的众多测试工具之一。其他包括更强大的unittest模块,以及在代码中添加assert
语句的功能较弱的技术。