0

I am in a project which needs to get datum from an oracle database. The source code reaches the database by cx_Oracle. Now I wanna find a way to make a fixture for the connection. Is there an elegant way to make such a fixture? Or could somebody recommend a pytest-plugin for Oracle connection?

Thanks in advance.

MT0
  • 113,669
  • 10
  • 50
  • 103

1 Answers1

0

You can use SQLAlchemy for this. Just plug in your connection string into the create_engine string and create a fixture for the connection (and session) like this:

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()

You can read more about the cx_Oracle connection engine from the SQLAlchemy docs here: Location

Your create_engine might look something like this:

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)))")

Which was pulled from this post