在为我的Flask应用程序编写测试时,我在尝试设置Flask配置设置时遇到了一个问题。
通常,我这样做:
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.assertTrue(self.app.config['SECRET_KEY'] is 'secret_key')
self.assertTrue(self.app.config['DEBUG'] is True)
导致错误
AttributeError:' FlaskClient'对象没有属性' config'
只有从调试中我才看到没有" config"相反,我必须去self.app.application.config
才能让它发挥作用。
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.assertTrue(self.app.application.config['SECRET_KEY'] is 'secret_key')
self.assertTrue(self.app.application.config['DEBUG'] is True)
我做了什么,Flask是否在更新中更改了此内容,还是有更好的方法来执行此操作?
编辑:错误地在顶部块中发布代码,现在正确
答案 0 :(得分:2)
生产中的app
类型为flask.app.Flask
,而您在测试中使用的flask.testing.FlaskClient
为self.app.application.config
,它们是2个不同的对象。使用self.app = app.test_client()
访问配置的方式是正确且唯一的方法,因为当您设置c/Users/631/AppData/Local/Continuum/Anaconda21
时,它会返回FlaskClient实例。
看看流动的:FlaskClient和test_client
答案 1 :(得分:0)
这里是您测试客户端本身时可以做的提示:
import unittest
from app import app
class 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)
在终端中执行py.test
会导致:
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 != True
src/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
如上面@ gyx-hh所述,application
有一个self.client
方法可用。现在,执行相同的过程,但现在检查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']
然后,您通常需要很多东西,并且有config
属性。