尝试这个示例,想知道用最统一的方法进行API测试的正确方法是什么,如何在API测试上加载不同的配置(例如:另一个db)?
config.py
class BaseConfig(object):
DEBUG = True
TESTING = False
# DATABASE
SQLALCHEMY_DATABASE_URI = 'postgresql://test:test@localhost:5432/api'
class DevelopmentConfig(BaseConfig):
pass
class TestingConfig(BaseConfig):
TESTING = True
# DATABASE
SQLALCHEMY_DATABASE_URI = 'postgresql://test:test@localhost:5432/api_testing'
config = {
"development": "api.config.DevelopmentConfig",
"testing": "api.config.TestingConfig",
"default": "api.config.DevelopmentConfig",
"production": "api.config.ProductionConfig",
}
def configure_app(app):
config_name = os.getenv('FLAKS_CONFIGURATION', 'default')
app.config.from_object(config[config_name])tests.py
class DataTestCase(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
def tearDown(self):
pass
def test_get_data(self):
uri = '/data/test'
resp = self.app.get(uri)
assert resp.status_code == status.HTTP_200_OK发布于 2017-01-06 04:03:26
是的,你能做到的。在运行测试之前,只需让应用程序知道要使用哪种配置即可。
您的config.py文件:
# config.py
class BaseConfig(object):
pass
class DevelopmentConfig(BaseConfig):
pass
class TestingConfig(BaseConfig):
pass
class ProductionConfig(BaseConfig):
pass
config = {
'development': DevelopmentConfig,
'testing': TestingConfig,
'production': ProductionConfig,
'default': DevelopmentConfig
}在其中创建应用程序:
from .config import config
app = Flask(_name__)
app.config.from_object(config['testing']) # or development, production...https://stackoverflow.com/questions/41493678
复制相似问题