2

I have code like below

 engine = create_engine(URL(
        host="host",
        username="username",
        password="password",
        database="dbname",
        drivername="drivername",
        port="port"
    )) 

 conn = engine.connect()

 table_obj = Table(
        table_name,
        metadata,
        autoload=True,
        autoload_with=engine)

 def check_something(conn, table_obj, u_id):
 stmt = select(*[c for c in table_obj.c])
 result = conn.execute(stmt).fetchall()
 return result

Here i need to test the check_something method without connecting to postgresql database using pytest. Had gone through links available in google but everyone connecting to DB.

any option available to test the method by mocking DB connection like we mock AWS connections(S3, etc..)

1 Answer 1

1

You can use the standard unittest.mock module from Python to decorate your test with a mock function replacing the pg method with whatever you want to return from postgres.

from unittest.mock import patch

def my_pg_mock():
    return []

@patch('module.check_something', my_pg_mock)
def test_something(mock): # include it here or it will override your other fixtures. Can have any name. 
    pass

The docs can be found here even though I always find them somewhat confusing: https://docs.python.org/dev/library/unittest.mock.html. The module.check_something is important because you need to patch the module were it's used, not where it is defined. So if your code imports check_something in main.py, the string to pass to patch is main.check_something and not for example postgres.check_something.

You can also pass the return value directly with:

from unittest.mock import patch

@patch('module.check_something', return_value=[])
def test_something(mock):  # include it here or it will override your other fixtures. Can have any name. 
    pass
Sign up to request clarification or add additional context in comments.

5 Comments

Any option available with 'pytest'
This is 100% pytest compatible and included in the python standard library. So if you define pytest tests, this runs
Using it with pytest myself. All pytest libraries are wrapping this anyway
In my case i need to compare the results. would it be better to mock the return result or create an in memory sqllite engine
It depends. How in-memory is it? If the sqlite approach means that you create persistent state somewhere f.e. a data file, I would advise against that, because it is going to bite you later. Better to just mock the return result. Mocking the result is better in almost all cases I think as it's simple and specific to your test. It is even better (if possible) to structure your code in a way that the pg interface is separate from the other methods, because then you can just call your method with the expected result from pg and you don't need to mock anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.