Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's unittest, classes and methods

Tags:

python

There's a question I've been stuck on for days now: 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's my solution:

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

Here's the unittest that i had to work with:

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

But for some reason, when i tried submitting that result, i got back an error message that said my solution failed to pass all the tests. I'm at my wits end here, what am i doing wrong? Please help

Updated Information

Here is the error we are seeing:

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

like image 768
Ailevi Avatar asked Dec 05 '25 03:12

Ailevi


2 Answers

You've accepted a name parameter in your class, which the unit test is not expecting or passing. Remove that.

like image 144
Daniel Roseman Avatar answered Dec 07 '25 16:12

Daniel Roseman


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
like image 23
Mageto Avatar answered Dec 07 '25 15:12

Mageto