[英]Unit testing Bottle app with 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.
我有一个尝试使用Unittest的瓶子应用程序,这似乎不合适,所以现在我正在尝试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. 麻烦的是,即使遵循该站点上最基本/最肤浅的示例,我也无法远程工作。
Here's the example: 例子如下:
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'
my code: 我的代码:
@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))
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.) 然后,在控制台中(一个问题是,我似乎无法从文件中获取任何测试代码。如果我在项目目录中运行任何文件,则bottle将改为运行开发服务器。任何尝试运行测试文件的结果导入错误。)
>>> from webtest import TestApp
>>> import board
>>> app = TestApp(board.page)
>>> res = app.get('/')
I get this error: 我收到此错误:
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)
As @ron.rothman mentions, the problem is that you are trying to wrap a method inside TestApp instead of the application. 正如@ ron.rothman所提到的那样,问题在于您试图将方法包装在TestApp中而不是应用程序中。
And from your code- 从您的代码中
@get('/')
def page():
letters = scorer.get_letter_set()
...
its evident that you are using the default application instead of creating an instance of the Bottle yourself. 很明显,您使用的是默认应用程序,而不是自己创建Bottle的实例。
Fix- 固定-
Make the following changes- 进行以下更改-
Add these first two lines before you page() method- 在page()方法之前添加以下两行:
app = Bottle() @app.get('/') def page(): letters = scorer.get_letter_set() ...
Make sure you save the file containing your above code as mywebapp.py 确保将包含上述代码的文件另存为mywebapp.py
In your unit test code, write the wrapper line like this- 在单元测试代码中,这样编写包装行:
def test_functional_login_logout(): app = TestApp(mywebapp.app) ...
The problem is here: 问题在这里:
app = TestApp(board.page)
Instead, you need to wrap TestApp around your Bottle app, which I'm presuming (since you didn't show it) lives in board.py
. 取而代之的是,您需要将TestApp封装在Bottle应用程序周围,我假设(因为您没有显示它)
board.py
在board.py
。
So, something like this should fix it up: 因此,这样的事情应该解决它:
app = TestApp(board.app)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.