Python 引发异常和异常的单元测试方法
我试图进行单元测试,若我们传递了坏项目\u id-查找给定\u id的\u价格,那个么负责返回给定产品价格的方法是否会引发异常 测试: 自动机类:Python 引发异常和异常的单元测试方法,python,unit-testing,Python,Unit Testing,我试图进行单元测试,若我们传递了坏项目\u id-查找给定\u id的\u价格,那个么负责返回给定产品价格的方法是否会引发异常 测试: 自动机类: if __name__ == '__main__': unittest.main() from Item import Item from exceptions.NoItemException import NoItemException from exceptions.NoProperAmountException import NoP
if __name__ == '__main__':
unittest.main()
from Item import Item
from exceptions.NoItemException import NoItemException
from exceptions.NoProperAmountException import NoProperAmountException
from Coin import Coin
from decimal import *
class Automat:
def __init__(self, _bank, objects=None):
self.item_id = 30
self.bank = _bank
if objects is None:
objects = {}
self.objects = objects
self.purchaseItemBank = []
def add_object(self, obj: Item):
id_to_assign = self.item_id
self.objects.update({id_to_assign: obj})
self.item_id += 1
return id_to_assign
def find_price_of_given_id(self, item_id):
if self.objects.get(item_id) is not None:
return self.objects.get(item_id).get_price()
else:
raise NoItemException
def find_amount_of_given_id(self, item_id):
if self.objects.get(item_id) is not None:
return self.objects.get(item_id).get_amount()
else:
raise NoItemException
def checkIfAmountIsPositive(self, item_id):
if self.objects.get(item_id) is not None:
var = True if self.objects.get(item_id).get_amount() > 0 else False
return var
else:
raise NoItemException
def withdrawItem(self, item_id):
self.objects.get(item_id).decrease()
def getAmountOfGivenNominal(self, coinValue):
counter = 0
for iterator in self.bank.bank:
if iterator.getCoinValue() == coinValue:
counter += 1
return counter
不幸的是我
Error
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\unittest\case.py", line 59, in testPartExecutor
yield
File "C:\ProgramData\Anaconda3\lib\unittest\case.py", line 615, in run
testMethod()
File "C:\Users\Admin\PycharmProjects\vending-machine\AutomatTest.py", line 22, in test_checkPriceOfGivenIDWithInvalidID
self.assertRaises(NoItemException, automat.find_price_of_given_id(31))
File "C:\Users\Admin\PycharmProjects\vending-machine\Automat.py", line 28, in find_price_of_given_id
raise NoItemException
exceptions.NoItemException.NoItemException
Ran 1 test in 0.003s
FAILED (errors=1)
Process finished with exit code 1
项目类别:
class Item:
def __init__(self, price, amount=5):
self.amount = amount
self.price = price
def get_price(self):
return self.price
def get_amount(self):
return self.amount
def decrease(self):
self.amount -= 1
def __str__(self):
return f"{self.amount} @ {self.price}"
您正在调用该方法,然后将其返回值传递给assertRaises;但在此之前,已经提出了例外。这不是使用assertRaises的方式。您可以通过两种方式使用它:
将要调用的方法及其参数传递给assertRaises:
注意:不,不是你自己叫的
将其用作上下文管理器:
with self.assertRaises(NoItemException):
automat.find_price_of_given_id(31)
您正在调用该方法,然后将其返回值传递给assertRaises;但在此之前,已经提出了例外。这不是使用assertRaises的方式。您可以通过两种方式使用它:
将要调用的方法及其参数传递给assertRaises:
注意:不,不是你自己叫的
将其用作上下文管理器:
with self.assertRaises(NoItemException):
automat.find_price_of_given_id(31)
请设法减少您发布的代码量。除了测试类之外,您只需要发布一个可能会或可能不会引发异常的函数,以证明您需要使用self.AssertRaises的可能副本。请尝试找到减少发布代码量的方法。除了测试类之外,您只需要发布一个函数,该函数可能会引发异常,也可能不会引发异常,以证明您需要使用self.assertraises的可能副本
self.assertRaises(NoItemException, automat.find_price_of_given_id, 31)
with self.assertRaises(NoItemException):
automat.find_price_of_given_id(31)