我想在我的main.py寫了下面的方法的單元測試情況如何編寫單元測試用例這個塞納里奧
def create_tmp_dir(tmp_dir):
logger.info('{app} Creating directory: {arg}'.format(
app=app_log, arg=tmp_dir))
mkdir_cmd = 'mkdir -p ' + tmp_dir
try:
utility.simple_cmd(mkdir_cmd)
except Exception, exc:
logger.critical('{app} Unable to create dir: {arg}'.format(app=app_log, arg=exc))
exit(1)
在utility.py的simple_cmd方法做:
def simple_cmd(cmd):
subprocess.call(cmd, shell=True, stdout=subprocess.PIPE)
這是我test_main.py
def test_create_tmp_dir_raises_exception_on_blank_dir():
with patch('utility.simple_cmd'):
with pytest.raises(Exception):
main.create_tmp_dir('')
當我運行pytest,它說異常不會引發
with patch('utility.simple_cmd'):
with pytest.raises(Exception):
> main.create_tmp_dir('')
E Failed: DID NOT RAISE <type 'exceptions.Exception'>
感謝您的回覆。 –