class PumpTests(unittest.TestCase): """ Unit tests for the Pump class """ def setUp(self): """ setup for PumpTest class :return: """ self.pump = Pump('127.0.0.1', 8000) def test_set_state(self): """ test setting the state of pump :return: """ self.pump.set_state = MagicMock(return_value=True) self.pump.set_state('PUMP_IN') self.pump.set_state.assert_called_with('PUMP_IN') def test_get_state(self): """ test calling get state of pump :return: """ self.pump.get_state = MagicMock(return_value=1) self.assertEqual(self.pump.get_state(), 1)
class ControllerTests(unittest.TestCase): """ Unit tests for the Controller class """ def setUp(self): """ Set up method for test_controller """ self.sensor = Sensor('127.0.0.1', 8000) self.sensor.measure = MagicMock(return_value=100) self.pump = Pump('127.0.0.1', 8000) self.pump.set_state = MagicMock(return_value=True) self.pump.get_state = MagicMock(return_value=self.pump.PUMP_OFF) self.decider = Decider(100, .10) self.decider.decide = MagicMock(return_value=self.pump.PUMP_OFF) self.controller = Controller(self.sensor, self.pump, self.decider) self.controller.tick() self.actions = { 'PUMP_IN': self.pump.PUMP_IN, 'PUMP_OUT': self.pump.PUMP_OUT, 'PUMP_OFF': self.pump.PUMP_OFF } def test_controller(self): """ Test controller and tick: 1: Check sensor.measure for accuracy 2: test pump.get_state for correct return 3: decider.decide for correct 'decision' 4: Test pump.set_state for expected mocked output 5: Test sensor.measure return val is correct type 6: Round-a-bout way of testing the try/except block of controller.tick() -- if pump.set_state returns False, it is because of a TypeError, checking that if it is False, it raises TypeError """ self.assertEqual(100, self.sensor.measure()) # 1 self.assertEqual(0, self.pump.get_state()) # 2 self.assertEqual(0, self.decider.decide(100, 0, self.actions)) # 3 self.assertEqual(True, self.pump.set_state(self.actions['PUMP_OFF'])) # 4 self.assertTrue(self.sensor.measure() == float(100)) # 5 if self.pump.set_state(False): # 6 self.assertRaises(TypeError)
def test_integration_set_pump_state_fail(self): decider = Decider(target_height=3000, margin=500) pump = Pump('127.0.0.1', 8000) sensor = Sensor('127.0.0.1', 8000) pump.set_state = MagicMock(return_value=False) pump.get_state = MagicMock(return_value=Pump.PUMP_OFF) sensor.measure = MagicMock(return_value=3500) controller = Controller(sensor, pump, decider) result = controller.tick() self.assertEqual(result, False)
def test_tick(self): """ Testing Sensor.tick """ sensor = Sensor('127.0.0.1', '8000') sensor.measure = MagicMock(return_value=10) pump = Pump('127.0.0.1', '8000') pump.get_state = MagicMock(return_value='PUMP_OFF') pump.set_state = MagicMock(return_value='PUMP_OFF') decider = Decider(10, 0.1) decider.decide = MagicMock(return_value='PUMP_OFF') controller = Controller(sensor, pump, decider) self.assertTrue(controller.tick())
class PumpTests(unittest.TestCase): """ This class performs a unit test on Pump """ def setUp(self): """ This method does a setup for unit testing Pump """ self.pump = Pump('127.0.0.1', 1000) def test_set_state(self): """ This method tests the set_state for Pump """ self.pump.set_state = MagicMock(return_value=True) self.pump.set_state('PUMP_OUT') self.pump.set_state.assert_called_with('PUMP_OUT') def test_get_state(self): """ This method tests the get_state for Pump """ self.pump.get_state = MagicMock(return_value=1) self.assertEqual(self.pump.get_state(), 1)