【问题标题】:Python classes and unittest [closed]Python类和单元测试[关闭]
【发布时间】:2012-03-27 02:57:44
【问题描述】:

我正在为新手使用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)

【问题讨论】:

  • 不幸的是,我不知道从哪里开始,我已经阅读了这一章,但我无法理解它
  • 也许阅读文档会有所帮助:docs.python.org/library/unittest.html#basic-example
  • 我已经阅读了它,但我可能会花更多时间在上面,尝试从那里开始并发布,谢谢您的帮助
  • @user1289022:你需要更明确地很多。你不能理解什么?那里有很多。
  • 首先我想用这个测试什么,它没有真正指定,我有点希望有人能根据我的代码有一个想法。该任务只是声明“包含一些测试”与 unittest 模块。

标签: python unit-testing class subclass


【解决方案1】:

让我开始吧..

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

希望你能从这里拿走它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 2011-11-07
    • 2010-12-21
    相关资源
    最近更新 更多