Python Learning Note Day 08
今天是Python学习第八天,主要学习第十一章的内容:主要是利用Python 的unittest 进行代码测试,针对函数和类进行测试。这一部分是在学PHP的时候没有接触过的,相对比较新,算是一个新的收获。
在今天的学习过程中,遇到一个目前尚未解决的问题,那就是在写测试的时候,测试脚本在PyCharm中运行的时候报错,而直接在命令行中运行就正常测试:
import unittest from name_function import get_formatted_name class NamesTestCase(unittest.TestCase): """测试name_function.py""" def test_first_last_name(self): """能够正确地处理像Janis Joplin这样的姓名吗?""" formatted_name = get_formatted_name('janis', 'joplin') self.assertEqual(formatted_name, 'Janis Joplin') def test_first_last_middle_name(self): """能够正确地处理像Wolfgang Amadeus Mozart这样的姓名吗?""" formatted_name = get_formatted_name( 'wolfgang', 'mozart', 'amadeus') self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart') unittest.main()
在命令行中运行如下:
而在PyCharm的调试工具中如下:
在Stackoverflow上找到一个解决方法就是把以上代码改成:
import unittest from name_function import get_formatted_name class NamesTestCase(unittest.TestCase): """测试name_function.py""" def test_first_last_name(self): """能够正确地处理像Janis Joplin这样的姓名吗?""" formatted_name = get_formatted_name('janis', 'joplin') self.assertEqual(formatted_name, 'Janis Joplin') def test_first_last_middle_name(self): """能够正确地处理像Wolfgang Amadeus Mozart这样的姓名吗?""" formatted_name = get_formatted_name( 'wolfgang', 'mozart', 'amadeus') self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart') if __name__ == '__main__': unittest.main()
发现一切正常,看了看手册上的测试方法是改过以后的方法,但是前面的方法在命令行中运行正常,这是一个待解决的问题。
本来打算今天看另外一本《Python基础教程(第3版)》把现在学的这本书基础部分补充一下,但是公司事情较多,自己今天又写了另外两篇博文,时间就有点不够了,所以这个工作只有推迟到明天了。
大纲
相关练习代码
from name_function import get_formatted_name print("Enter 'q' at any time to quit.") while True: first = input("\nPlease give me a first name: ") if first == 'q': break last = input("Please give me a last name: ") if last == 'q': break formatted_name = get_formatted_name(first, last) print("\tNeatly formatted name: " + formatted_name + '.')
import unittest from name_function import get_formatted_name class NamesTestCase(unittest.TestCase): """测试name_function.py""" def test_first_last_name(self): """能够正确地处理像Janis Joplin这样的姓名吗?""" formatted_name = get_formatted_name('janis', 'joplin') self.assertEqual(formatted_name, 'Janis Joplin') def test_first_last_middle_name(self): """能够正确地处理像Wolfgang Amadeus Mozart这样的姓名吗?""" formatted_name = get_formatted_name( 'wolfgang', 'mozart', 'amadeus') self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart') # if __name__ == '__main__': # unittest.main() # 以上代码针对用pycharm进行测试 # 以下代码针对在命令行里运行 unittest.main()
# 11-1 城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名。这 # 个函数返回一个格式为 City, Country 的字符串,如 Santiago, Chile。将这个函数存储 # 在一个名为 city _functions.py 的模块中。 # 创建一个名为 test_cities.py 的程序,对刚编写的函数进行测试(别忘了,你需要导 # 入模块 unittest 以及要测试的函数)。编写一个名为 test_city_country()的方法,核实 # 使用类似于'santiago'和'chile'这样的值来调用前述函数时,得到的字符串是正确的。 # 运行 test_cities.py,确认测试 test_city_country()通过了。 # 11-2 人口数量:修改前面的函数,使其包含第三个必不可少的形参 population,并 # 返回一个格式为 City, Country – population xxx 的字符串,如 Santiago, Chile – # population 5000000。运行 test_cities.py,确认测试 test_city_country()未通过。 # 修改上述函数,将形参 population 设置为可选的。再次运行 test_cities.py,确认测 # 试 test_city_country()又通过了。 # 再编写一个名为 test_city_country_population()的测试,核实可以使用类似于 # 'santiago'、'chile'和'population=5000000'这样的值来调用这个函数。再次运行 # test_cities.py,确认测试 test_city_country_population()通过了。 import unittest from city_country import city_country class CityTestCase(unittest.TestCase): """测试city_country函数""" def test_city_country(self): back=city_country('santiago','chile') self.assertEqual(back,'Santiago, Chile') def test_city_country_population(self): back=city_country('santiago','chile',10000) self.assertEqual(back,'Santiago, Chile - population 10000') unittest.main()
# 各种断言方法 # 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中 # 你只能在继承unittest.TestCase的 # 类中使用这些方法 from survey import AnonymousSurvey question = 'What language do you like ?' survey = AnonymousSurvey(question) survey.show_question() print('Enter \'q\' anytime to quit') while True: ans = input('Language: ') if ans.lower() == 'q': break survey.store_response(ans) survey.show_results()
# def city_country(city,country): # back=city.title()+', '+country.title() # return back def city_country(city,country,population=None): if population: back=city.title()+', '+country.title()+' - population '+str(population) else: back = city.title() + ', ' + country.title() return back
# 11-3 雇员:编写一个名为 Employee 的类,其方法__init__()接受名、姓和年薪,并 # 将它们都存储在属性中。编写一个名为 give_raise()的方法,它默认将年薪增加 5000 # 美元,但也能够接受其他的年薪增加量。 # 为 Employee 编写一个测试用例,其中包含两个测试方法:test_give_default_ # raise()和 test_give_custom_raise()。使用方法 setUp(),以免在每个测试方法中都创 # 建新的雇员实例。运行这个测试用例,确认两个测试都通过了。 class Employee(): """雇员类""" def __init__(self,fname,lname,salary): self.fname=fname self.lname=lname self.salary=salary def give_rise(self,add_salary=5000): """给雇员加年薪,默认5000""" self.salary+=add_salary
# def get_formatted_name(first, last): # """返回一个整洁姓名""" # full_name = first + ' ' + last # return full_name.title() # def get_formatted_name(first, middle, last): # """生成整洁的姓名""" # full_name = first + ' ' + middle + ' ' + last # return full_name.title() def get_formatted_name(first, last, middle=''): """生成整洁的姓名""" if middle: full_name = first + ' ' + middle + ' ' + last else: full_name = first + ' ' + last return full_name.title()
class AnonymousSurvey(): """收集匿名调查问卷的答案""" def __init__(self, question): """存储一个问题,并为存储答案做准备""" self.question = question self.responses = [] def show_question(self): """显示调查问卷""" print(self.question) def store_response(self, new_response): """存储单份调查答卷""" self.responses.append(new_response) def show_results(self): """显示收集到的所有答卷""" print("Survey results:") for response in self.responses: print('- ' + response)
import unittest from employee import Employee class TestEmployee(unittest.TestCase): """测试employee.py(Employee)""" def setUp(self): self.fname='Kiwi' self.lname='Qing' self.salary=5000 self.employeeObj=Employee(self.fname,self.lname,self.salary) def test_give_default_raise(self): """测试是否是默认加薪后的年薪""" self.employeeObj.give_rise() raised_salary=self.employeeObj.salary hope_salary=self.salary+5000 self.assertEqual(hope_salary,raised_salary) def test_give_custom_raise(self): """测试是否是自定义加薪后的年薪""" self.employeeObj.give_rise(1000) raised_salary = self.employeeObj.salary hope_salary=self.salary+1000 self.assertEqual(hope_salary,raised_salary) unittest.main()
import unittest from survey import AnonymousSurvey class TestAnonymousSurvey(unittest.TestCase): """针对AnonymousSurvey的测试""" def test_single_ans_store(self): """测试单个答案会被妥善地存储""" question='What\'s your favorite subject ?' survey=AnonymousSurvey(question) survey.store_response('english') self.assertIn('english',survey.responses) def test_store_three_responses(self): question = 'What\'s your favorite subject ?' survey=AnonymousSurvey(question) ans=['English','Math','Physics','PE'] for item in ans: survey.store_response(item) for item in survey.responses: self.assertIn(item,ans) unittest.main()
import unittest from survey import AnonymousSurvey class TestAnonymousSurvey(unittest.TestCase): """针对AnonymousSurvey的测试""" def setUp(self): question = 'What\'s your favorite subject ?' self.survey = AnonymousSurvey(question) self.ans=['English','Math','Physics','PE'] def test_single_ans_store(self): """测试单个答案会被妥善地存储""" self.survey.store_response(self.ans[0]) self.assertIn(self.ans[0],self.survey.responses) def test_store_three_responses(self): for item in self.ans: self.survey.store_response(item) for item in self.survey.responses: self.assertIn(item,self.ans) unittest.main()
最近都挺累的吧,晚上还得做题,愿努力有回报吧!BTW Python确实是一门很nice的语言,写起来真的很舒服,相见恨晚。
版权声明:
作者:Kiwi
链接:https://www.qingwei.tech/programe-develops/910.html
来源:清渭技术小站
文章版权归作者所有,未经允许请勿转载。
THE END
二维码
共有 0 条评论