用户提问: 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地址: 141.224.190.23
搜索次数: 3
提问时间: 2025-04-19 22:39:07
热门提问:
双创50ETF
景顺长城量化小盘股票
ai电视助手下载
介绍域名toui.cn的含义、价值与适合的行业。
华夏鼎佳债券C