我正在嘗試測驗正在執行的腳本中的引數數量少于三個的情況。除錯時,我看到發生了IndexError,但由于某種原因assertRaises沒有“看到”它。任何人都可以幫助解決這個問題嗎?
我的_file.py:
def my_func():
try:
sys.argv[2]
except IndexError:
pass
text_sys_srgv.py
import sys
import unittest
import my_file
from unittest.mock import patch
class TestServer(unittest.TestCase):
def test_my_func(self):
work_dir = os.path.dirname(os.getcwd())
my_file_full_path = os.path.join(work_dir, 'my_file.py')
test_args = [my_file_full_path, 'arg1']
with patch.object(sys, 'argv', test_args):
self.assertRaises(IndexError, my_file.my_func)
if __name__ == '__main__':
unittest.main()
uj5u.com熱心網友回復:
您my_func不會引發例外。它抓住了它。assertRaises將確認例外是否實際上是從函式中拋出的,而不是在函式內部被捕獲并被抑制。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/371016.html
標籤:Python 蟒蛇-3.x python-unittest argv 系统
