【发布时间】:2021-05-17 20:17:28
【问题描述】:
使用 Python 3.x。运行以下代码的测试用例时,我收到错误 - NameError: name 'upper' is not defined。我附上要测试的文件,File_P_third文件是代码,单元测试文件test_upper。
文件File_P_third:
def upper_text(text):
return text.upper()
文件test_upper:
import unittest
import File_P_third
class TestUpper(unittest.TestCase):
"""docstring for ClassName"""
def test_one_word(self):
text = 'hello!'
result = upper.upper_text(text)
self.assertEqual(result, 'HELLO!')
if __name__ == '__main__':
unittest.main()
我的 cmd 的退出文本:
D:\Дохуя программист\Projects>python test_upper.py
E
======================================================================
ERROR: test_one_word (__main__.TestUpper)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_upper.py", line 9, in test_one_word
result = upper.upper_text(text)
NameError: name 'upper' is not defined
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
所有文件都在一个目录中,我仍然无法理解为什么不起作用。网上搜不到同样的问题((
【问题讨论】:
-
嗯,是的。
upper未定义。upper应该在upper.upper_text(text)中是什么? -
您的 test_upper 文件是否完整?您应该使用 File_P_third。 upper_text() 或从 File_P_third 导入 upper_text 然后 upper_text(text)
-
用
File_P_third.upper_text(text)替换upper.upper_text(text),它应该可以工作
标签: python unit-testing nameerror