Skip to content

Commit 60f24cd

Browse files
authored
Speed up a couple of tests (#5799)
Speed up tests by using `unittest.TestCase` for tests that don't require directory setup This PR improves test performance by switching several test classes from `BeetsTestCase` to standard `unittest.TestCase` when they don't require the directory setup and teardown overhead. The changes focus on test classes that: 1. Only test utility functions 2. Don't need temporary directories 3. Don't interact with the filesystem The PR removes unnecessary imports of `BeetsTestCase` and adds direct imports of `unittest` where needed. This change reduces the test execution time by avoiding the expensive setup/teardown steps for tests that don't require them.
2 parents e439c04 + 5900282 commit 60f24cd

13 files changed

+32
-35
lines changed

test/plugins/test_art.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import os
2020
import shutil
21+
import unittest
2122
from typing import TYPE_CHECKING
2223
from unittest.mock import patch
2324

@@ -1012,7 +1013,7 @@ def test_deinterlace_and_resize(self):
10121013
self._assert_image_operated(self.IMG_348x348, self.RESIZE_OP, True)
10131014

10141015

1015-
class DeprecatedConfigTest(BeetsTestCase):
1016+
class DeprecatedConfigTest(unittest.TestCase):
10161017
"""While refactoring the plugin, the remote_priority option was deprecated,
10171018
and a new codepath should translate its effect. Check that it actually does
10181019
so.
@@ -1030,7 +1031,7 @@ def test_moves_filesystem_to_end(self):
10301031
assert isinstance(self.plugin.sources[-1], fetchart.FileSystem)
10311032

10321033

1033-
class EnforceRatioConfigTest(BeetsTestCase):
1034+
class EnforceRatioConfigTest(unittest.TestCase):
10341035
"""Throw some data at the regexes."""
10351036

10361037
def _load_with_config(self, values, should_raise):

test/plugins/test_beatport.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""Tests for the 'beatport' plugin."""
1616

17+
import unittest
1718
from datetime import timedelta
1819

1920
from beets.test import _common
@@ -585,7 +586,7 @@ def test_genre_applied(self):
585586
assert track.genre == test_track.genre
586587

587588

588-
class BeatportResponseEmptyTest(BeetsTestCase):
589+
class BeatportResponseEmptyTest(unittest.TestCase):
589590
def _make_tracks_response(self):
590591
results = [
591592
{

test/plugins/test_lastgenre.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import pytest
2020

21-
from beets import config
2221
from beets.test import _common
2322
from beets.test.helper import BeetsTestCase
2423
from beetsplug import lastgenre
@@ -32,12 +31,12 @@ def setUp(self):
3231
def _setup_config(
3332
self, whitelist=False, canonical=False, count=1, prefer_specific=False
3433
):
35-
config["lastgenre"]["canonical"] = canonical
36-
config["lastgenre"]["count"] = count
37-
config["lastgenre"]["prefer_specific"] = prefer_specific
34+
self.config["lastgenre"]["canonical"] = canonical
35+
self.config["lastgenre"]["count"] = count
36+
self.config["lastgenre"]["prefer_specific"] = prefer_specific
3837
if isinstance(whitelist, (bool, (str,))):
3938
# Filename, default, or disabled.
40-
config["lastgenre"]["whitelist"] = whitelist
39+
self.config["lastgenre"]["whitelist"] = whitelist
4140
self.plugin.setup()
4241
if not isinstance(whitelist, (bool, (str,))):
4342
# Explicit list of genres.
@@ -463,11 +462,10 @@ def mock_fetch_artist_genre(self, obj):
463462
lastgenre.LastGenrePlugin.fetch_album_genre = mock_fetch_album_genre
464463
lastgenre.LastGenrePlugin.fetch_artist_genre = mock_fetch_artist_genre
465464

466-
# Configure
467-
config["lastgenre"] = config_values
468-
469465
# Initialize plugin instance and item
470466
plugin = lastgenre.LastGenrePlugin()
467+
# Configure
468+
plugin.config.set(config_values)
471469
item = _common.item()
472470
item.genre = item_genre
473471

test/plugins/test_musicbrainz.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""Tests for MusicBrainz API wrapper."""
1616

17+
import unittest
1718
from unittest import mock
1819

1920
import pytest
@@ -665,7 +666,7 @@ def test_track_disambiguation(self):
665666
assert t[1].trackdisambig == "SECOND TRACK"
666667

667668

668-
class ArtistFlatteningTest(BeetsTestCase):
669+
class ArtistFlatteningTest(unittest.TestCase):
669670
def _credit_dict(self, suffix=""):
670671
return {
671672
"artist": {

test/plugins/test_subsonicupdate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""Tests for the 'subsonic' plugin."""
22

3+
import unittest
34
from urllib.parse import parse_qs, urlparse
45

56
import responses
67

78
from beets import config
8-
from beets.test.helper import BeetsTestCase
99
from beetsplug import subsonicupdate
1010

1111

@@ -24,7 +24,7 @@ def _params(url):
2424
return parse_qs(urlparse(url).query)
2525

2626

27-
class SubsonicPluginTest(BeetsTestCase):
27+
class SubsonicPluginTest(unittest.TestCase):
2828
"""Test class for subsonicupdate."""
2929

3030
@responses.activate

test/plugins/test_the.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""Tests for the 'the' plugin"""
22

3+
import unittest
4+
35
from beets import config
4-
from beets.test.helper import BeetsTestCase
56
from beetsplug.the import FORMAT, PATTERN_A, PATTERN_THE, ThePlugin
67

78

8-
class ThePluginTest(BeetsTestCase):
9+
class ThePluginTest(unittest.TestCase):
910
def test_unthe_with_default_patterns(self):
1011
assert ThePlugin().unthe("", PATTERN_THE) == ""
1112
assert (

test/test_files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def test_hardlink_changes_path(self):
200200
assert self.i.path == util.normpath(self.dest)
201201

202202

203-
class HelperTest(BeetsTestCase):
203+
class HelperTest(unittest.TestCase):
204204
def test_ancestry_works_on_file(self):
205205
p = "/a/b/c"
206206
a = ["/", "/a", "/a/b"]

test/test_importer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ def test_choose_second_candidate(self):
924924
assert self.lib.albums().get().album == "Applied Album MM"
925925

926926

927-
class InferAlbumDataTest(BeetsTestCase):
927+
class InferAlbumDataTest(unittest.TestCase):
928928
def setUp(self):
929929
super().setUp()
930930

@@ -1220,7 +1220,7 @@ def add_item_fixture(self, **kwargs):
12201220
return item
12211221

12221222

1223-
class TagLogTest(BeetsTestCase):
1223+
class TagLogTest(unittest.TestCase):
12241224
def test_tag_log_line(self):
12251225
sio = StringIO()
12261226
handler = logging.StreamHandler(sio)

test/test_logging.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@
33
import logging as log
44
import sys
55
import threading
6+
import unittest
67
from io import StringIO
78

89
import beets.logging as blog
910
import beetsplug
1011
from beets import plugins, ui
1112
from beets.test import _common, helper
12-
from beets.test.helper import (
13-
AsIsImporterMixin,
14-
BeetsTestCase,
15-
ImportTestCase,
16-
PluginMixin,
17-
)
13+
from beets.test.helper import AsIsImporterMixin, ImportTestCase, PluginMixin
1814

1915

20-
class LoggingTest(BeetsTestCase):
16+
class LoggingTest(unittest.TestCase):
2117
def test_logging_management(self):
2218
l1 = log.getLogger("foo123")
2319
l2 = blog.getLogger("foo123")

test/test_query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def test_invalid_query(self):
373373
dbcore.query.RegexpQuery("year", "199(")
374374

375375

376-
class MatchTest(BeetsTestCase):
376+
class MatchTest(unittest.TestCase):
377377
def setUp(self):
378378
super().setUp()
379379
self.item = _common.item()
@@ -811,7 +811,7 @@ def test_match_slow_after_set_none(self):
811811
self.assertInResult(item, matched)
812812

813813

814-
class NotQueryMatchTest(BeetsTestCase):
814+
class NotQueryMatchTest(unittest.TestCase):
815815
"""Test `query.NotQuery` matching against a single item, using the same
816816
cases and assertions as on `MatchTest`, plus assertion on the negated
817817
queries (ie. assert q -> assert not NotQuery(q)).

0 commit comments

Comments
 (0)