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)
The problem is here:
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: