Reputation: 405
I have the following file1.py which has the code.
I'm trying to create mock test to test run_q()
file1.py
def exec_mysql(query):
mysql_conn = MySqlActions(..)
..
cur.execute(query)
mysql_conn.commit()
mysql_conn.close()
def run_q():
qa = "delete from table where dts = '%s'" % val
exec_mysql(qa)
Below is the mock code. Not sure how to present the mock for the run_q()
method. Is this the right way to present it?
test_file1.py
import mock
@mock.patch('file1.exec_mysql')
def test_run(mysql_mock)
run_q = mock.Mock()
query = "delete from table where dts = '2015-01-01'"
mysql_mock.assert_called_with(query)
Upvotes: 1
Views: 4062
Reputation: 3010
You almost got it right. There is no need to mock run_q
- you just have to call it inside your test.
Working example:
app.py
def exec_mysql(query):
# do something
return query
def run_q():
qa = 'blahblahblah'
exec_mysql(qa)
tests.py
from unittest import mock
from app import run_q
@mock.patch('app.exec_mysql')
def test_run_q(mysql_mock):
run_q()
mysql_mock.assert_called_with('blahblahblah')
Test execution:
$ pytest -vvv tests.py
===================== test session starts =====================
platform linux -- Python 3.5.2, pytest-3.2.1, py-1.4.34
cachedir: .cache
rootdir: /home/kris/projects/tmp, inifile:
plugins: mock-1.6.2, celery-4.1.0
collected 1 item
tests.py::test_run_q PASSED
================== 1 passed in 0.00 seconds ===================
Upvotes: 1