mercredi 24 février 2016

Can Unit Test code be wrong? Code passes manual tests, but fails on unit test

I am 3 weeks into python programming using an online source with an online unit testing code. One of the exercises asks to create a class called 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

Here is my code:

class BankAccount:
    def __init__(self, balance):
        self.balance = balance

    def deposit(self, amount):
        self.balance += amount
        return self.balance

    def withdraw(self, amount):
        if(amount > self.balance):
            print "invalid transaction."
        else:
            self.balance -= amount
            return self.balance   

class MinimumBalanceAccount(BankAccount):
    def __init__(self, minimum_balance):
        BankAccount.__init__(self, self.balance)
        self.minimum_balance = minimum_balance

    def withdraw(self, amount):
        if(self.balance - amount < self.minimum_balance):
            print "Minimum balance exceeded."
        else:
            self.balance -= amount
            return self.balance

I am testing the code on the following unit test code, but it does not pass. Need help pointing out where my code is wrong. Sorry in advance if the question does not meet the required standards.

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')

Aucun commentaire:

Enregistrer un commentaire