def test_animal_constructor(self): animal_test = Animal("test") self.assertEqual("test", animal_test.name, "simple constructor naming test") self.assertEqual(100, animal_test.get_energy(), "simple constructor energy test") animal_test = Animal("test", init_parameters={ "name": "changed", "energy": 500 }) self.assertEqual("changed", animal_test.name, "init parameters naming test") self.assertEqual(500, animal_test.get_energy(), "init parameters energy test") self.assertEqual(self.name, self.animal.name, "setup class test")
class TestAnimal(unittest.TestCase): def setUp(self): self.name = "test_animal" self.animal = Animal(self.name) def test_animal_constructor(self): animal_test = Animal("test") self.assertEqual("test", animal_test.name, "simple constructor naming test") self.assertEqual(100, animal_test.get_energy(), "simple constructor energy test") animal_test = Animal("test", init_parameters={ "name": "changed", "energy": 500 }) self.assertEqual("changed", animal_test.name, "init parameters naming test") self.assertEqual(500, animal_test.get_energy(), "init parameters energy test") self.assertEqual(self.name, self.animal.name, "setup class test") def test_animal_saying(self): captured = StringIO() sys.stdout = captured self.animal.say() sys.stdout = sys.__stdout__ captured_value = captured.getvalue() self.assertEqual("Hello, I'm Animal and my name is test_animal.\n", captured_value, "Animal saying test") def test_animal_actions(self): self.animal.fly() self.animal.swim() self.animal.run() self.assertEqual(100, self.animal.get_energy(), "Animal energy should not change")