Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 772
Eric Hydrick Avatar asked Jun 19 '12 23:06

Eric Hydrick


People also ask

How do I run a unit test in flask?

If you want to run the unit test that was just created, the easiest way is to just execute that file: . Please take note of the directory that the unit test was run from (top-level folder of the Flask application). Why can’t we just run from the directory where the unit test file is located?

What is the latest version of flask for testing?

Testing Flask Applications Warning:This is an old version. The latest stable version is Version 2.1.x. Testing Flask Applications¶ Flask provides utilities for testing an application. This documentation goes over techniques for working with different parts of the application in tests. We will use the pytestframework to set up and run our tests.

How do I send a test request in flask?

Sending Requests with the Test Client¶ The test client makes requests to the application without running a live server. Flask’s client extends Werkzeug’s client, see those docs for additional information. The clienthas methods that match the common HTTP request methods, such as client.get()and client.post().

What is flask-unittest and how to use it?

Flask is a backend web framework for python. One that is beloved by many python developers! But to make a great flask webapp, testing is important. I noticed the lack of a good library to integrate with the stdlib unittest for testing flask apps. So I made one myself! Check out flask-unittest on github!


1 Answers

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()
like image 106
Mark Hildreth Avatar answered Oct 05 '22 05:10

Mark Hildreth