How should I unit test MySQL queries?

我的未来我决定 提交于 2021-02-10 05:12:49

问题


I'm building some unit tests for my Python module which interfaces with a MySQL database via SQLAlchemy. From reading around I gather the best way to do this is to create a test database that I can query as if it was the real thing. I've done this however how should I test the existing queries in the module as they currently all point at the live database?

The only idea I'd come up with was to do something like the following:

def run_query(engine, db_name='live_db')
    engine.execute(f'SELECT * FROM {db_name}.<table_name>')

I could then pass in test_db when I run the function from unittest. Is there a better way?


回答1:


For a scalable testing approach, I would suggest having an intermediate DAL Layer than should decide to which DB the query should be routed.

Testing with a Test Database



来源:https://stackoverflow.com/questions/62943645/how-should-i-unit-test-mysql-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!