def get_formatted_name(first,last):
"""Generate a neatly formatted full name. """
full_name = first + ' ' + last
return full_name
from name_function import get_formatted_name
print("Enter 'q' at any time to quit.")
while True:
first = input("\nPlease give me at 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)
结果:
Enter 'q' at any time to quit.
Please give me at first name: li
Please give me a last name: ming
Neatly formatted name: li ming
Please give me at first name: liou
Please give me a last name: bei
Neatly formatted name: liou bei
Please give me at first name: zhang
Please give me a last name: fei
Neatly formatted name: zhang fei
Please give me at first name: zhao
Please give me a last name: yun
Neatly formatted name: zhao yun
Please give me at first name: q
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')
unittest.main()
结果:
.
----------------------------------------------------------------------
Ran 1 test in 0.002s
OK
Out[3]: <spydercustomize.IPyTesProgram at 0x19b1f96b5e0>
import unittest
from name_function11 import get_formatted_name1
class NamesTestCase1(unittest.TestCase):
"""测试name_function.py"""
def test_first_last_name(self):
"""能够正确地处理像Janis Joplin这样的姓名吗?"""
formatted_name = get_formatted_name1('janis','li', 'joplin')
self.assertEqual(formatted_name, 'janis li joplin')
unittest.main()
结果:
----------------------------------------------------------------------
Ran 2 tests in 0.004s
FAILED (failures=1)
Out[11]: <spydercustomize.IPyTesProgram at 0x19b1f9fb8e0>
import unittest
from name_function111 import get_formatted_name11
class NamesTestCase11(unittest.TestCase):
"""测试name_function.py"""
def test_first_last_name(self):
"""能够正确地处理像Janis Joplin这样的姓名吗?"""
formatted_name = get_formatted_name11('janis', 'joplin','li')
self.assertEqual(formatted_name, 'Janis Li Joplin')
unittest.main()
结果:
..
----------------------------------------------------------------------
Ran 2 tests in 0.003s
OK
Out[34]: <spydercustomize.IPyTesProgram at 0x19b1fa27700>
“”"
方法 用途
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中
“”"
from survey import AnonymousSurvey
question = "What language did you first learn to speak?"
my_survey = AnonymousSurvey(question)
my_survey.show_question()
print("Enter 'q' at any time to quit.\n")
while True:
response = input("Language: ")
if response == 'q':
break
my_survey.store_responses(response)
print("\nThank you to everyone who participated in the survey!")
my_survey.show_results()
结果:
What language did you first learn to speak?
Enter 'q' at any time to quit.
Language: English
Language: chinese
Language: English
Language: Franch
Language: q
Thank you to everyone who participated in the survey!
Survey results:
- English
- chinese
- English
- Franch
import unittest
from survey import AnonymousSurvey
class TestAnonymousSurvey(unittest.TestCase):
"""针对AnonymousSurvey类的测试"""
def test_store_single_response(self):
"""测试单个答案会被妥善地存储"""
question = "What language did you first learn to speak?"
my_survey = AnonymousSurvey(question)
my_survey.store_responses('English')
self.assertIn('English', my_survey.responses)
unittest.main()
结果:
...
----------------------------------------------------------------------
Ran 3 tests in 0.005s
OK
Out[36]: <spydercustomize.IPyTesProgram at 0x19b1fa4c1f0>
import unittest
from survey import AnonymousSurvey
class TestAnonymousSurvey(unittest.TestCase):
"""针对AnonymousSurvey类的测试"""
def test_store_single_response(self):
"""测试单个答案会被妥善地存储"""
question = "What language did you first learn to speak?"
my_survey = AnonymousSurvey(question)
my_survey.store_responses('English')
self.assertIn('English', my_survey.responses)
def test_store_three_responses(self):
"""测试三个答案都会被妥善地存储"""
question = "What language did you first learn to speak?"
my_survey = AnonymousSurvey(question)
responses = ['English','Spanish','Mandarin']
for response in responses:
my_survey.store_responses(response)
for response in responses:
self.assertIn(response, my_survey.responses)
unittest.main()
结果:
....
----------------------------------------------------------------------
Ran 4 tests in 0.006s
OK
Out[37]: <spydercustomize.IPyTesProgram at 0x19b1fa55cd0>
import unittest
from survey import AnonymousSurvey
class TestAnonymousSurvey(unittest.TestCase):
"""针对AnonymousSurvey类的测试"""
def setUp(self):
"""
创建一个调查对象和一组答案,供使用的测试方法使用
"""
question = "What language did you first learn to speak?"
self.my_survey = AnonymousSurvey(question)
self.responses = ["English",'Spanish','Mandarin']
def test_store_single_response(self):
"""测试单个答案会被妥善地存储"""
self.my_survey.store_responses(self.responses[0])
self.assertIn(self.responses[0], self.my_survey.responses)
# question = "What language did you first learn to speak?"
# my_survey = AnonymousSurvey(question)
# my_survey.store_responses('English')
# self.assertIn('English', my_survey.responses)
def test_store_three_responses(self):
"""测试三个答案都会被妥善地存储"""
for response in self.responses:
self.my_survey.store_responses(response)
for response in self.responses:
self.assertIn(response, self.my_survey.responses)
# question = "What language did you first learn to speak?"
# my_survey = AnonymousSurvey(question)
# responses = ['English','Spanish','Mandarin']
# for response in responses:
# my_survey.store_responses(response)
# for response in responses:
# self.assertIn(response, my_survey.responses)
unittest.main()
结果:
....
----------------------------------------------------------------------
Ran 4 tests in 0.005s
OK
Out[38]: <spydercustomize.IPyTesProgram at 0x19b1f89d670>
测试的目的:检验我们编写的代码是否满足功能要求,或者检测我们的代码具有哪些漏洞。
测试的对象:函数、类
测试的模块:unittest模块
测试类创建步骤:
1、导入模块unittest以及测试的函数
2、创建一个继承unittest.TestCase的类,
并写一系列方法对函数行为的不同方面进行测试
测试类的写法要求:
1、建立一个测试类测试类的名称最好包括test字眼,并且该类必须要继承unittest.TestCase类
2、该类中的方法的名称必须以test开头,
3、要调用合适的unittest模块中的合适方法,这里面调用了assertEqual():