不同目录中功能的单位测试给出属性:模块没有属性

发布于 2025-02-07 01:19:10 字数 3137 浏览 0 评论 0原文

必须在下面更改哪些特定语法,以便在文件系统的一个部分中运行的单元测试以成功测试位于文件系统完全不同部分的类中的函数?

该测试文件位于c:\ path \ to \ some-test-classes \ test_an_example.py

所测试的类位于c:\\完全\\完全\\ path位于\\ an_example.py

问题可能在c:\\ altermy \\不同\\ path \\ an_example.py的结构中,因为c:\\完全\\不同\\ path \\ an_example.py正在导入到c:\ path \ to \ some-test-classes \ test_an_example.py.py,如下所示。

以下是详细信息:

测试文件:

位于c:\ path \ to \ some-test-classes \ test_an_example.py is:

import unittest
import subprocess

#Run the tests in this file by running the following command in the terminal:
#python -m unittest test_an_example.py

class TestCommandBuilder(unittest.TestCase):

  def test_someMethod(self):
    import sys
    sys.path.insert(0, 'C:\\completely\\different\\path\\')
    print('sys.path is: ', str(sys.path))
    import an_example
    print('90909090')
    firstString = "hello"
    secondString = ' there'
    returnBool = an_example.someMethod(firstString, secondString)
    self.assertTrue(returnBool)

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

类别的测试文件测试:

被测试的类位于c:\\完全\\不同\\路径\\ an_example.py.py,并包含以下内容:

class an_example:

  def __init__(self):  
    pass

  def someMethod(firstString, secondString):
    print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
    print("firstString is: ",firstString)
    print("secondString is: ",secondString)
    combinedString = firstString+secondString
    print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
    if combinedString == "hello there":
      return True
    else: 
      return False

当前错误:

当前,正在返回以下错误:

C:\path\to\some-test-classes>python -m unittest test_an_example.py
sys.path is:  ['C:\\completely\\different\\path\\', 'C:\\path\\to\\some-test-classes', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\DLLs', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\lib', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages']
90909090
E
 ======================================================================  
ERROR: test_someMethod (test_an_example.TestCommandBuilder)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\path\to\some-test-classes\test_an_example.py", line 62, in test_someMethod
    returnBool = an_example.someMethod(firstString, secondString)
AttributeError: module 'an_example' has no attribute 'someMethod'

----------------------------------------------------------------------
Ran 1 test in 0.006s

FAILED (errors=1)

从上面的print(...)命令的结果中可以看到,类an_example正在加载进入test_an_example.py,但是somemethod(第一串,第二串) an_example class的成员 class没有找到。

What specific syntax must be changed below in order for a unit test running in one part of a file system to successfully test a function in a class that is located in a completely different part of a file system?

The test file is located at C:\path\to\some-test-classes\test_an_example.py

The class being tested is located at C:\\completely\\different\\path\\an_example.py

The problem might be in the structure of C:\\completely\\different\\path\\an_example.py, because the C:\\completely\\different\\path\\an_example.py IS being imported into C:\path\to\some-test-classes\test_an_example.py as shown below.

Here are the details:

TEST FILE:

The test file located at C:\path\to\some-test-classes\test_an_example.py is:

import unittest
import subprocess

#Run the tests in this file by running the following command in the terminal:
#python -m unittest test_an_example.py

class TestCommandBuilder(unittest.TestCase):

  def test_someMethod(self):
    import sys
    sys.path.insert(0, 'C:\\completely\\different\\path\\')
    print('sys.path is: ', str(sys.path))
    import an_example
    print('90909090')
    firstString = "hello"
    secondString = ' there'
    returnBool = an_example.someMethod(firstString, secondString)
    self.assertTrue(returnBool)

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

CLASS BEING TESTED:

The class being tested is located at C:\\completely\\different\\path\\an_example.py and contains the following:

class an_example:

  def __init__(self):  
    pass

  def someMethod(firstString, secondString):
    print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
    print("firstString is: ",firstString)
    print("secondString is: ",secondString)
    combinedString = firstString+secondString
    print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
    if combinedString == "hello there":
      return True
    else: 
      return False

CURRENT ERROR:

Currently, the following error is being returned:

C:\path\to\some-test-classes>python -m unittest test_an_example.py
sys.path is:  ['C:\\completely\\different\\path\\', 'C:\\path\\to\\some-test-classes', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\DLLs', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\lib', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310', 'C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages']
90909090
E
 ======================================================================  
ERROR: test_someMethod (test_an_example.TestCommandBuilder)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\path\to\some-test-classes\test_an_example.py", line 62, in test_someMethod
    returnBool = an_example.someMethod(firstString, secondString)
AttributeError: module 'an_example' has no attribute 'someMethod'

----------------------------------------------------------------------
Ran 1 test in 0.006s

FAILED (errors=1)

As you can see from the results of the print(...) commands in the above, the class an_example IS being loaded into test_an_example.py, but the someMethod(firstString, secondString) member of the an_example class is NOT being found.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下伊人醉 2025-02-14 01:19:10

您的代码工作正常。问题是您永远不会导入an_example类。在您的测试文件中,您导入an_example,但是您正在导入模块。您实际上可以在追溯中看到它:

attributeError:模块'an_example'没有属性'somemethod'

,以便导入类an_example 您需要做这样的事情:

  def test_someMethod(self):
    import sys
    sys.path.insert(0, 'C:\\completely\\different\\path\\')
    print('sys.path is: ', str(sys.path))

    # import an_example  # remove this

    from an_example import an_example        # changed this

    print('90909090')
    firstString = "hello"
    secondString = ' there'
    returnBool = an_example.someMethod(firstString, secondString)
    self.assertTrue(returnBool)

Your code works fine. The issue is that you never import the an_example class. In your test file you import an_example but you are importing the module. You can actually see it in the traceback:

AttributeError: module 'an_example' has no attribute 'someMethod'

In order to import the class an_example you would need to do something like this:

  def test_someMethod(self):
    import sys
    sys.path.insert(0, 'C:\\completely\\different\\path\\')
    print('sys.path is: ', str(sys.path))

    # import an_example  # remove this

    from an_example import an_example        # changed this

    print('90909090')
    firstString = "hello"
    secondString = ' there'
    returnBool = an_example.someMethod(firstString, secondString)
    self.assertTrue(returnBool)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文