在pytest中是否有一种优雅的方法来通过python处理oracle连接?

问题描述 投票:0回答:1

我在一个需要从oracle数据库中获取数据的项目中。源代码通过cx_Oracle到达数据库。现在,我想找到一种制作固定装置的方法。有没有一种优雅的方法来制作这样的灯具?还是有人可以推荐pytest-plugin用于Oracle连接?

提前感谢。

python oracle pytest fixtures cx-oracle
1个回答
0
投票

您可以为此使用SQLAlchemy。只需将您的连接字符串插入create_engine字符串中,然后为连接(和会话)创建固定装置,如下所示:

engine = create_engine('your connection string goes here with your login creds')

@pytest.fixture(scope='module')
def connection():
    connection = engine.connect()
    yield connection
    connection.close()

您可以从SQLAlchemy文档中阅读有关cx_Oracle连接引擎的更多信息:Location

您的create_engine可能看起来像这样:

engine = create_engine("oracle+cx_oracle://<username>:<password>@(DESCRIPTION = (LOAD_BALANCE=on) (FAILOVER=ON) (ADDRESS = (PROTOCOL = TCP)(HOST = <host>)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = devdb)))")

this post中拔出

© www.soinside.com 2019 - 2024. All rights reserved.