Error while accessing Flask config while testing Error while accessing Flask config while testing flask flask

Error while accessing Flask config while testing


The app in production is of type flask.app.Flask while the one you use in testing is flask.testing.FlaskClient which are 2 different objects. The way you access the config by using self.app.application.config is the correct and only way as i beilive when you set self.app = app.test_client() this returns a FlaskClient instance.

take a look at the flowing: FlaskClient and test_client


Here's a tip that you can do when testing the client itself:

import unittestfrom app import appclass Tests(unittest.TestCase):    # setup client    def setUp(self):        self.client = app.test_client()    # generating proposital error to see methods available by self.client    def test_app_configs(self):        print(dir(self.client))        self.assertEqual(False, True)

So doing py.test in terminal, this results in:

self = <test_app.Tests testMethod=test_if_inside_aws>    def test_app_configs(self):        print(dir(self.client))>       self.assertEqual(False, True)E       AssertionError: False != Truesrc/test_app.py:29: AssertionError--------------------------------------------------- Captured stdout call ---------------------------------------------------['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'allow_subdomain_redirects', 'application', 'cookie_jar', 'delete', 'delete_cookie', 'environ_base', 'get', 'head', 'open', 'options', 'patch', 'post', 'preserve_context', 'put', 'resolve_redirect', 'response_wrapper', 'run_wsgi_app', 'session_transaction', 'set_cookie', 'trace']================================================= short test summary info ==================================================FAILED src/test_app.py::Tests::test_app_configs - AssertionError: False != True

As @gyx-hh mentioned above, there's a application method available for the self.client. Now, doing the same process but now for checking methods for self.client.application:

['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_before_request_lock', '_blueprint_order', '_find_error_handler', '_get_exc_class_and_code', '_got_first_request', '_register_error_handler', '_static_folder', '_static_url_path', 'add_template_filter', 'add_template_global', 'add_template_test', 'add_url_rule', 'after_request', 'after_request_funcs', 'app_context', 'app_ctx_globals_class', 'auto_find_instance_path', 'before_first_request', 'before_first_request_funcs', 'before_request', 'before_request_funcs', 'blueprints', 'cli', 'config', 'config_class', 'context_processor', 'create_global_jinja_loader', 'create_jinja_environment', 'create_url_adapter', 'debug', 'default_config', 'dispatch_request', 'do_teardown_appcontext', 'do_teardown_request', 'endpoint', 'env', 'error_handler_spec', 'errorhandler', 'extensions', 'finalize_request', 'full_dispatch_request', 'get_send_file_max_age', 'got_first_request', 'handle_exception', 'handle_http_exception', 'handle_url_build_error', 'handle_user_exception', 'has_static_folder', 'import_name', 'inject_url_defaults', 'instance_path', 'iter_blueprints', 'jinja_env', 'jinja_environment', 'jinja_loader', 'jinja_options', 'json_decoder', 'json_encoder', 'log_exception', 'logger', 'make_config', 'make_default_options_response', 'make_null_session', 'make_response', 'make_shell_context', 'name', 'open_instance_resource', 'open_resource', 'open_session', 'permanent_session_lifetime', 'preprocess_request', 'preserve_context_on_exception', 'process_response', 'propagate_exceptions', 'raise_routing_exception', 'register_blueprint', 'register_error_handler', 'request_class', 'request_context', 'response_class', 'root_path', 'route', 'run', 'save_session', 'secret_key', 'select_jinja_autoescape', 'send_file_max_age_default', 'send_static_file', 'session_cookie_name', 'session_interface', 'shell_context_processor', 'shell_context_processors', 'should_ignore_error', 'static_folder', 'static_url_path', 'subdomain_matching', 'teardown_appcontext', 'teardown_appcontext_funcs', 'teardown_request', 'teardown_request_funcs', 'template_context_processors', 'template_filter', 'template_folder', 'template_global', 'template_test', 'templates_auto_reload', 'test_cli_runner', 'test_cli_runner_class', 'test_client', 'test_client_class', 'test_request_context', 'testing', 'trap_http_exception', 'try_trigger_before_first_request_functions', 'update_template_context', 'url_build_error_handlers', 'url_default_functions', 'url_defaults', 'url_map', 'url_map_class', 'url_rule_class', 'url_value_preprocessor', 'url_value_preprocessors', 'use_x_sendfile', 'view_functions', 'wsgi_app']

Then now there's a lot of things that you usually need, and there's the config attribute.