Je suis en train d'écrire un cas de unittest pour la méthode ci-dessous dans mon main.pyComment écrire cas de test unitaire pour cette senario
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)
la méthode simple_cmd en utility.py fait:
def simple_cmd(cmd):
subprocess.call(cmd, shell=True, stdout=subprocess.PIPE)
ceci est mon 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('')
quand je lance pytest, il dit exception ne se pose pas
with patch('utility.simple_cmd'):
with pytest.raises(Exception):
> main.create_tmp_dir('')
E Failed: DID NOT RAISE <type 'exceptions.Exception'>
Merci pour votre réponse. –