為了練習,我正在撰寫一個類 BankAccount 來學習 Python 中的 OOP。為了使我的程式更加冗余,我正在嘗試撰寫一個測驗函式test_BankBankAccount()來練習如何執行測驗函式。
該測驗功能test_BankBankAccount()是假設檢驗的方法deposit(),withdraw(),transfer()和get_balance()作業按預期。
然而,由于內部的方法測驗功能失敗computed_deposit = test_account.deposit(400),computed_transfer = test_account.transfer(test_account2, 200)等等似乎沒有商店,我ASIGN他們的價值觀。
**這是我收到的錯誤訊息(正是我試圖避免的錯誤訊息)**
assert success1 and success2 and success3 and success4, (msg1, msg2, msg3, msg4)
AssertionError: ('computet deposit = None is not 400', 'computet transfer = None is not 200', 'computet withdrawal = None is not 200', 'computet balance = 0 is not 0')
這是我迄今為止撰寫的大部分代碼的片段
class BankAccount:
def __init__(self, first_name, last_name, number, balance):
self._first_name = first_name
self._last_name = last_name
self._number = number
self._balance = balance
def deposit(self, amount):
self._balance = amount
def withdraw(self, amount):
self._balance -= amount
def get_balance(self):
return self._balance
def transfer(self,other_account, transfer_amount):
self.withdraw(transfer_amount)
other_account.deposit(transfer_amount)
def print_info(self):
first = self._first_name
last = self._last_name
number = self._number
balance = self._balance
s = f"{first} {last}, {number}, balance: {balance}"
print(s)
def main():
def test_BankBankAccount():
test_account = BankAccount("Dude", "man", "1234", 0)
test_account2 = BankAccount("Dude2", "man2","5678", 0)
expected_deposit = 400
expected_withdrawal = 200
expected_transfer = 200
expected_get_balance = 0
computed_deposit = test_account.deposit(400)
computed_transfer = test_account.transfer(test_account2, 200)
computed_withdrawal = test_account.withdraw(200)
computed_get_balance = test_account.get_balance()
#tol = 1E-17
success1 = abs(expected_deposit == computed_deposit) #< tol
success2 = abs(expected_transfer == computed_transfer) #< tol
success3 = abs(expected_withdrawal == computed_withdrawal) #< tol
success4 = abs(expected_get_balance == computed_get_balance) #<tol
msg1 = f"computet deposit = {computed_deposit} is not {expected_deposit}"
msg2 = f"computet transfer = {computed_transfer} is not {expected_transfer}"
msg3 = f"computet withdrawal = {computed_withdrawal} is not {expected_withdrawal}"
msg4 = f"computet balance = {computed_get_balance} is not {expected_get_balance}"
assert success1 and success2 and success3 and success4, (msg1, msg2, msg3, msg4)
test_BankBankAccount()
我的問題是:
- 有沒有好心的人可以幫助我解決這個問題并發現我的錯誤?
歡迎和感謝所有幫助。
uj5u.com熱心網友回復:
問題是您沒有從這些成員函式中回傳任何內容。
嘗試,例如,使deposit看起來像這樣:
def deposit(self, amount):
self._balance = amount
return self._balance
uj5u.com熱心網友回復:
作為 Goodword 答案的替代方案,您可以嘗試:
test_account.deposit(400)
computed_deposit = test_account.get_balance()
test_account.transfer(test_account2, 200)
computed_withdrawal = test_account.get_balance()
computed_transfer = test_account2.get_balance()
test_account.withdraw(200)
computed_get_balance = test_account.get_balance()
關于測驗,向你致敬!我建議您熟悉 Python 的單元測驗框架,因為從長遠來看它會很好地為您服務。
例如,您可以定義一個class用于測驗您的 BankAccount,例如
import unittest
class TestBankAccount(unittest.TestCase):
# setUp gets run before every individual test
def setUp(self):
self.account1 = BankAccount("First","Account","1234",0)
self.account2 = BankAccount("Second","Account","5678",200)
def test_deposit(self):
self.account1.deposit(400)
self.assertEqual(self.account1.get_balance(),400)
def test_withdraw(self):
self.account2.withdraw(200)
self.assertEqual(self.account1.get_balance(),0)
def test_transfer(self):
self.account2.transfer(self.account1,200)
self.assertEqual(self.account1.get_balance(),200)
self.assertEqual(self.account2.get_balance(),0)
if __name__=="__main__":
unittest.main()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/334320.html
