Home > Enterprise >  Python - Banking project with test cases
Python - Banking project with test cases

Time:10-04

I am a beginner on programming and I just started my programming course. We are looking at OOP, and I am struggling with a banking project.

Basically, we have been given test cases to test that our code is working properly. We will have to write our own test cases for the second part of the project.

My current issue with the given test cases is that I cannot pass one of the last tests which checks for a successful transfer method.

I have written my transfer method and I have tested it on my BankAccount module and seems to be doing what is expected: withdrawing money from account1 and moving it into account2. However, the test case still failing.

As far as I can see, the test case is looking at the following 3 conditions: account balance on withdrawn account, account balance on deposited account and amount transferred.

Any idea if I am missing something here?

Below is my deposit and transfer methods

#Method to process money deposits. Receives deposit_amount variable (float) and return class variable account_balance incremented by deposit amount
def deposit (self,deposit_amount):
    self.account_balance  = deposit_amount


#Method to make money transfers. Receives account (obj) and amount_transferred (float) arguments. Returns amount of money transferred if
#enough funds, otherwise returns None

def transfer (self, account, amount):
    if amount > 0 and amount <= account.account_balance:
        self.account_balance -= amount
        account.deposit(amount)
    else:
        return None

Now, below is the test case to check a successful transfer

# Test BANK_ACCOUNT_TEST_7: Tests transfer method for success.
amount_transferred = account_1.transfer(account_2, 20.0)
if account_1.get_balance() != 40.65 or account_2.get_balance() != 50.0 or amount_transferred != 20.0:
    print('FAILED BANK_ACCOUNT_TEST_7')
    return 7

I have done several trials and still getting FAILED BANK_ACCOUNT_TEST_7.

CodePudding user response:

Check your transfer logic .. See the Updated logic .. Suggestion: Try to use getter setter method instead of directly using object variable. (haven't used the getter setter in current soln.)

def transfer (self, account, amount):
    if amount > 0 and amount <= self.account_balance:
        self.account_balance -= amount
        account.deposit(amount)
        return amount
    else:
        return None
  • Related