Viết tests cho hệ thống ngân hàng

Quay lại bài học

Viết tests cho hệ thống ngân hàng

Lập trình

Tạo unit tests cho class BankAccount

Viết tests cho class BankAccount với các trường hợp: - Gửi tiền hợp lệ/không hợp lệ - Rút tiền hợp lệ/không hợp lệ - Xử lý lỗi

Code mẫu
import unittest

class BankAccount:
    def __init__(self, initial_balance=0):
        self.balance = initial_balance
    
    def deposit(self, amount):
        if amount > 0:
            self.balance += amount
            return True
        return False
    
    def withdraw(self, amount):
        if 0 < amount <= self.balance:
            self.balance -= amount
            return True
        return False
    
    def get_balance(self):
        return self.balance

# Viết test cases ở đây
class TestBankAccount(unittest.TestCase):
    pass