Eric Hydrick
Eric Hydrick

Reputation: 3537

Unit testing a Flask application class

I'm fairly new to Python, so my apologies in advance if this is much ado for something basic.

I have situation similar to How do you set up a Flask application with SQLAlchemy for testing? The big difference for me is that unlike most other Flask examples I see on the Internet, most of the code I have for my application is in a class. For some reason, this is causing my unit testing to not work correctly. Below is a basic setup of my application and tests:

Application:

from Flask import Flask

app = Flask(__name__)

class MyApplication():
    def __init__(self, param1, param2):
        app.add_url("/path/<methodParam>", "method1", self.method1, methods=["POST"])
        # Initialize the app

    def getApplication(self):
       options = # application configuration options
       middleware = ApplicationMiddleware(app, options)
       return middleware

    def method1(self, methodParam):
        # Does useful stuff that should be tested
    # More methods, etc.

Application Tests:

import unittest
from apppackage import MyApplication

class ApplicationTestCase(unittest.TestCase):

    def setUp(self):
        self.tearDown()
        param1 = # Param values
        param2 = # Param values
        # Other local setup 
        self.app = MyApplication(param1, param2).app.test_client()

    def tearDown(self):
       # Clean up tests

    def test_method1(self):
        methodParam = # Param value
        response = self.app.post("path/methodParam")
        assert(reponse.status_code == 200)

When I run my tests via

nosetests --with-coverage --cover-package apppackage ./test/test_application.py

I get the following error:

param2).app.test_client() AttributeError: MyApplication instance has no attribute 'app'

I've tried moving app inside the class declaration, but that doesn't do any good, and isn't how every other unit testing guide I've seen does it. Why can't my unit tests find the "app" attribute?

Upvotes: 7

Views: 6382

Answers (1)

Mark Hildreth
Mark Hildreth

Reputation: 43111

Your unit test cannot find the "app" attribute because MyApplication does not have one. There is an "app" attribute in the module where MyApplication is defined. But those are two separate places.

Perhaps try the following:

class MyApplication(object):
    def __init__(self, param1, param2):
        self.app = Flask(__name__)
        self.app.add_url("/path/<methodParam>", "method1", self.method1, methods=["POST"])
        # Initialize the app

Alternatively, you also seem to have a "getApplication" method which you aren't really doing anything with, but I imagine that you're using it for something. Perhaps you actually want this in your test...

def setUp(self):
        self.tearDown()
        param1 = # Param values
        param2 = # Param values
        # Other local setup 
        self.app = MyApplication(param1, param2).getApplication().test_client()

Upvotes: 4

Related Questions