Python 类和单元测试

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

最佳答案

让我开始吧..

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

希望你能从这里拿走它

关于Python 类和单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9882783/

相关文章:

python - str.replace() 的时间复杂度是 O(n^2) 吗?

c++ - 在共享库的类中调用 GSL 函数

unit-testing - 生产中的 NJasmine

java - Easymock andReturn(模拟)返回 null

python - 使用 Nose 和 Sqlalchemy 以及固定装置编写 Python 测试类

ruby-on-rails - 使用 ActiveRecord::Relation 对象

Python:unittest,AttributeError 尝试从测试类的 __init__ 访问自身属性

python - 如何更高效地将簇写入文件?

python - 如何使用 Python 将按钮(加载项)添加到 Outlook

python - 输出某个特定列的具有特定初始字符串的行