def test_objective_function_fail(self): """Tests if exception is thrown when objective function is not callable""" pass_a_list = [1, 2, 3, 4, 5] pass_a_dict = {'a': 0, 'b': 2, 'c': 4} with self.assertRaises(TypeError): plt_env = PlotEnvironment(self.optimizer, pass_a_list, 100) with self.assertRaises(TypeError): plt_env = PlotEnvironment(self.optimizer, pass_a_dict, 100)
def setUp(self): """Sets up test fixtures""" self.optimizer = GlobalBestPSO(n_particles=10, dimensions=3, options={ 'c1': 0.5, 'c2': 0.3, 'w': 0.9 }) self.class_methods = [ 'get_cost_history', 'get_pos_history', 'get_velocity_history', 'optimize', 'reset' ] self.get_specs = lambda idx: [ x for i, x in enumerate(self.class_methods) if i != idx ] self.plt_env = PlotEnvironment(self.optimizer, sphere_func, 1000)
def test_getters_pso(mock_pso, attributes): """Tests an instance of the PSO class and should raise an exception when the class has missing attributes""" idx, _ = attributes with pytest.raises(AttributeError): m = mock_pso(idx) PlotEnvironment(m, sphere_func, 100)
def plot_environment(): """Returns a PlotEnvironment instance""" optimizer = GlobalBestPSO(10, 3, options={'c1': 0.5, 'c2': 0.3, 'w': 0.9}) return PlotEnvironment(optimizer, sphere_func, 1000)
def plot_environment(): """Returns a PlotEnvironment instance""" optimizer = GlobalBestPSO(10, 3, options={"c1": 0.5, "c2": 0.3, "w": 0.9}) return PlotEnvironment(optimizer, sphere_func, 1000)
def test_attribute_reset_fail(self): """Test if exception is thrown if reset attr is missing""" m = Mock(spec=self.get_specs(4)) with self.assertRaises(AttributeError): plt_env = PlotEnvironment(m, sphere_func, 100)
def test_optimizer_getters_get_velo_history_fail(self): """Test if exception is thrown if get_velocity_history is missing""" m = Mock(spec=self.get_specs(2)) with self.assertRaises(AttributeError): plt_env = PlotEnvironment(m, sphere_func, 100)