Unit testing Bottle app with WebTest

2019-09-14 05:28发布

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.

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.)

>>> 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)

2条回答
冷血范
2楼-- · 2019-09-14 05:54

As @ron.rothman mentions, the problem is that you are trying to wrap a method inside TestApp instead of the application.

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.

Fix-

Make the following changes-

  1. Add these first two lines before you page() method-

    app = Bottle()
    @app.get('/')
    def page():
        letters = scorer.get_letter_set()
    ...
    
  2. Make sure you save the file containing your above code as mywebapp.py

  3. In your unit test code, write the wrapper line like this-

    def test_functional_login_logout():
        app = TestApp(mywebapp.app)
    ...
    
查看更多
一夜七次
3楼-- · 2019-09-14 06:05

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.

So, something like this should fix it up:

app = TestApp(board.app)
查看更多
登录 后发表回答