Skip to content Skip to sidebar Skip to footer

Actually Testing My Constructed Application (Flask, Python)

If I have an application built, what is the protocol for testing the actual application? I'm just understanding testing, and a test for an extension where you'd construct a shell

Solution 1:

There are many ways to test your application.

Flask's documentation provides information on how to initlialize your app and make requests to it:

import flaskr

class FlaskrTestCase(unittest.TestCase):

    def setUp(self):
        self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
        self.app = flaskr.app.test_client()
        flaskr.init_db()

    def tearDown(self):
        os.close(self.db_fd)
        os.unlink(flaskr.DATABASE)

    def test_empty_db(self):
        rv = self.app.get('/')
        assert 'No entries here so far' in rv.data

This allows you to request any route using their test_client. You can request every route in your application and assert that it is returning the data you expect it to.

You can write tests to do test specific functions too, Just import the function and test it accordingly.

You shouldn't have to write "a separate test application" at all. However, you might have to do things like loading test data, mocking objects/classes, these are not very straightforward, but there exist many blog posts/python tools that will help you do them

I would suggest reading the flask testing documentation to start with, They provide a great overview.

Additionally, it would be extremely helpful for you to provide specifics.

What I've tried so far (importing the app into a test and starting to build tests for it) has been both unpleasant and unsuccessful

Is not that constructive. Are you getting errors when you execute your tests? If so could you post them. How is it unsuccessful? Do you not know which parts to test? Are you having problems executing your tests? Loading data? Testing the responses? Errors?


Post a Comment for "Actually Testing My Constructed Application (Flask, Python)"