简体   繁体   中英

Flask app wrapped with DispatcherMiddleware no longer has test_client

We can obtain test_client for sample application in way like:

class MyTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        my_app.app.config['TESTING'] = True
        cls.client = my_app.app.test_client()

However, if we wrap app with DispatcherMiddleware - we will get error like AttributeError: 'DispatcherMiddleware' object has no attribute 'test_client' .

Are there way to test composition of flask applications?

We want to be able to do something like:

cls.client = my_app.all_apps.test_client()

When all_apps is middleware like:

all_apps = DispatcherMiddleware(my_app, {
    '/backend': backend_app,
})

To add WSGI middleware to a Flask app, wrap and replace the app's wsgi_app attribute. You're replacing the reference to the Flask app with a reference to some other WSGI app, which obviously won't have the same properties. By replacing wsgi_app , you retain the reference to the Flask app but change the WSGI callable that backs it.

app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {
    '/backend': backend_app.wsgi_app,
})

I was looking to use the test_client on both of the bundled apps. Here is a modification to davidism's answer that allowed me to use test clients for both:

app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {
    '/backend': backend_app.wsgi_app
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM