jarus / flask-testing

Unittest extensions for Flask
http://pythonhosted.org/Flask-Testing/
Other
501 stars 110 forks source link

AttributeError when test client is named self.app #93

Open brandones opened 7 years ago

brandones commented 7 years ago

When the test client is named self.app, i.e. you have a situation like

    def setUp(self):
        self.app = application.test_client()

Then when assertRedirects tries to access the Flask instance at self.app it instead gets the FlaskClient instance and crashes as below.

Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "/Users/bistenes/Code/socos/jive/server/src/tests/test_pages.py", line 118, in test_logs_in_user
    self.assertRedirects(rv, '/manage')
  File "/Users/bistenes/Code/socos/jive/server/src/lib/flask_testing/utils.py", line 278, in assertRedirects
    server_name = self.app.config.get('SERVER_NAME') or 'localhost'
AttributeError: 'FlaskClient' object has no attribute 'config'

I'm pretty surprised this wasn't a problem before 0.6.0 -- you would think that resolving self.app elsewhere, e.g. for self.app.get(...) would also be a problem, but it apparently wasn't. The obvious solution is to not name the test client self.app, but rather, say, self.client.

This is mostly a request for documentation -- somewhere it should advise against doing this. It's also, of course, a note to those running into this confusing error in the future. Actual code change is probably not called for here.

ghost commented 7 years ago

+1 for self.client because it works, thanks

codeKrowe commented 6 years ago

While writing tests for my Flask app I came across a similar problem.

Only from debugging I saw that there was no "config" attribute instead I had to go self.app.application.config

Not sure why it's missing, I've usually always done self.app.config just like in the production code

    import unittest

    from factory import create_app

    class ConfigTests(unittest.TestCase):

        def setUp(self):
            app = create_app('flask_test.cfg')
            app.testing = True
            self.app = app.test_client()

        def test_app_is_development(self):
            self.assertFalse(self.app.application.config['SECRET_KEY'] is 'secret_key')
            self.assertTrue(self.app.application.config['DEBUG'] is True)