class TestCharacterAttacks(unittest.TestCase): """ Generally things work like this: -> setUpClass() -> setUp() -> test_something() -> tearDown() -> setUp() -> test_something_else() -> tearDown() -> tearDownClass() You should write a seperate TestCase derived class for each behavior. For example this is TestCharacterAttacks which tests the behavior of attacking characters. For magic you might also have a TestCharacterMagicAttacks, and TestCharacterMagicHeals, etc. I'd be careful running these with Rincewind since he's rather terrible at magic. Maybe TestCharacterWhines would be a better fit. """ @classmethod def setUpClass(cls): """ Will run this, then run all tests """ pass @classmethod def tearDownClass(cls): """ Will run all tests, then call this """ pass def setUp(self): """ Will run this before each test_foo method """ self.victim = Character(char_name="Rincewind", char_class="Wizard") self.lord = Character(char_name="Ventenari", char_class="Lord", hit_points=1000, armor_class=500) def tearDown(self): """ Clean up file contexts and more after each test""" pass def test_attack(self): """ Victim takes 1 point of damage when attacked """ self.lord.attack(self.victim) self.assertEquals(self.victim.hit_points, 9) def test_otherstuff(self): pass def test_somemore_stuff(self): pass
def _test_successful_attack(self, armor_class, expected_damage): attacker = Character() defender = Character(armor_class=3) damage = attacker.attack(defender) self.assertEqual(damage, expected_damage)
def test_attack_hits_if_roll_greater_than_armor_class(self): random.seed(1) # Will give a roll of 3 attacker = Character() defender = Character(armor_class=2) damage = attacker.attack(defender) self.assertTrue(damage > 0)