提问人:Rolling Happy 提问时间:10/31/2023 最后编辑:Minal ChauhanRolling Happy 更新时间:10/31/2023 访问量:20
原始文件和test_module文件对同一情况给出不同的答案
Original File and test_module file giving different answers for the same case
问:
我对编码有点陌生,正在处理一个 replit 项目,但无法弄清楚为什么test_modules文件一直给出一个,即使代码在主文件上运行良好。AssertionError
原始文件budget_app.py(对于这个问题,只关注存款功能就足够了):
class Category:
def __init__(self, title):
self.title = title
ledger = []
def deposit(self, amount=0, description=""):
self.ledger.append({"amount": amount, "description": description})
def withdraw(self, amount=0, description=""):
if not self.check_funds(amount):
return False
amount = -1 * amount
self.ledger.append({"amount": amount, "description": description})
return True
def get_balance(self):
totalAmount = 0
for i in self.ledger:
totalAmount += i["amount"]
return totalAmount
def transfer(self, amount, cat):
if not self.check_funds(amount):
return False
self.withdraw(amount, ("Transfer to " + cat.title))
cat.deposit(amount, ("Transfer from " + self.title))
return True
def check_funds(self, amount):
if amount > self.get_balance():
return False
return True
测试模块:
import unittest
import budget_app
class UnitTests(unittest.TestCase):
maxDiff = None
def setUp(self):
self.food = budget_app.Category("Food")
self.entertainment = budget_app.Category("Entertainment")
self.business = budget_app.Category("Business")
def test_deposit(self):
self.food.deposit(900, "deposit")
actual = self.food.ledger[0]
expected = {"amount": 900, "description": "deposit"}
self.assertEqual(actual, expected, 'Expected `deposit` method to create a specific object in the ledger instance variable.')
if __name__ == "__main__":
unittest.main()
运行此test_module.py文件时遇到的错误是
======================================================================
FAIL: test_deposit (__main__.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\fnafb\OneDrive\Desktop\programming\python programs\test_modules.py", line 17, in test_deposit
self.assertEqual(actual, expected, 'Expected `deposit` method to create a specific object in the ledger instance variable.')
AssertionError: {'amount': 10, 'description': 'deposit'} != {'amount': 900, 'description': 'deposit'}
- {'amount': 10, 'description': 'deposit'}
? ^
+ {'amount': 900, 'description': 'deposit'}
? ^^
: Expected `deposit` method to create a specific object in the ledger instance variable.
======================================================================
它声称我的存款功能返回而不是{'amount': 10, 'description': 'deposit'}
{'amount': 900, 'description': 'deposit'}
但是当我通过添加行直接在主文件中检查此问题时(我正在运行 budget.py 文件)
food = Category("Food")
food.deposit(900, "deposit")
actual = food.ledger[0]
print(actual)
输出是所需的输出{'amount': 900, 'description': 'deposit'}
对于多个类似的测试用例,我遇到了类似的错误。它们都在主文件中工作
答: 暂无答案
评论