测试用例-单元测试
django不同的版本之间的差异还是挺大的,大到足以影响正常的模块包调用,我在这些博客地址中找到的core是django中的包,它里面的子模块包在不同的版本之中是不一样的名字
测试用例,示例代码
函数、类,示例
# -*- encoding=utf8 def get_format_name(first_n, last_n, middle_n=""): """ 拼接姓、名,字符串 :return: """ if middle_n: full_name = "{} {} {}".format(first_n, middle_n, last_n) else: full_name = "{} {}".format(first_n, last_n) return full_name.title() def get_city_country(city_n, country_n, population_count=0): """ 拼接city,country字符串 :return: """ if population_count: city_country_full = "{},{}-{}".format(city_n, country_n, population_count) else: city_country_full = "{},{}".format(city_n, country_n) return city_country_full.title() class AnonymousSurvey: """test a class example""" def __init__(self, question): self.question = question self.response = set() def show_question(self): """show the question""" print(self.question) def store_response(self, new_response): """store the response into a list""" self.response.add(new_response) def show_results(self): """show all the collection response""" print("Survey results:") for response in self.response: print("- {}".format(response)) class Employee: def __init__(self, first_n, last_n, salary): self.first_n = first_n self.last_n = last_n self.salary = salary def give_raise(self, plus_salary=0): if plus_salary: self.salary += plus_salary else: self.salary += 5000
编写测试用例,测试上面的函数、类
import unittest from name_function import get_format_name, get_city_country, \ AnonymousSurvey, Employee class NameTestCase(unittest.TestCase): def setUp(self): """like kind of init function, the program run setUp function first, then run the function startswith'test' """ question = "what language did you first learn to speak" self.my_survey = AnonymousSurvey(question) self.responses_li = ["English", "Spanish", "Japanese", "French"] self.original_salary = 30000 self.employee = Employee("John", "Black", self.original_salary) def test_first_last_name(self): """ test first & last name :return: """ formatted_name = get_format_name("ains", "joplin") self.assertEqual(formatted_name, "Ains Joplin") # self.assertEqual(get_format_name("ains", "joplin"), "Ains Joplin") def test_first_middle_last_name(self): """ test first middle last name :return: """ formatted_name = get_format_name("ains", "joplin", "mozart") self.assertEqual(formatted_name, "Ains Mozart Joplin") def test_city_country_name(self): """ test city country name :return: """ full_name = get_city_country("santiago", "chile") self.assertEqual(full_name, "Santiago,Chile") def test_city_country_population(self): """ test city,country,population :return: """ full_name = get_city_country("santiago", "chile", "population=2000000") self.assertEqual(full_name, "Santiago,Chile-Population=2000000") def test_anonymous_survey_single_response(self): """test anonymous single response""" self.my_survey.store_response("Spanish") self.assertIn("Spanish", self.my_survey.response) def test_anonymous_survey_multiple_response(self): """test anonymous multiple response""" for response in self.responses_li: self.my_survey.store_response(response) for response in self.responses_li: self.assertIn(response, self.my_survey.response) def test_employee_give_default_raise(self): """test employee raise default salary""" self.employee.give_raise() self.assertEqual(self.employee.salary, self.original_salary+5000) def test_employee_give_custom_raise(self): """test employee raise custom salary""" custom_salary = 500 self.employee.give_raise(custom_salary) self.assertEqual(self.employee.salary, self.original_salary+custom_salary) unittest.main()
以上贴出来的代码,都可以运行,亲自测试过