dennismonsewicz
dennismonsewicz

Reputation: 25552

Newb to Writing Integration Tests

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

Upvotes: 1

Views: 1029

Answers (1)

RohitJ
RohitJ

Reputation: 563

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

Upvotes: 1

Related Questions