3

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.

1 Answer 1

3

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, my question was vague, and I know that can be problematic for some. However, you can get great responses sometimes by lowballing a question content wise. I had read the flask documentation and I am seeking more information, but my question existed only as part of me trying to wrap my brain around something I need to do -- basically "take my app, and poke it with tests, without constructing a completely separate app for testing"
@blueblank - dm03514 isn't suggesting building another app completely for testing - he's suggesting importing your existing app into your tests and using the app's built-in test_client to test it. What's missing from the example is the import flaskr (which, in this example is the application under test).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.