Unit testing conn() using mock:
app.py
import mysql.connector
import os, urlparse
def conn():
if "DATABASE_URL" in os.environ:
url = urlparse(os.environ["DATABASE_URL"])
g.db = mysql.connector.connect(
user=url.username,
password=url.password,
host=url.hostname,
database=url.path[1:],
)
else:
return "Error"
test.py
def test_conn(self):
with patch(app.mysql.connector) as mock_mysql:
with patch(app.os.environ) as mock_environ:
con()
mock_mysql.connect.assert_callled_with("credentials")
Error: Assertion mock_mysql.connect.assert_called_with
is not called.
which I believe it is because 'Database_url' is not in my patched os.environ
and because of that test call is not made to mysql_mock.connect.
Questions:
What changes do I need to make this test code work?
Do I also have to patch
urlparse
?