vendredi 29 mai 2015

Flask server returns 404 when attempting unit tests

I'm trying to start doing unit testing on my flask app before I add any more functionality, and I've been stuck at the starting line for longer than I'd like to admit. I'm using Flask-Testing (latest master from their repo). I'm also using LiveServerTestCase because I'd like to use selenium live testing. An example of the setup guide was provided on the flask docs website which seemed simple enough.

I have tried so many code variations that have not worked but I'll post the most recent iteration.

Here is the error message that I keep getting:

(venv)username@username-VM64:~/git/approot$ python ./seleniumtests.py
 * Running on http://127.0.0.1:8943/ (Press CTRL+C to quit)
127.0.0.1 - - [29/May/2015 20:01:12] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [29/May/2015 20:01:13] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [29/May/2015 20:01:14] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [29/May/2015 20:01:15] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [29/May/2015 20:01:16] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [29/May/2015 20:01:17] "GET / HTTP/1.1" 404 -
E
======================================================================
ERROR: test_server_is_up_and_running (__main__.BaseTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./seleniumtests.py", line 23, in test_server_is_up_and_running
    response = urllib2.urlopen(self.get_server_url())
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: NOT FOUND

----------------------------------------------------------------------
Ran 1 test in 5.093s

FAILED (errors=1)

This is just the basic server test, and it appears that the app instance doesn't exist even though it looks like its running at the start.

Here is the seleniumtests.py

import urllib2, os, unittest
from app import models, db, app, views
from flask import Flask
from flask_testing import LiveServerTestCase

class BaseTestCase(LiveServerTestCase):

    def create_app(self):
        app = Flask(__name__)
        app.config.from_object('config.TestConfiguration')
        return app

    def test_server_is_up_and_running(self):
        response = urllib2.urlopen(self.get_server_url())
        self.assertEqual(response.code, 200)

    def setUp(self):
        db.create_all()

    def tearDown(self):
        db.session.remove()
        db.drop_all()



if __name__ == '__main__':
    unittest.main()

this is the config i'm using stored in config.py

class TestConfiguration(BaseConfiguration):
    TESTING = True
    DEBUG = True
    WTF_CSRF_ENABLED = False
    SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
    LIVESERVER_PORT = 8943

This is what my folder structure looks like, if that helps.

.
├── app
│   ├── forms.py
│   ├── __init__.py
│   ├── models.py
│   ├── momentjs.py
│   ├── static
│   ├── templates
│   └── views.py
├── config.py
├── __init__.py
├── README.md
├── requirements
├── run.py
├── seleniumtests.py
├── tmp
│   ├── cover
│   └── coverage
├── unittests.py
└── venv

Any insight would be helpful. I was finally getting the hang of flask (and feeling good about it) until I hit automated testing.

Aucun commentaire:

Enregistrer un commentaire