選擇框架之前首先要確認一下我們的選項概念和她們之間的區別。
PyUnit
PyUnit(The Python unit testing framework) 是 Kent Beck 和 Erich Gamma 所設計的 JUnit 的 Python 版本。從 Python 2.1 版本后,PyUnit 成為 Python 標準庫的一部分。
這就是我們日常使用的 unittest 。它太過優秀了,以至于我們幾乎沒有必要再用別的測試框架了。
下面這個例子,是bottle框架下測試首頁的簡單例子,首先是bottle的代碼:
#!/usr/bin/env python
# encoding: utf-8
import bottle
@bottle.route('/')
def index():
return 'Hi!'
if __name__ == '__main__':
bottle.run()
這是單元測試的代碼:
#!/usr/bin/env python
# encoding: utf-8
import mywebapp
import unittest
class TestBottle(unittest.TestCase):
def test_webapp_index(self):
assert mywebapp.index() == 'Hi!'
if __name__ == '__main__':
unittest.main()
直接運行這個文件就可以了,可以看到在普通模式,和verbosity模式下的返回結果:
普通模式:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
verbosity模式:
test_webapp_index (__main__.TestBottle) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
表示我們的測試都通過了。