问题描述
我一直在尝试使自己熟悉单元测试,但是却遇到了很多麻烦.我有一个尝试使用Unittest的瓶子应用程序,这似乎不合适,所以现在我正在尝试WebTest.
I've been attempting to familiarize myself with unit testing but have been having a lot of trouble with it. I have a bottle app that I tried using Unittest, which didn't seem appropriate, so now I'm trying WebTest.
问题在于,即使遵循该站点上最基本/最肤浅的示例,我也无法远程操作它.
The trouble is that I can't get it to even remotely work, even following along with the most basic/superficial example on the site.
这里是例子:
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将改为运行开发服务器.任何尝试运行测试文件会导致导入错误.)
Then, in the console (one problem is that I can't seem to get any testing code to work from a file. If I run any file in my project directory, bottle runs the development server instead. Any attempt to run test files results in import errors.)
>>> 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)
推荐答案
正如@ ron.rothman所提到的那样,问题在于您试图将方法包装在TestApp中而不是应用程序中.
As @ron.rothman mentions, the problem is that you are trying to wrap a method inside TestApp instead of the application.
根据您的代码-
@get('/')
def page():
letters = scorer.get_letter_set()
...
很明显,您使用的是默认应用程序,而不是自己创建Bottle的实例.
its evident that you are using the default application instead of creating an instance of the Bottle yourself.
修复-
进行以下更改-
-
在page()方法之前添加前两行-
Add these first two lines before you page() method-
app = Bottle()
@app.get('/')
def page():
letters = scorer.get_letter_set()
...
确保将包含上述代码的文件另存为mywebapp.py
Make sure you save the file containing your above code as mywebapp.py
在单元测试代码中,这样编写包装行-
In your unit test code, write the wrapper line like this-
def test_functional_login_logout():
app = TestApp(mywebapp.app)
...
这篇关于使用WebTest进行单元测试Bottle应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!