class TestElectricCar(unittest.TestCase): def setUp(self): self.electric_car = ElectricCar() def test_electric_car_fuel_cells(self): self.assertEqual(1, self.electric_car.getNumberFuelCells()) self.electric_car.setNumberFuelCells(8) self.assertEqual(8, self.electric_car.getNumberFuelCells()) self.assertRaises(ValueError, self.electric_car.setNumberFuelCells, '6') self.assertRaises(TypeError, self.electric_car.setNumberFuelCells, 2, 4) def test_electric_car_mileage(self): self.assertEqual(0, self.electric_car.getMileage()) self.electric_car.setMileage(460000) self.assertEqual(460000, self.electric_car.getMileage()) self.assertRaises(ValueError, self.electric_car.setMileage, '60000')
from Car import ElectricCar my_tesla = ElectricCar('tesla', 'model s', 2019) print(my_tesla.get_descriptive_name()) my_tesla.bettery.describe_battery() my_tesla.bettery.get_range()
def setUp(self): self.electric_car = ElectricCar()
def test_check_car_type(self): self.assertEqual('E', self.rental.check_car_type(ElectricCar())) self.assertRaises(ValueError, self.rental.check_car_type, 'Electric')
#从一个模块导入多个类 from Car import Car, ElectricCar my_tesla = ElectricCar("tesla", "model s", 2016) print(my_tesla.get_descriptive_name()) my_tesla.battery.get_range() my_car = Car("audi", "a15", 2020) print(my_car.get_descriptive_name())
from Car import ElectricCar, Car my_beetle = Car('volkswagen', 'beetle', 2016) print(my_beetle.get_descriptive_name()) my_tesla = ElectricCar('tesla', 'roadster', 2016) print(my_tesla.get_descriptive_name())