Python unit test - NameError: name 'upper' is not defined

Python unit test - NameError: name 'upper' is not defined

使用 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()

我的命令退出文本:

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)

所有文件都在一个目录中,我仍然不明白为什么不起作用。无法在互联网上搜索相同的问题((

如果您尝试将 'hello' 转换为大写,python 中的函数是 string.upper() 我不带参数见下文:

test='hello'
up=test.upper()
print(up)
# prints HELLO

import File_P_third 替换为 from File_P_third import upper_text。以这种方式调用您的函数 result = upper_text(text)。还要确保文件 File_P_third.py 和 test_upper.py 都在同一目录中。

下面是文件的完整代码 File_P_third.py:

import unittest
from File_P_third import upper_text


class TestUpper(unittest.TestCase):
    """docstring for ClassName"""
    def test_one_word(self):
        text = 'hello!'
        result = upper_text(text)
        self.assertEqual(result, 'HELLO!')
        

if __name__ == '__main__':
    unittest.main()