Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError when test client is named self.app #93

Open
brandones opened this issue Sep 12, 2016 · 2 comments
Open

AttributeError when test client is named self.app #93

brandones opened this issue Sep 12, 2016 · 2 comments

Comments

@brandones
Copy link

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
Copy link

ghost commented Sep 7, 2017

+1 for self.client because it works, thanks

@codeKrowe
Copy link

codeKrowe commented May 29, 2018

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants