简体   繁体   English

不同目录中函数的单元测试给出 AttributeError: module has no attribute

[英]unit test of function in different directory gives AttributeError: module has no attribute

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测试文件位于C:\path\to\some-test-classes\test_an_example.py

The class being tested is located at C:\\completely\\different\\path\\an_example.py正在测试的类位于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.问题可能出在C:\\completely\\different\\path\\an_example.py的结构中,因为C:\\completely\\different\\path\\an_example.py正在导入到C:\path\to\some-test-classes\test_an_example.py如下所示。

Here are the details:以下是详细信息:

TEST FILE:测试文件:

The test file located at C:\path\to\some-test-classes\test_an_example.py is:位于C:\path\to\some-test-classes\test_an_example.py的测试文件是:

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:正在测试的类位于C:\\completely\\different\\path\\an_example.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

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.从上面print(...)命令的结果可以看出,类an_example正在加载到test_an_example.py中,但未找到an_example类的someMethod(firstString, secondString)成员。

Your code works fine.您的代码工作正常。 The issue is that you never import the an_example class.问题是您从不导入an_example类。 In your test file you import an_example but you are importing the module.在您的测试文件中,您导入an_example但您正在导入模块。 You can actually see it in the traceback:您实际上可以在回溯中看到它:

AttributeError: module 'an_example' has no attribute 'someMethod' AttributeError:模块“an_example”没有属性“someMethod”

In order to import the class an_example you would need to do something like this:为了导入类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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 模块“test”没有属性“randhiletter” - AttributeError: module 'test' has no attribute 'randhiletter' AttributeError:模块“测试”没有属性“myfunc” - AttributeError: module 'test' has no attribute 'myfunc' 用于单元测试的python3:AttributeError:模块'__main__'没有属性“内核......” - python3 for unit test: AttributeError: module '__main__' has no attribute “kernel…” 安装functools给我AttributeError'模块'对象没有属性'compose' - Installing functools gives me AttributeError 'module' object has no attribute 'compose' 导入 theano 给出了 AttributeError: module 'theano' has no attribute 'gof' - Import theano gives the AttributeError: module 'theano' has no attribute 'gof' kds 库给出 AttributeError: module 'kds' has no attribute 'metrics' - kds library gives AttributeError: module 'kds' has no attribute 'metrics' 导入大熊猫会给出AttributeError'模块'对象没有属性'partial' - Importing pandas gives an AttributeError 'module' object has no attribute 'partial' Python 2.7错误:“ AttributeError:'模块'对象没有属性'Unit'” - Python 2.7 error: “AttributeError: 'module' object has no attribute 'Unit'” AttributeError:模块没有属性 - AttributeError: module has no attribute AttributeError:模块“ ...”没有属性“ ...” - AttributeError: module '…' has no attribute '…'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM