blueblank
blueblank

Reputation: 4904

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 application and then test your extension makes sense to me, but not if I want to test parts of an actual application I'm constructing.

I'm wondering if someone has any pointers, guides, or thoughts on how they go about packaging and testing their flask applications. 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. I'm at a point where I know I need the application to X,Y,Z and I can save a lot of future time by building a test that ensures X,Y,Z happens. however building a separate test application would be time costly and unproductive it would seem.

Upvotes: 3

Views: 402

Answers (1)

dm03514
dm03514

Reputation: 55972

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?

Upvotes: 3

Related Questions