I'm currently writing a simple flask application and want to test that my configuration options are working.
Here's my config.py:
import logging
import logging.handlers
import os
class BaseConfig(object):
DEBUG = False
TESTING = False
...
class DevelopmentConfig(BaseConfig):
DEBUG = True
TESTING = True
...
class TestingConfig(BaseConfig):
DEBUG = False
TESTING = True
...
config = {
'development': 'app.config.DevelopmentConfig',
'testing': 'app.config.TestingConfig',
'default': 'app.config.BaseConfig'
}
def configure_app(app):
config_name = os.environ.get('FLASK_CONFIGURATION', 'default')
app.config.from_object(config[config_name])
app.config.from_pyfile('config.py', silent=True)
apikey = os.environ.get('TOKEN', None)
if apikey:
app.config['APIKEY'] = apikey
I call configure_app
in my __init__.py
right after I create the Flack object.
app = Flask(__name__)
configure_app(app)
config.py:
APIKEY = 'filesecret'
HOOKS = {'testpush': 'scripts/test.sh'}
What I want to do is be able to unittest using py.test the various configuration options. My current attempt is to try and use mock_open
, but the configuration keep reading what's in the file instead of skipping it.
class TestConfig:
def setup(self):
self.app = app.app
self.app.config['TESTING'] = True
def test_default(self, mocker):
m = mocker.mock_open(read_data='')
configure_app(self.app)
assert self.app.config['APIKEY'] == "secret"
results of test:
def test_default(self, mocker):
m = mocker.mock_open(read_data='')
configure_app(self.app)
> assert self.app.config['APIKEY'] == "secret"
E assert 'filesecret' == 'secret'
I'm not sure how to do this when I don't know the internals of flask. Is there a way to fake/mock the file? I want to be able to write the various test cases of the configuration file being present, not present, and the environment variable being set to make sure priority is maintained.