1

Currently trying to write a unittest for a particular function. The error is shown below:

E
======================================================================
ERROR: https://www (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'https://www'

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

While the funtion itself has not been called within the Test function, I am trying to initialize a class Hasher inside the test. Commenting out the initialization line leads to the program running.

class Test(unittest.TestCase): 

def test_YT(self): 
        self.H = Hasher()
        self.assertTrue(True)

The code for the class is shown below:

class Hasher:

    import hashlib 

    def __init__(self, hash_algo='md5'): 
        print('we getting here')
        # TODO: support for more hash algos
        self.hash_algo = hash_algo 


    def hash_file(self, filename):

        return hashlib.md5(open(filename, 'rb').read()).hexdigest()


    def compare_file_txt(self, filename, hash_txt_file):
        # Useful for when there is an MD5 txt in the folder

        hash1 = self.hash_file(filename)

        if hash1 == open(hash_txt_file).readline():
            return True

        return False


    def YT_create_hash(self, link, output_loc='test_hash.txt'):

        DL = Downloader()
        file_name = DL.YT_extract(link)
        hash_txt = self.hash_file(os.getcwd() + '/' + file_name)
        o_file = open(output_loc, 'w')
        o_file.write(hash_txt)
        o_file.close()

There's nothing in the class initialization which indicates that it is using 'https://www', so not really sure where this error is coming from.

My imports are in the form of:

from Hasher import *
from Downloader import *

And my file structure right now is:

enter image description here

SDG
  • 2,260
  • 8
  • 35
  • 77
  • How are you running the test? This looks like the test could not load some of the files - is there an `__init.py__` that does some initialization, or is some other module loaded by your sut that is not shown? – MrBean Bremen Mar 27 '20 at 18:24
  • @MrBeanBremen Just made changes to the question. `__init__.py` does not have any code inside of it. – SDG Mar 29 '20 at 01:35
  • At a first guess I would say the error comes from the downloader import. Try to comment out the import and the related code and check if the error persists. Generally it is almost never a good idea to `import *`. If possible, try to replace this with specific imports. – MrBean Bremen Mar 29 '20 at 01:59
  • Changing to `from Hasher import Hasher` and `from Downloader import Downloader` seemed to fix the problem. Thanks for the advice! If you put it in an answer, I will accept it. – SDG Mar 30 '20 at 04:31

1 Answers1

1

It is almost never a good idea to use from my module import *. This can cause clashes with names imported from other modules, bugs due to the wrong function or class used, and unwanted side effects.
Try to always import only needed objects. Use tools like pylint or flake8, or the built-in hints in your IDE to be notified of similar issues.

In this concrete case, the statement from downloader import * most probably caused the problem.

MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46