下面的代码是用于测试原始代码的单元测试代码
import unittest
from drones import Drone, DroneStore
class DroneTest(unittest.TestCase):
def test_add_success(self):
drone = Drone('Drone1', 1, False)
store = DroneStore()
store.add(drone)
self.assertEqual(drone, store.get(1))
print(store.get(1).name)
def test_add_raiseException_true(self):
drone = Drone('Drone1', 1, False)
store = DroneStore()
store.add(drone)
self.assertEqual(drone, store.get(1))
with self.assertRaises(Exception) as context:
store.add(drone)
self.assertTrue('Drone already exists in store' in str(context.exception)
def test_remove_success(self):
drone = Drone('Drone1', 1, False)
store = DroneStore()
store.add(drone)
self.assertEqual(drone, store.get(1))
print(store.get(1).name)
store.remove(drone)
if __name__ == '__main__':
unittest.main()
似乎我从名为“ test_add_raiseException_true”的函数中收到错误,该函数的创建是为了测试原始代码中的“ add”函数是否在商店中已经存在无人机时引发异常