Python的单元测试,类和方法

时间:2016-01-06 13:23:13

标签: 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 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')

但由于某种原因,当我尝试提交该结果时,我收到一条错误消息,表示我的解决方案无法通过所有测试。我的智慧在这里结束,我做错了什么?请帮忙

更新信息

以下是我们看到的错误:

Internal Error: runTests aborted: TestOutcomeEvent(handled=False, test=, result=, outcome='error', exc_info=(, TypeError('this constructor takes no arguments',), ), reason=None, expected=False, shortLabel=None, longLabel=None) is not JSON serializable

4 个答案:

答案 0 :(得分:3)

您已经在班级中接受了Cipher cipherencrypt = Cipher.getInstance("AES");参数,单元测试不期望或通过该参数。删除它。

答案 1 :(得分:2)

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

答案 2 :(得分:0)

我已经在我的机器上测试了你的代码,测试都没有任何问题。

\x -> [(+3) x, (+2) x, (+1) x]

我发现的一个问题是BankAccount构造函数中的Ran 5 tests in 0.001s OK 参数是无用的,测试没有对它做任何事情。

答案 3 :(得分:0)

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