ERROR: https://www (unittest.loader._FailedTest) while Unit Testing

Viewed 5072

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

1 Answers

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.

Related