Python mock Patch os.environ并返回值

时间:2022-07-14 22:44:00

Unit testing conn() using mock:

使用mock对conn()进行单元测试:

app.py

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 mysql.connector.error.Errors as err:
     return "Error

test.py

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.

错误:未调用断言mock_mysql.connect.assert_called_with。

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.

我认为这是因为'Database_url'不在我修补的os.environ中,并且因为没有对mysql_mock.connect进行测试调用。

Questions:

问题:

1 what changes i need to make to make this test code work?

1我需要做些什么改变才能使这个测试代码工作?

2.Do i also have to patch 'urlparse'?

2.我还要补丁'urlparse'吗?

2 个解决方案

#1


27  

import mysql.connector
import os,urlparse
@mock.patch.dict(os.environ,{'DATABASE_URL':'mytemp'})
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 mysql.connector.error.Errors as err:
     return "Error

You can try this way.Just call conn with a dummy argument.

你可以尝试这种方式。只需用伪参数调用conn。

Or

要么

If you dont want to modify ur original function try this:

如果你不想修改你原来的功能试试这个:

def func():
    print os.environ["mytemp"]

def test_func():
    k=mock.patch.dict(os.environ,{'mytemp':'mytemp'})
    k.start()
    func()
    k.stop()

test_func()

#2


2  

You can also use something like the modified_environ context manager describe in this question to set/restore the environment variables.

您还可以使用此问题中描述的modified_environ上下文管理器来设置/恢复环境变量。

with modified_environ(DATABASE_URL='mytemp'):
    func()

#1


27  

import mysql.connector
import os,urlparse
@mock.patch.dict(os.environ,{'DATABASE_URL':'mytemp'})
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 mysql.connector.error.Errors as err:
     return "Error

You can try this way.Just call conn with a dummy argument.

你可以尝试这种方式。只需用伪参数调用conn。

Or

要么

If you dont want to modify ur original function try this:

如果你不想修改你原来的功能试试这个:

def func():
    print os.environ["mytemp"]

def test_func():
    k=mock.patch.dict(os.environ,{'mytemp':'mytemp'})
    k.start()
    func()
    k.stop()

test_func()

#2


2  

You can also use something like the modified_environ context manager describe in this question to set/restore the environment variables.

您还可以使用此问题中描述的modified_environ上下文管理器来设置/恢复环境变量。

with modified_environ(DATABASE_URL='mytemp'):
    func()