# -*- coding: UTF-8 -*- import unittest from unittest.mock import Mock class TestCustomer(unittest.TestCase): def test_login_true(self): from g6atm4.views.customer import Customer from g6atm4.models.dbutil import DbUtil customer = Customer() Customer.input_id = Mock() Customer.input_id.return_value = "111111" DbUtil.input_pin = Mock() DbUtil.input_pin.return_value = "222222" self.assertEqual(customer.login(), True, "账号密码匹配,登录ATM成功") def test_login_false(self): from g6atm4.views.customer import Customer from g6atm4.models.dbutil import DbUtil customer = Customer() Customer.input_id = Mock() Customer.input_id.return_value = "123456" DbUtil.input_pin = Mock() DbUtil.input_pin.return_value = "111111" self.assertEqual(customer.login(), False, "账号或密码错误,登录ATM失败") if __name__ == '__main__': unittest.main(argv=['first-arg-ignored'], exit=False)仿照这个,写一个存款测试代码
时间: 2024-04-27 19:24:55 浏览: 168
好的,这里是一个简单的 Python 代码示例,用于测试 ATM 存款的功能:
```python
import unittest
from unittest.mock import Mock
# 导入要测试的类
from atm import ATM
class TestATM(unittest.TestCase):
def test_deposit(self):
atm = ATM()
account_number = '1234567890'
initial_balance = 5000
deposit_amount = 1000
expected_balance = initial_balance + deposit_amount
# 模拟银行系统中的账户信息
atm.bank_system.accounts[account_number] = Mock()
atm.bank_system.accounts[account_number].balance = initial_balance
# 模拟用户输入存款金额
ATM.input_deposit_amount = Mock()
ATM.input_deposit_amount.return_value = deposit_amount
# 调用存款方法
atm.deposit(account_number)
# 验证存款后的账户余额是否正确
self.assertEqual(atm.bank_system.accounts[account_number].balance, expected_balance)
if __name__ == '__main__':
unittest.main()
```
这个代码示例使用 `unittest` 框架来测试 `ATM` 类中的 `deposit` 方法。在测试中,我们模拟了银行系统中的账户信息和用户输入的存款金额,然后调用 `deposit` 方法进行存款操作。最后,我们验证存款后的账户余额是否正确。你可以根据需要修改测试用例中的账户信息和存款金额。
阅读全文