【发布时间】:2022-01-06 02:24:01
【问题描述】:
当尝试在 argparse 中对参数的验证进行单元测试时,以下工作:
我的模块:
def validate_mac_addr(mac_addr):
regex = re.compile(r'^((([a-f0-9]{2}:){5})|(([a-f0-9]{2}-){5}))[a-f0-9]{2}$', re.IGNORECASE)
if re.match(regex, mac_addr) is not None:
return mac_addr
msg = f"[-] Invalid MAC address: '{mac_addr}'"
raise argparse.ArgumentTypeError(msg)
测试:
import mymodule
import unittest
def test_mac_address_false(self):
self.assertRaises(Exception, mymodule.validate_mac_addr,"n0:ma:ca:dd:re:ss:here")
但我想捕捉一个更具体的“ArgumentTypeError”,但在这个例子中这显然是不可能的!? assertRaises() 中 Exception 的一般用法是怎么回事?
顺便说一句
isinstance(argparse.ArgumentTypeError, Exception)
返回 False?!
【问题讨论】:
标签: python exception argparse python-unittest