I have a function that takes in a path to an.mp3 or.m4a file and returns a string containing information extracted from the file using TinyTag (import statement removed for brevity):
def create_search_term(path_to_song):
tag = TinyTag.get(path_to_song)
artist = tag.artist
album = tag.album
search_term = f"{album} {artist} Album Cover"
return search_term
I've tested this manually, and it works with a real MP3 file path on my computer. However, I'm trying to add unit tests to this project. Is there a best practice to go writing a unit test for such a function?
A sample use case would be "/path/to/bruce/springsteen/born_to_run.mp3"
taken as an input, and "Born to Run Album Cover"
as a return value.
Patch out TinyTag
to produce a sample response, and then test the function result against that response:
from mock import patch
def test_create_search_term():
with patch("__main__.TinyTag") as mock_tinytag:
mock_tinytag.get.return_value.artist = "Bruce Springsteen"
mock_tinytag.get.return_value.album = "Born to Run"
assert create_search_term("foo") == "Born to Run Bruce Springsteen Album Cover"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.