Python 错误:https://www (unittest.loader.\u未通过测试)进行单元测试时
当前正在尝试为特定函数编写单元测试。错误如下所示:Python 错误:https://www (unittest.loader.\u未通过测试)进行单元测试时,python,python-3.x,unit-testing,python-3.7,python-unittest,Python,Python 3.x,Unit Testing,Python 3.7,Python Unittest,当前正在尝试为特定函数编写单元测试。错误如下所示: E ====================================================================== ERROR: https://www (unittest.loader._FailedTest) ---------------------------------------------------------------------- AttributeError: module '__main_
E
======================================================================
ERROR: https://www (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'https://www'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
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()
虽然函数本身没有在测试函数中被调用,但我正在尝试在测试中初始化一个类Hasher
。注释掉初始化行将导致程序运行
class Test(unittest.TestCase):
def test_YT(self):
self.H = Hasher()
self.assertTrue(True)
该类的代码如下所示:
E
======================================================================
ERROR: https://www (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'https://www'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
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()
类初始化中没有任何东西表明它正在使用https://www“
,因此不确定此错误来自何处
我的进口产品的形式如下:
from Hasher import *
from Downloader import *
我现在的文件结构是:
从我的模块导入中使用
几乎从来都不是一个好主意*
。这可能会导致与从其他模块导入的名称冲突、由于使用了错误的函数或类而导致的错误以及不必要的副作用。尝试始终只导入所需的对象。使用诸如pylint或flake8之类的工具,或者IDE中的内置提示,可以收到类似问题的通知
在这个具体的例子中,downloader import*中的
语句很可能导致了问题 你是如何进行测试的?这似乎是测试无法加载某些文件-是否有执行初始化的\uuuu init.py\uuuu
,或者是sut加载的未显示的其他模块?@MrBeanBremen刚刚对问题进行了更改<代码>\uuuu init\uuuuu.py
中没有任何代码。乍一看,我会说错误来自下载程序导入。尝试注释掉导入和相关代码,并检查错误是否仍然存在。通常,导入*几乎从来都不是一个好主意。如果可能的话,尝试用特定导入来替换。从Hasher导入Hasher更改为,从Downloader导入Downloader更改为,似乎可以解决问题。谢谢你的建议!如果你把它写在回信里,我会接受的。