title: Unit testing a Flask application class
body: <p>I'm fairly new to Python, so my apologies in advance if this is much ado for something basic.</p>
  
  <p>I have situation similar to <a href="https://stackoverflow.com/questions/5025720/how-do-you-set-up-a-flask-application-with-sqlalchemy-for-testing">How do you set up a Flask application with SQLAlchemy for testing?</a> 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:</p>
  
  <p><strong>Application:</strong></p>
  
  <pre><code>from Flask import Flask
  
  app = Flask(__name__)
  
  class MyApplication():
      def __init__(self, param1, param2):
          app.add_url("/path/&lt;methodParam&gt;", "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.
  </code></pre>
  
  <p><strong>Application Tests:</strong></p>
  
  <pre><code>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)
  </code></pre>
  
  <p>When I run my tests via </p>
  
  <blockquote>
    <p>nosetests --with-coverage --cover-package apppackage
    ./test/test_application.py</p>
  </blockquote>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>param2).app.test_client() AttributeError: MyApplication instance has
    no attribute 'app'</p>
  </blockquote>
  
  <p>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?</p>