让测试容器与棉花糖一起工作
Getting testcontainers working with marshmallow
我正在尝试让 python 测试容器与 marshmallow 一起工作以 运行 测试,但我似乎无法让它工作。当我 运行 测试时,我总是得到 connection refused。我已经创建了一个 sqlalchemy 引擎并对其进行了测试,它工作正常但是当将 testcontainer 连接字符串作为棉花糖配置传递时,它就不起作用了。下面是我的基础测试 class。
class BaseTestCase(TestCase):
base_url = '/api/v1'
def create_app(self):
config = MySqlContainer('mysql:8.0.19')
with config as mysql:
print(mysql.get_connection_url())
e = sqlalchemy.create_engine(mysql.get_connection_url())
result = e.execute("select version()")
for row in result:
print("Printing::::::::::::::::::::::" + str(row))
result.close()
logging.getLogger('connexion.operation').setLevel('ERROR')
connex_app = connexion.App(__name__, specification_dir='../../api/')
connex_app.app.json_encoder = JSONEncoder
connex_app.add_api('static/openapi.yaml')
app = connex_app.app
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = mysql.get_connection_url()
app.config['LOG_LEVEL'] = 'DEBUG'
bcrypt.init_app(app)
db.init_app(app)
ma.init_app(app)
print("Finished setting up")
return app
当我使用 sqlite 作为连接字符串时,我可以让它工作。
class BaseTestCase(TestCase):
base_url = '/api/v1'
def create_app(self):
class Config:
PORT = 5000
SQLALCHEMY_TRACK_MODIFICATIONS = False
FLASK_ENV = 'local'
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
LOG_LEVEL = 'DEBUG'
logging.getLogger('connexion.operation').setLevel('ERROR')
connex_app = connexion.App(__name__, specification_dir='../../api/')
connex_app.app.json_encoder = JSONEncoder
connex_app.add_api('static/openapi.yaml')
app = connex_app.app
app.config.from_object(Config)
bcrypt.init_app(app)
db.init_app(app)
ma.init_app(app)
print("Finished setting up test")
return app
任何帮助将不胜感激。
保罗
我决定使用 testcontainers.compose 并且连接字符串似乎有效。
class BaseTestCase(TestCase):
base_url = '/api/v1'
def create_app(self):
self.compose = testcontainers.compose.DockerCompose(".")
self.compose.start()
time.sleep(10)
class Config:
PORT = 3306
SQLALCHEMY_TRACK_MODIFICATIONS = False
FLASK_ENV = 'local'
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:password@0.0.0.0:3306/test-db'
LOG_LEVEL = 'DEBUG'
logging.getLogger('connexion.operation').setLevel('ERROR')
connex_app = connexion.App(__name__, specification_dir='../../api/')
connex_app.app.json_encoder = JSONEncoder
connex_app.add_api('static/openapi.yaml')
app = connex_app.app
app.config.from_object(Config)
bcrypt.init_app(app)
db.init_app(app)
ma.init_app(app)
print("Finished setting up test")
return app
我正在尝试让 python 测试容器与 marshmallow 一起工作以 运行 测试,但我似乎无法让它工作。当我 运行 测试时,我总是得到 connection refused。我已经创建了一个 sqlalchemy 引擎并对其进行了测试,它工作正常但是当将 testcontainer 连接字符串作为棉花糖配置传递时,它就不起作用了。下面是我的基础测试 class。
class BaseTestCase(TestCase):
base_url = '/api/v1'
def create_app(self):
config = MySqlContainer('mysql:8.0.19')
with config as mysql:
print(mysql.get_connection_url())
e = sqlalchemy.create_engine(mysql.get_connection_url())
result = e.execute("select version()")
for row in result:
print("Printing::::::::::::::::::::::" + str(row))
result.close()
logging.getLogger('connexion.operation').setLevel('ERROR')
connex_app = connexion.App(__name__, specification_dir='../../api/')
connex_app.app.json_encoder = JSONEncoder
connex_app.add_api('static/openapi.yaml')
app = connex_app.app
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = mysql.get_connection_url()
app.config['LOG_LEVEL'] = 'DEBUG'
bcrypt.init_app(app)
db.init_app(app)
ma.init_app(app)
print("Finished setting up")
return app
当我使用 sqlite 作为连接字符串时,我可以让它工作。
class BaseTestCase(TestCase):
base_url = '/api/v1'
def create_app(self):
class Config:
PORT = 5000
SQLALCHEMY_TRACK_MODIFICATIONS = False
FLASK_ENV = 'local'
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
LOG_LEVEL = 'DEBUG'
logging.getLogger('connexion.operation').setLevel('ERROR')
connex_app = connexion.App(__name__, specification_dir='../../api/')
connex_app.app.json_encoder = JSONEncoder
connex_app.add_api('static/openapi.yaml')
app = connex_app.app
app.config.from_object(Config)
bcrypt.init_app(app)
db.init_app(app)
ma.init_app(app)
print("Finished setting up test")
return app
任何帮助将不胜感激。
保罗
我决定使用 testcontainers.compose 并且连接字符串似乎有效。
class BaseTestCase(TestCase):
base_url = '/api/v1'
def create_app(self):
self.compose = testcontainers.compose.DockerCompose(".")
self.compose.start()
time.sleep(10)
class Config:
PORT = 3306
SQLALCHEMY_TRACK_MODIFICATIONS = False
FLASK_ENV = 'local'
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:password@0.0.0.0:3306/test-db'
LOG_LEVEL = 'DEBUG'
logging.getLogger('connexion.operation').setLevel('ERROR')
connex_app = connexion.App(__name__, specification_dir='../../api/')
connex_app.app.json_encoder = JSONEncoder
connex_app.add_api('static/openapi.yaml')
app = connex_app.app
app.config.from_object(Config)
bcrypt.init_app(app)
db.init_app(app)
ma.init_app(app)
print("Finished setting up test")
return app