我一直試圖熟悉單元測試,但一直有很多麻煩。我有一個瓶子應用程序,我嘗試使用Unittest,這似乎不合適,所以現在我正在嘗試WebTest。WebTest單元測試瓶應用程序
麻煩的是,我甚至無法讓它遠程工作,甚至跟隨着網站上最基本/膚淺的例子。
這裏的例子:
from webtest import TestApp
import mywebapp
def test_functional_login_logout():
app = TestApp(mywebapp.app)
app.post('/login', {'user': 'foo', 'pass': 'bar'}) # log in and get a cookie
assert app.get('/admin').status == '200 OK' # fetch a page successfully
app.get('/logout') # log out
app.reset() # drop the cookie
# fetch the same page, unsuccessfully
assert app.get('/admin').status == '401 Unauthorized'
我的代碼:
@get('/')
def page():
letters = scorer.get_letter_set()
c = db_connect()
c.execute('SELECT player_name,score FROM Scores order by score DESC limit 5')
data = c.fetchall()
c.close()
return template('board', letters=letters, scores=data, letterset=json.dumps(letters))
然後,在控制檯(一個問題是,我似乎無法得到任何測試代碼從文件工作。如果我在我的項目目錄中運行任何文件,bottle會運行開發服務器,而不是運行測試文件的任何嘗試都會導致導入錯誤。)
>>> from webtest import TestApp
>>> import board
>>> app = TestApp(board.page)
>>> res = app.get('/')
我得到這個錯誤:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/webtest/app.py", line 322, in get
expect_errors=expect_errors)
File "/usr/local/lib/python2.7/dist-packages/webtest/app.py", line 605, in do_request
res = req.get_response(app, catch_exc_info=True)
File "/usr/local/lib/python2.7/dist-packages/webob/request.py", line 1313, in send
application, catch_exc_info=True)
File "/usr/local/lib/python2.7/dist-packages/webob/request.py", line 1281, in call_application
app_iter = application(self.environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/webtest/lint.py", line 198, in lint_app
iterator = application(environ, start_response_wrapper)
TypeError: page() takes no arguments (2 given)
哪裏是在你的代碼的應用程序瓶? 'TestApp'是一個包裝,而不是一個函數。 –