【发布时间】:2020-08-03 23:21:49
【问题描述】:
我正在为预算计划制作测试工具。个人购买由 Item 类表示。如果类初始化不正确,我想返回 None。但是,当我运行我的测试工具时,它不会将变量检测为无。为什么测试工具没有将 Item 检测为 None?
def __init__(self, nameItem, costItem):
try:
if costItem < 0.00 or type(nameItem) is not str:
self = None
else:
if len(nameItem) < 1:
self = None
else:
self.name = nameItem
self.cost = round(costItem,2)
if self.name == None or self.cost == None:
self = None
except:
self = None
#Test Case
currentTest = currentTest + 1
print("Test" ,currentTest,":Create invalid item using negative cost.")
try:
testItem = Item("Coffee",-1.00)
if testItem is None:
print("Made None value when given incorrect data:TEST PASSED")
passTests = passTests + 1
else:
raise TypeError
except TypeError:
print("Error when creating item, created item instead of detecing error:TEST FAILED")
failedTests.insert(failedCount,currentTest)
failedCount = failedCount + 1
except:
print("Error when conducting test:TEST FAILED")
failedTests.insert(failedCount,currentTest)
failedCount = failedCount + 1
【问题讨论】:
标签: python unit-testing testing try-catch