1

Here is my test file:

from flask import Flask
from flask.ext.testing import TestCase


class TestInitViews(TestCase):

    render_templates = False

    def create_app(self):
        app = Flask(__name__)
        app.config['TESTING'] = True
        return app

    def test_root_route(self):
        self.client.get('/')
        self.assert_template_used('index.html')

Here is the full stack trace:

$ nosetests tests/api/client/test_init_views.py
F
======================================================================
FAIL: test_root_route (tests.api.client.test_init_views.TestInitViews)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/dmonsewicz/dev/autoresponders/tests/api/client/test_init_views.py", line 17, in test_root_route
    self.assert_template_used('index.html')
  File "/Users/dmonsewicz/.virtualenvs/autoresponders-api/lib/python2.7/site-packages/flask_testing.py", line 120, in assertTemplateUsed
    raise AssertionError("template %s not used" % name)
AssertionError: template index.html not used

----------------------------------------------------------------------
Ran 1 test in 0.012s

FAILED (failures=1)

I am a newb to Python and can't seem to figure this one out. All I am trying to do is write a simple test that hits the / (root route) endpoint and asserts that the template used was in fact index.html

Attempt at using LiveServerTestCase

from flask import Flask
from flask.ext.testing import LiveServerTestCase


class TestInitViews(LiveServerTestCase):

    render_templates = False

    def create_app(self):
        app = Flask(__name__)
        app.config['TESTING'] = True
        app.config['LIVESERVER_PORT'] = 6765

        return app

    def setUp(self):
        self.app = self.app.test_client()

    def test_root_route(self):
        res = self.app.get('/')

        print(res)

        self.assert_template_used('index.html')

I'm using Flask-Testing version 0.4 and for some reason the LiveServerTestCase doesn't exist in my import

Working Code

from flask import Flask
from flask.ext.testing import TestCase
from api.client import blueprint


class TestInitViews(TestCase):

    render_templates = False

    def create_app(self):
        app = Flask(__name__)
        app.config['TESTING'] = True

        app.register_blueprint(blueprint)

        return app

    def test_root_route(self):
        res = self.client.get('/')
        self.assert_template_used('index.html')
Community
  • 1
  • 1
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189

1 Answers1

1

You have to run pip install blinker and make sure your flask version is greater than .6.

It looks like you omitted setting app.config['TESTING'] = True

I was able to get the following test to run to validate the assert was True:

#!/usr/bin/python

import unittest
from flask import Flask
from flask.ext.testing import TestCase
from flask import render_template


class MyTest(TestCase):

  def create_app(self):
    app = Flask(__name__)
    app.config['TESTING'] = True

    @app.route('/')
    def hello_world():
      return render_template('index.html')
    return app

  def test_root_route(self):
    self.client.get('/')
    self.assert_template_used('index.html')

if __name__ == '__main__':
  unittest.main()
RohitJ
  • 543
  • 4
  • 8
  • Thanks for your answer! I have updated my question with what I am getting now... for some reason when the `self.client.get('/')` runs, a 404 is returned. – dennismonsewicz Jun 14 '14 at 02:18
  • I think you'll need to have a function with the decorator app.route('/'). See the hello world function that I placed before returning app. You will also need to create a templates directory with index.html. Place it in the same directory as your test. – RohitJ Jun 14 '14 at 02:20
  • So, it won't physically visit the `/` route and check that the layout rendered was index.html? I come from a `Rails` background, so please excuse my ignorance – dennismonsewicz Jun 14 '14 at 02:21
  • Also, where did you place your template file? – dennismonsewicz Jun 14 '14 at 02:24
  • 1
    There are multiple ways to test. In this case, you aren't hitting the live server but just testing the logic. You can also test with the live server. You will want to use the **LiveServerTestCase**. See the section entitled "Testing with LiveServer" in the flask testing documentation for more help: http://flask-testing.readthedocs.org/en/latest/ – RohitJ Jun 14 '14 at 02:27
  • You need a child directory called "templates" in which you can put your index.html. It looks for a templates directory by default. – RohitJ Jun 14 '14 at 02:28
  • I tried extending the *LiveServerTestCase* but it looks like it no longer exists... :( – dennismonsewicz Jun 14 '14 at 02:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55615/discussion-between-rohitj-and-dennismonsewicz). – RohitJ Jun 14 '14 at 02:34
  • The LiveServerTestCase class will be in the same module as the TestCase class. You could locate utils.py and see if it exists in there. – RohitJ Jun 14 '14 at 03:02
  • Having my route inside of a Blueprint wouldn't have anything to do with why it's not working, would it? – dennismonsewicz Jun 14 '14 at 03:06