我正在建立与雪花的连接,并希望通过模拟该连接对其进行单元测试,但是我不确定我是否做得正确。
在测试中,我正在修补snowflake.connector.connect
并调用该方法,断言该方法是否以特定的参数运行。
然后要测试错误处理,我将模拟连接的副作用设置为Exception
,然后进行断言以查看是否引发了异常。
我认为我做的不正确,因为我的测试覆盖率表明代码的最后两行没有覆盖。有任何想法吗?谢谢!
CONNECTION-------------
def connection():
try:
connection = snowflake.connector.connect(
account = 'account',
user = 'user',
password = 'password',
warehouse = 'warehouse',
role = 'role'
)
return connection
except Exception as error:
print (error)
TEST---------------
@patch('snowflake.connector.connect')
def test_connection(self):
connection = connection()
mock_connection.assert_call_once_with(
account = 'account',
user = 'user',
password = 'password',
warehouse = 'warehouse',
role = 'role'
)
mock_connect.side_effect = Exception
with self.assertRaises(Exception):
connection()