单元测试在 Python IDE PyCharm 中失败
Unittest failed in Python IDE PyCharm
我试图在Python中做一个简单的单元测试,但我不知道为什么测试失败了。
我制作了三个文件:
- name_function.py 其中我有一个接收两个参数的函数(名字,姓氏)和return 连接的名称。
def get_formatted_name(first, last):
full_name = first + ' ' + last
return full_name.title()
- names.py 其中要求用户输入名字和姓氏或 q 退出。之后调用函数 get_formatted_name 并打印连接的名称。
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()
- 在这个window中,我运行cmd命令(见附件Capture_1) .
- 在 cmd I 运行 命令中(见附件 Capture_2 和 Capture_3).
- 我不明白我的错误在哪里?在Capture_3看看我在运行考试时得到了什么
- 我用的是Python3.7.2,我用的IDEPython是PyCharm.
Capture_1
Capture_2
Capture_3
我觉得你的代码不错。我 运行 它在我的机器上运行得很好。我唯一注意到的是 python -m unittest
没有找到您的测试。一个快速的解决方法是在单元测试文件的末尾添加以下内容。
if __name__ == '__main__':
unittest.main()
然后,您可以运行使用以下命令。 python test_name_function.py
由于您具体询问的是 PyCharm,因此您只需要这样做:
- 从单元测试文件中删除最后一行:
unittest.main()
- 或者按照@Zykerd 的建议将其变成
这个:
if __name__ == '__main__':
unittest.main()
- 右键单击 PyCharm 和 select
Run Unittests in test_name_function
中的测试
两者都有效(即不调用 main,或调用主名称检查。
这是因为假定您 运行正在使用交互式解释器,并且在出现异常时它因 SystemExit 而失败。但是,就像@Zykerd 的评论中提到的
if __name__ == '__main__':
unittest.main()
以上假设脚本是从命令行而不是交互式解释器 运行 来解决这个问题的。
干杯!
请在此处查看更多说明:Tests succeed, still get traceback
我试图在Python中做一个简单的单元测试,但我不知道为什么测试失败了。 我制作了三个文件:
- name_function.py 其中我有一个接收两个参数的函数(名字,姓氏)和return 连接的名称。
def get_formatted_name(first, last):
full_name = first + ' ' + last
return full_name.title()
- names.py 其中要求用户输入名字和姓氏或 q 退出。之后调用函数 get_formatted_name 并打印连接的名称。
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()
- 在这个window中,我运行cmd命令(见附件Capture_1) .
- 在 cmd I 运行 命令中(见附件 Capture_2 和 Capture_3).
- 我不明白我的错误在哪里?在Capture_3看看我在运行考试时得到了什么
- 我用的是Python3.7.2,我用的IDEPython是PyCharm.
Capture_1 Capture_2 Capture_3
我觉得你的代码不错。我 运行 它在我的机器上运行得很好。我唯一注意到的是 python -m unittest
没有找到您的测试。一个快速的解决方法是在单元测试文件的末尾添加以下内容。
if __name__ == '__main__':
unittest.main()
然后,您可以运行使用以下命令。 python test_name_function.py
由于您具体询问的是 PyCharm,因此您只需要这样做:
- 从单元测试文件中删除最后一行:
unittest.main()
- 或者按照@Zykerd 的建议将其变成
这个:
if __name__ == '__main__':
unittest.main()
- 右键单击 PyCharm 和 select
Run Unittests in test_name_function
中的测试
两者都有效(即不调用 main,或调用主名称检查。
这是因为假定您 运行正在使用交互式解释器,并且在出现异常时它因 SystemExit 而失败。但是,就像@Zykerd 的评论中提到的
if __name__ == '__main__':
unittest.main()
以上假设脚本是从命令行而不是交互式解释器 运行 来解决这个问题的。
干杯!
请在此处查看更多说明:Tests succeed, still get traceback