Python mock Patch os.environ and return value
Solution 1
You can try unittest.mock.patch.dict solution. Just call conn
with a dummy
argument:
import mysql.connector
import os, urlparse
@mock.patch.dict(os.environ, {"DATABASE_URL": "mytemp"}, clear=True) # why need clear=True explained here https://stackoverflow.com/a/67477901/248616
def conn(mock_A):
print os.environ["mytemp"]
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"
Or if you don't want to modify your original function try this solution:
def func():
print os.environ["mytemp"]
def test_func():
k = mock.patch.dict(os.environ, {"mytemp": "mytemp"})
k.start()
func()
k.stop()
test_func()
Solution 2
For this, I find that pytest's monkeypatch fixture leads to better code when you need to set environment variables:
def test_conn(monkeypatch):
monkeypatch.setenv('DATABASE_URL', '<URL WITH CREDENTIAL PARAMETERS>')
with patch(app.mysql.connector) as mock_mysql:
conn()
mock_mysql.connect.assert_called_with(<CREDENTIAL PARAMETERS>)
Solution 3
In my use case, I was trying to mock having NO environmental variable set. To do that, make sure you add clear=True
to your patch.
with patch.dict(os.environ, {}, clear=True):
func()
Solution 4
The accepted answer is correct. Here's a decorator @mockenv
to do the same.
def mockenv(**envvars):
return mock.patch.dict(os.environ, envvars)
@mockenv(DATABASE_URL="foo", EMAIL="[email protected]")
def test_something():
assert os.getenv("DATABASE_URL") == "foo"
Solution 5
At the head of your file mock environ before importing your module:
with patch.dict(os.environ, {'key': 'mock-value'}):
import your.module
immrsteel
Updated on November 05, 2021Comments
-
immrsteel over 2 years
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:
1 what changes i need to make to make this test code work?
2.Do i also have to patch 'urlparse'?
-
immrsteel almost 9 yearsThanks man,i realized it later and soon after deleted my comment.Thanks a lot it is working but i am confused that using above method how {'mytemp':'mytemp'} getting passed into os.environ
-
immrsteel almost 9 yearsThanks a lot,I accepted the answer and will upvote the answer when i will have 15 reputation.
-
Sonic Soul about 5 yearsthis gives me
AttributeError: 'MockFixture' object has no attribute 'setenv'
-
r44 about 5 yearsThis works for me, using
pytest==3.5.0
. @SonicSoul maybe your version is different? -
Martin Thoma almost 4 yearsAs it doesn't make a difference for the question / answer, I removed the wrong Python code from both :-)
-
Calvin Li almost 4 years@SonicSoul make sure to include
monkeypatch
in the test arguments -
Andrew Vaccaro almost 3 yearsThis is exactly what I was missing, thanks!
-
Javi Torre over 2 yearsGreat answer!!!