豌豆Ai站群搜索引擎系统:www.wd.chat

🗣 SL提问: 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.

英语分析
📢 商家广告

广告招商

月饼

广东过滤器

广告招商

贵阳用友软件

域名抢注

广告招商

高防vps

豌豆Ai站群搜索引擎系统

广告招商

广告招商

广告招商

广告招商

广告招商


0

IP地址: 169.65.73.171

搜索次数: 62

提问时间: 2025-04-19 22:39:07

❓️ 热门提问
宁波港
黃清洋
圆盘黄金吊坠
k金耳饰过敏吗
黄金etf杠杆
坚固贵金属交易
现在外贸哪个平台好做
2022年黄金期货走势图
买外汇app
1两黄金多少g
豌豆Ai站群搜索引擎系统

🖌 热门作画


🤝 关于我们
三乐Ai 作文批改 英语分析 在线翻译 拍照识图
Ai提问 英语培训 本站流量 联系我们

🗨 加入群聊
群

🔗 友情链接
网页上传源码  月饼  ai提问

🧰 站长工具
Ai工具  whois查询  搜索

📢 温馨提示:本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。

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

上一篇 50857 50858 50859 下一篇