w w w . w d . c h a t
🗣 Wd提问
ai写代码测试
🤖 Ai回答
Let's write a simple test for a basic calculator class using Python and the unittest framework.

calculator.py
python
class Calculator:
def add(self, a, b):
return a + b

def subtract(self, a, b):
return a b

def multiply(self, a, b):
return a * b

def divide(self, a, b):
if b == 0:
raise ZeroDivisionError(Cannot divide by zero)
return a / b

test_calculator.py
python
import unittest
from calculator import Calculator

class TestCalculator(unittest.TestCase):
def setUp(self):
self.calc = Calculator()

def test_add(self):
self.assertEqual(self.calc.add(2, 2), 4)
self.assertEqual(self.calc.add(1, 1), 0)
self.assertEqual(self.calc.add(1, 1), 2)

def test_subtract(self):
self.assertEqual(self.calc.subtract(2, 2), 0)
self.assertEqual(self.calc.subtract(1, 1), 2)
self.assertEqual(self.calc.subtract(1, 1), 0)

def test_multiply(self):
self.assertEqual(self.calc.multiply(2, 2), 4)
self.assertEqual(self.calc.multiply(1, 1), 1)
self.assertEqual(self.calc.multiply(1, 1), 1)

def test_divide(self):
self.assertEqual(self.calc.divide(2, 2), 1)
self.assertEqual(self.calc.divide(1, 1), 1)
self.assertEqual(self.calc.divide(1, 1), 1)
with self.assertRaises(ZeroDivisionError):
self.calc.divide(2, 0)

if __name__ == '__main__':
unittest.main()

In this example:

* We define a `Calculator` class with methods for basic arithmetic operations.
* We create a `TestCalculator` class that inherits from `unittest.TestCase`.
* In the `setUp` method, we create an instance of the `Calculator` class that will be used for testing.
* We define test methods for each operation:

* `test_add`: Tests the `add` method with different inputs.
* `test_subtract`: Tests the `subtract` method with different inputs.
* `test_multiply`: Tests the `multiply` method with different inputs.
* `test_divide`: Tests the `divide` method with different inputs, including a test for division by zero.

To run the tests, save both files and execute the following command:

bash
python m unittest test_calculator.py

If all tests pass, you should see an output indicating the number of tests run and that they all passed.

Additional Example with More Advanced Testing

Let's assume we have a more complex class, like a simple bank account system:

bank_account.py
python
class BankAccount:
def __init__(self, initial_balance=0):
self.balance = initial_balance

def deposit(self, amount):
if amount < 0:
raise ValueError(Deposit amount cannot be negative)
self.balance += amount

def withdraw(self, amount):
if amount < 0:
raise ValueError(Withdrawal amount cannot be negative)
if amount self.balance:
raise ValueError(Insufficient funds)
self.balance = amount

def get_balance(self):
return self.balance

test_bank_account.py
python
import unittest
from bank_account import BankAccount

class TestBankAccount(unittest.TestCase):
def test_default_initial_balance(self):
account = BankAccount()
self.assertEqual(account.get_balance(), 0)

def test_initial_balance(self):
account = BankAccount(100)
self.assertEqual(account.get_balance(), 100)

def test_deposit(self):
account = BankAccount(50)
account.deposit(20)
self.assertEqual(account.get_balance(), 70)

def test_withdraw(self):
account = BankAccount(50)
account.withdraw(20)
self.assertEqual(account.get_balance(), 30)

def test_insufficient_funds(self):
account = BankAccount(50)
with self.assertRaises(ValueError):
account.withdraw(100)

def test_negative_deposit(self):
account = BankAccount(50)
with self.assertRaises(ValueError):
account.deposit(20)

def test_negative_withdrawal(self):
account = BankAccount(50)
with self.assertRaises(ValueError):
account.withdraw(20)

if __name__ == '__main__':
unittest.main()

In this example, we test various scenarios for a bank account, including:

* Initial balance
* Depositing money
* Withdrawing money
* Attempting to withdraw more than the balance
* Attempting to deposit or withdraw a negative amount

Running these tests helps ensure that our `BankAccount` class behaves as expected under different conditions.
0
📍
IP地址 67.235.214.111
🔍
搜索次数 84
提问时间 2025-10-12 05:00:25

📣 商家广告

取名

取名

香港vps

香港vps

广告招商

广告招商

🛒 域名购买

热门提问

🌐 域名评估

最新挖掘

🖌 热门作画

🤝 关于我们

🗨 加入群聊
💬选择任意群聊,与同好交流分享

🔗 友情链接

🧰

站长工具

📢

温馨提示

本站所有 ❓️ 问答 由Ai自动创作,内容仅供参考,若有误差请用"联系"里面信息通知我们人工修改或删除。

👉

技术支持

本站由 🟢 豌豆Ai 提供技术支持,使用的最新版: 《豌豆Ai站群搜索引擎系统 V.25.10.25》 搭建本站。

上一篇 50464 50465 50466 下一篇