代码中的Nosetests错误,其中手动引发异常

时间:2018-04-30 08:13:52

标签: python

我写了一个简单的程序来练习,但它的测试代码一直在失败。这是主要代码:

class Bank(object):

    def __init__(self, name, deposit=0, withdraw=0, balance=0.0):
        self.name = name
        self.deposit = deposit
        self.withdraw = withdraw
        self.balance = balance

    def deposited(self):
        self.balance += self.deposit
        return self.balance

    def withdrawn(self):
        if self.balance < self.withdraw:
            raise ValueError("INSUFFICIENT BALANCE!")
        else:
            self.balance -= self.withdraw
            return self.balance

这是它的测试代码:

from nose.tools import *
from bank.account import Bank

def test_account():
     Maria = Bank('Maria', None, None)
     assert_equal(Maria.name, 'Maria')
`   `assert_equal(Maria.balance, 0.0)
     assert_equal(Maria.deposit, None)
     assert_equal(Maria.withdraw, None)

    Sam = Bank('Sam', 10, 1)
    assert_equal(Sam.name, 'Sam')
    assert_equal(Sam.balance, 0.0)
    assert_equal(Sam.deposit, 10)
    assert_equal(Sam.withdraw, 1)

def test_deposit():
    Rotschild = Bank('Rotschild', 100, 0)
    Rotschild.deposited()
    assert_equal(Rotschild.balance, 100)

def test_withdaw():
    Morgan = Bank('Morgan', 100, 50)
    Morgan.deposited()
    Morgan.withdrawn()
    assert_equal(Morgan.balance, 50)

    Bill = Bank('Bill', 5, 10)
    Bill.deposited()
    assert_raises(ValueError, Bill.withdrawn())

运行nosetests时,它总是显示Failed(错误= 1),我无法弄清楚它是否是语法问题或者是什么。 我希望测试在帐户余额低于提取的金额时提出异常。

0 个答案:

没有答案