我正在尝试为我的其中一个函数编写一个模拟单元测试,以检查git_status
。我无法弄清楚该如何进行。
该函数看起来像这样:
import git_status
def check_git_status():
status = git_status.Status(".")
if status.A != []:
raise RuntimeError(
'Added git files not commited: {}'.format(status.A)
)
elif status.M != []:
raise RuntimeError(
'Modified git files not commited: {}'.format(status.M)
)
elif status.D != []:
raise RuntimeError(
'Deleted git files not commited: {}'.format(status.D)
)
elif status.R != []:
raise RuntimeError(
'Renamed git files not commited: {}'.format(status.R)
)
我尝试了什么:
import pytest
from unittest import mock
@pytest.mark.parametrize(
"status, expected_status",
[
("A", []), ("M", []), ("D", []), ("R", []),
("B", 'error')
])
@mock.patch("git_status.Status", autospec=True)
def test_check_git_status(mock_git_status, status, expected_status):
if expected_status != 'error':
mock_git_status = mock.Mock(name="mock git status", **{str(status): expected_status})
else:
with pytest.raises(RuntimeError):
check_git_status()
如果有人可以在这里指导/帮助我,那就太好了。谢谢:)