class 中的断言错误测试属性和方法
Assertion error testing attributes and methods in a class
为了练习,我正在写一个class 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)
等内部的方法似乎没有存储我分配给它们的值。
**这是我收到的错误消息(这正是我试图避免的错误消息)**
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()
我的问题是:
- 有好心人帮我解决这个问题并指出我的错误吗?
欢迎并感谢所有帮助。
问题是您没有从其中一些成员函数返回任何内容。
例如,尝试让 deposit
看起来像这样:
def deposit(self, amount):
self._balance += amount
return self._balance
作为 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 的 unittesting framework,因为它将在长期 运行.
中为您提供良好的服务
例如,您可以定义一个 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()
为了练习,我正在写一个class 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)
等内部的方法似乎没有存储我分配给它们的值。
**这是我收到的错误消息(这正是我试图避免的错误消息)**
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()
我的问题是:
- 有好心人帮我解决这个问题并指出我的错误吗?
欢迎并感谢所有帮助。
问题是您没有从其中一些成员函数返回任何内容。
例如,尝试让 deposit
看起来像这样:
def deposit(self, amount):
self._balance += amount
return self._balance
作为 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 的 unittesting framework,因为它将在长期 运行.
中为您提供良好的服务例如,您可以定义一个 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()