【发布时间】:2015-06-21 20:56:22
【问题描述】:
我一直在尝试熟悉单元测试,但遇到了很多麻烦。我有一个瓶子应用程序,我尝试使用 Unittest,这似乎不合适,所以现在我正在尝试 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)
【问题讨论】:
-
您的代码中的 Bottle 应用在哪里?
TestApp是一个包装器,而不是一个函数。
标签: python unit-testing bottle webtest