Python - 带有测试用例的银行项目

Python - Banking project with test cases

我是编程初学者,我刚刚开始我的编程课程。我们正在研究 OOP,而我正在为一个银行项目而苦苦挣扎。

基本上,我们已经获得了测试用例来测试我们的代码是否正常工作。我们将不得不为项目的第二部分编写自己的测试用例。

我当前对给定测试用例的问题是我无法通过检查传输方法是否成功的最后测试之一。

我已经编写了我的转账方法,并且已经在我的 BankAccount 模块上对其进行了测试,并且似乎在做预期的事情:从 account1 取款并将其转入 account2。但是,测试用例仍然失败。

据我所知,测试用例正在查看以下 3 个条件:提款账户的账户余额、入金账户的账户余额和转账金额。

知道我是否遗漏了什么吗?

以下是我的入金转账方式

#Method to process money deposits. Receives deposit_amount variable (float) and return class variable account_balance incremented by deposit amount
def deposit (self,deposit_amount):
    self.account_balance += deposit_amount


#Method to make money transfers. Receives account (obj) and amount_transferred (float) arguments. Returns amount of money transferred if
#enough funds, otherwise returns None

def transfer (self, account, amount):
    if amount > 0 and amount <= account.account_balance:
        self.account_balance -= amount
        account.deposit(amount)
    else:
        return None

现在,下面是检查传输是否成功的测试用例

# Test BANK_ACCOUNT_TEST_7: Tests transfer method for success.
amount_transferred = account_1.transfer(account_2, 20.0)
if account_1.get_balance() != 40.65 or account_2.get_balance() != 50.0 or amount_transferred != 20.0:
    print('FAILED BANK_ACCOUNT_TEST_7')
    return 7

我已经做了几次试验,但仍然失败 BANK_ACCOUNT_TEST_7。

检查你的传输逻辑.. 查看更新的逻辑.. 建议:尝试使用 getter setter 方法而不是直接使用对象变量。 (在当前的解决方案中没有使用getter setter。)

def transfer (self, account, amount):
    if amount > 0 and amount <= self.account_balance:
        self.account_balance -= amount
        account.deposit(amount)
        return amount
    else:
        return None