import unittest def get_formatted_city_name(city, country, population=''): '''fanhui'''
if population: full_name = city + ", " + country + ' - ' + 'population ' + str(population) else: full_name = city + ", " + country return full_name.title() class CityTestCase(unittest.TestCase): '''sadsa''' def test_cities(self): full_name = get_formatted_city_name('hz', 'china') self.assertEqual(full_name, "Hz, China") def test_cities_population(self): full_name = get_formatted_city_name('hz', 'china', 1200000) self.assertEqual(full_name, 'Hz, China - Population 1200000') unittest.main()
unittest Module 中的断言方法
方法 | 用途 |
assertEqual(a, b) | 核实 a == b |
assertNotEqual(a, b) | 核实 a != b |
assertTrue(x) | 核实x为True |
assertFalse(x) | 核实x为False |
assertIn(item , list ) | 核实item在list中 |
assertNotIn(item , list ) |
核实item不在list中 |