【发布时间】:2021-09-24 22:09:30
【问题描述】:
我对 Python 还很陌生。我正在开发一个小型 Python 项目,结构如下:
artwork_grabber/
|
|-- artwork_grabber/
| |-- __init__.py
| |-- helpers.py
|
|-- tests/
| |-- __init__.py
| |-- test_module.py
|
|-- README
__init__.py 两个文件都没有任何内容。
helpers.py文件包含几个函数,其中之一如下:
from os import path
from tinytag import TinyTag
def create_search_term(file_path_to_song):
"""
Takes in a file path to a song and returns a phrase that will be used to search for the song's corresponding album artwork.
:param file_path_to_song: a file path to an .mp3 or .m4a file.
:type file_path_to_song: `string`, required.
:return: an object of type string that represents the search term to be used when finding album artwork for song file passed into the function.
:rtype: `string`.
"""
if str(path.isfile(file_path_to_song)):
tag = TinyTag.get(file_path_to_song)
album = tag.get_album()
artist = tag.get_artist()
term = f"{artist} {album} Album Cover"
return term
else:
return False
我想使用模拟对象库为create_search_term() 编写一个测试。在test_module.py 函数中,我有以下内容:
from unittest import TestCase
from mock import patch
import unittest
from artwork_grabber.helpers import create_search_term
class UnitTests(TestCase):
mock_song_info = {
"album": "A Deeper Understanding",
"artist": "The War On Drugs"
}
# patch where the function is USED, not where it is DEFINED
@mock.patch('artwork_grabber.helpers.create_search_term', return_value=mock_song_info)
def test_create_search_term(self, mock_song):
actual_result = create_search_term(mock_song_info)
expected_result = "A Deeper Understanding The War On Drugs Album Cover"
self.assertEqual(actual_result, expected_result,
"Expected the search terms to match.")
if __name__ == "__main__":
unittest.main()
问题是当我从终端运行python test_module.py 时(pwd 输出/path/to/artwork_grabber/tests),我收到以下错误:
Traceback (most recent call last):
File "test_module.py", line 5, in <module>
from artwork_grabber.artwork_grabber import create_search_term
ModuleNotFoundError: No module named 'artwork_grabber'
知道我可能缺少什么吗?我看过几个关于使用 Mock 的教程,但它们似乎没有帮助。
【问题讨论】:
-
与问题本身无关,但仅供参考,看起来您正在模拟要测试的功能,因此使您的单元测试无用,因为您没有经历真实的功能
标签: python python-3.x mocking python-unittest