Skip to content
Advertisement

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:pathtosome-test-classestest_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:pathtosome-test-classestest_an_example.py as shown below.

Here are the details:

TEST FILE:

The test file located at C:pathtosome-test-classestest_an_example.py is:

JavaScript

CLASS BEING TESTED:

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

JavaScript

CURRENT ERROR:

Currently, the following error is being returned:

JavaScript

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.

Advertisement

Answer

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:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement