我试着用Python做一个简单的unittest,但是我不知道为什么测试失败了。
我做了三份文件:
name_函数.py
其中我有一个函数接收两个参数(
名字,姓氏
)并返回连接的名称。
def get_formatted_name(first, last):
full_name = first + ' ' + last
return full_name.title()
名称.py
要求用户输入名字和姓氏,或
问
放弃。在那之后
获取格式化的名称
调用并打印连接的名称。
from name_function import get_formatted_name
print("\n Enter 'q' at any time to quit.")
while True:
first = input("\n Please give me a first name : ")
if first == 'q':
break
last = input("\n Please give me a second name : ")
if last == 'q':
break
formatted_name = get_formatted_name(first, last)
print("\n\t Neatly formatted name : " + formatted_name + '.')
test_name_function.py测试名称
测试功能的地方。
import unittest
from name_function import get_formatted_name
class NamesTestCase(unittest.TestCase):
def test_first_last_name(self):
formatted_name = get_formatted_name('Clint', 'Eastwood')
self.assertEqual(formatted_name, 'Clint Eastwood')
unittest.main()
在这个窗口,我跑
命令
命令(请参见附加
捕获1
).
在
命令
我运行命令(参见附件
捕获2
和
捕获3
).
我不明白我的错误在哪里?在Capture_3中,查看我运行测试时获得的信息。
我使用Python 3.7.2,我使用的IDE Python是PyCharm。