Python类和unittest

时间:2012-03-27 02:57:45

标签: python unit-testing class subclass

我正在通过python为新手工作,我遇到了一个问题,我必须创建一个类,子类很好(我认为我做得对)

但我现在必须在python unittest模块中包含一些测试,我无法弄清楚如何实现这一点,任何帮助都将不胜感激。

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

    def withdraw(self,amount):
        if self.balance - amount <= 0:
            print "Overdrawn, please try another option."            
        else:
            self.balance -= amount
            return self.balance
    def deposit(self, amount):
        self.balance += amount
        return self.balance

    def interest(self, amount):
        percent = self.balance / 100.0 * amount
        self.balance = self.balance + percent
        return self.balance


class CreditAccount(BankAccount):
    def withdraw(self,amount):
        if self.balance - amount <= 0:
            self.balance = self.balance - amount - 5
            print "Overdrawn, you have been charged £5 for this."
            return self.balance
        else:
            self.balance -= amount
            return self.balance

class StudentAccount(BankAccount):

    def __init__(self):
        self.balance = 500

    def withdraw(self, amount):       
        if self.balance - amount >= -3000:
            self.balance -= amount
            return self.balance
        else:
            print "£3000 Overdraft limit reached"
            return self.balance


account = BankAccount()
account1 = CreditAccount()
account2 = StudentAccount()
account2.deposit(500)

1 个答案:

答案 0 :(得分:1)

让我让你开始......

my_account = BankAccount()
balance = my_account.deposit(1000) # can also be accessed by my_account.balance

希望你能从这里拿走它

相关问题