Python';单元测试、类和方法
有一个问题我已经讨论了好几天了: 创建一个名为BankAccount的类Python';单元测试、类和方法,python,Python,有一个问题我已经讨论了好几天了: 创建一个名为BankAccount的类 Create a constructor that takes in an integer and assigns this to a `balance` property. Create a method called `deposit` that takes in cash deposit amount and updates the balance accordingly. Create a method calle
Create a constructor that takes in an integer and assigns this to a `balance` property.
Create a method called `deposit` that takes in cash deposit amount and updates the balance accordingly.
Create a method called `withdraw` that takes in cash withdrawal amount and updates the balance accordingly. if amount is greater than balance return `"invalid transaction"`
Create a subclass MinimumBalanceAccount of the BankAccount class
以下是我的解决方案:
class BankAccount(object):
def __init__(self, name, balance = 90):
self.name = name
self.balance = balance
def deposit(self, amount):
self.balance += amount
return self.balance
def withdraw(self, amount):
if self.balance >= amount:
self.balance -= amount
else:
return 'invalid transaction'
class MinimumBalanceAccount(BankAccount):
def __init__(self, name, minimum):
self.name = name
self.minimum = minimum
以下是我必须使用的单元测试:
import unittest
class AccountBalanceTestCases(unittest.TestCase):
def setUp(self):
self.my_account = BankAccount(90)
def test_balance(self):
self.assertEqual(self.my_account.balance, 90, msg='Account Balance Invalid')
def test_deposit(self):
self.my_account.deposit(90)
self.assertEqual(self.my_account.balance, 180, msg='Deposit method inaccurate')
def test_withdraw(self):
self.my_account.withdraw(40)
self.assertEqual(self.my_account.balance, 50, msg='Withdraw method inaccurate')
def test_invalid_operation(self):
self.assertEqual(self.my_account.withdraw(1000), "invalid transaction", msg='Invalid transaction')
def test_sub_class(self):
self.assertTrue(issubclass(MinimumBalanceAccount, BankAccount), msg='No true subclass of BankAccount')
但由于某种原因,当我尝试提交结果时,我收到一条错误消息,说我的解决方案未能通过所有测试。我在这里束手无策,我做错了什么?请帮忙
更新信息
下面是我们看到的错误:
内部错误:runTests中止:TestOutcomeEvent(handled=False,test=,result=,output='Error',exc_info=(,TypeError('此构造函数不带参数',),),reason=None,expected=False,shortLabel=None,longlab=None)不可JSON序列化,单元测试未预期或未通过的。删除它。我已经在我的机器上测试了你的代码,测试都顺利通过
Ran 5 tests in 0.001s
OK
我发现的一个问题是BankAccount构造函数中的name
参数是无用的,测试对它没有任何作用。似乎代码必须是真的,您得到了哪条断言消息?测试的结果是什么?发生了什么事?你的意思是说它没有通过由谁设定的测试?所以它通过了你发布的测试,但没有通过我们看不到的其他测试?这将是导致问题的虚假的name
参数。嘿,邓肯,单元测试是唯一附加到练习的测试。我删除了name参数,现在它只是告诉我在运行脚本时发生了一个错误。翻译可能有问题,至少我希望这就是问题所在。感谢大家帮我检查这一点。从技术上讲,单元测试传递的名称参数值为90
。如果他们中的任何一个使用非默认值作为余额,这一点就会变得明显。对代码的一点解释不会伤害任何人。我认为这是不言自明的。
class BankAccount(object):
def __init__(self, balance = 90):
self.balance = balance
def deposit(self, amount):
self.balance += amount
return self.balance
def withdraw(self, amount):
if self.balance >= amount:
self.balance -= amount
else:
return 'invalid transaction'
class MinimumBalanceAccount(BankAccount):
def __init__(self, minimum):
self.balance = minimum
class BankAccount(object):
def __init__(self,balance):
self.balance = balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
if amount > self.balance:
return "invalid transaction"
class MinimumBalanceAccount(BankAccount):
pass