def test_setting_child(self): lootin = LootBag() with open("data/children.txt", "r") as children: self.assertFalse("Megan" in children.read()) lootin.set_child("Megan") self.assertTrue("Megan" in children.read())
class LootbagTest(unittest.TestCase): """testing class for the lootbag class""" @classmethod def setUpClass(self): self.bag = LootBag() def clean_up_files(self, file_name, item): """method to clean up files modified after testing Arguments: file_name {string} -- file to be cleaned item {string} -- item to be removed from file """ with open(file_name, 'r+') as target_file: t = target_file.read() target_file.seek(0) for line in t.split('\n'): if item not in line: target_file.write(line + '\n') target_file.truncate() def test_can_create_instance_of_LootBag(self): test_bag = LootBag() self.assertIsInstance(test_bag, LootBag) def test_childrens_toys_can_be_added_to_bag(self): self.bag.add_loot("test_toy", "test_child") with open('children.txt', 'r') as children: self.assertTrue("test_child" in children.read()) with open('toys.txt', 'r') as toys: self.assertTrue('test_toy' in toys.read()) self.clean_up_files('children.txt', 'test_child') self.clean_up_files('toys.txt', 'test_toy') def test_children_can_be_added_to_bag(self): """method to test that a child can be added to the database """ # test that test_child is not already in the bag with open("children.txt", "r") as children: self.assertFalse("test_child" in children.read()) # add test_child to the bag self.bag._add_child("test_child") # test that child was added with open("children.txt", "r") as children: self.assertTrue("test_child" in children.read()) # removes test_child from the list self.clean_up_files('children.txt', 'test_child') def test_childrens_toys_can_be_removed_from_bag(self): self.assertTrue(self.bag.remove_loot()) def test_can_get_list_of_children(self): self.assertTrue(self.bag.get_children()) def test_can_get_list_of_childs_loot(self): self.assertTrue(self.bag.get_childs_loot())
class TestLootBag(unittest.TestCase): @classmethod def setUpClass(self): self.bag = LootBag() def test_can_list_toys_from_bag_for_child(self): expected_result = 'Transformer' actual_result = self.bag.list_toys_for_child('Raffy') self.assertIn(expected_result, actual_result) def test_add_toy_to_bag(self): bag = LootBag() toy = 'Transformer' self.assertEqual(toy, bag.add_toy_to_bag('Transformer', 'Raffy'), "Toy added to bag.")
class TestBagOLoot(unittest.TestCase): @classmethod def setUpClass(self): self.bag = LootBag() def test_can_list_toys_from_bag_for_child(self): expected_result = "ball" actual_result = self.bag.list_toys_for_child("Mikey") self.assertIn(expected_result, actual_result) def test_add_toy_to_bag(self): bag = LootBag() toy = "ball" self.assertEqual(bag.add_toy_to_bag("ball", "Mikey"), "Toy added to bag")
class TestBagOLoot(unittest.TestCase): @classmethod def setUpClass(self): self.bag = LootBag() def test_can_list_toys_from_bag_for_child(self): expected_result = 'ball' actual_result = self.bag.list_toys_for_child('Mikey') self.assertIn(expected_result, actual_result) def test_add_toy_to_bag(self): toy = "Truck" toy_list = self.bag.list_toys_for_child("Mikey") self.assertNotIn(toy, toy_list) self.bag.add_toy_to_bag('truck', 'Mikey') self.bag.list_toys_for_child("Mikey") self.assertIn(toy, toy_list)
class TestLootBag(unittest.TestCase): @classmethod def setUpClass(self): self.bag = LootBag() def test_can_list_toys_from_bag_for_child(self): toy = "ball" mikey_toys = self.bag.list_child_toys("Mikey") self.assertIn(toy, mikey_toys) def test_setting_child(self): lootin = LootBag() with open("data/children.txt", "r") as children: self.assertFalse("Megan" in children.read()) lootin.set_child("Megan") self.assertTrue("Megan" in children.read()) def test_add_toy_to_bag(self): bag = LootBag() toy = "ball" self.assertEqual(bag.add_toy("ball", "Mikey"), "Toy added to bag")
def test_add_toy_to_bag(self): bag = LootBag() toy = "ball" self.assertEqual(bag.add_toy("ball", "Mikey"), "Toy added to bag")
def setUpClass(self): self.bag = LootBag()
def test_add_toy_to_bag(self): bag = LootBag() toy = 'Transformer' self.assertEqual(toy, bag.add_toy_to_bag('Transformer', 'Raffy'), "Toy added to bag.")
def test_can_create_instance_of_LootBag(self): test_bag = LootBag() self.assertIsInstance(test_bag, LootBag)
def setUpClass(self): print("Set up class") self.lootbag = LootBag()
class TestLootBag(unittest.TestCase): @classmethod def setUpClass(self): print("Set up class") self.lootbag = LootBag() def test_find_child(self): """Finds a child that already exists in the database """ child = "Sebastian" self.assertEqual(self.lootbag.find_child(child), 1) def test_find_add_child(self): """Attempts to find a child that already exists in the database, when that fails it creates a new child and returns the id """ new_name = string_generator() self.assertIsInstance(self.lootbag.find_child(new_name), int) def test_add_toy_existing_child(self): """Adds a new toy with an existing child. Also calls find_child method """ child = "Sebastian" toy = string_generator() self.assertIsInstance(self.lootbag.add_toy(child, toy), int) def test_add_toy_add_child(self): """Creates a new child and then adds a toy associated with that child to the database """ child = string_generator() toy = "Video Game" self.assertIsInstance(self.lootbag.add_toy(child, toy), int) def test_remove_toy(self): """Creates a new toy to assign to an existing child and then removes the toy from the database""" child = "Sebastian" toy = string_generator() self.lootbag.add_toy(toy, child) self.lootbag.remove_toy(child, toy) self.assertIsNone(self.lootbag.find_toy(child, toy)) def test_find_toy(self): """Creates a new toy and a new child and then finds the toy. Then removes that toy and child from the datbase.""" child = string_generator() toy = string_generator() self.lootbag.add_toy(toy, child) self.assertIsInstance(self.lootbag.find_toy(child, toy), tuple) self.lootbag.remove_toy(child, toy) self.assertIsNone(self.lootbag.find_toy(child, toy))