Skip to content

Commit

Permalink
Simplify the 'pytest' setup
Browse files Browse the repository at this point in the history
- Don't explicitly state 'scope="function"'.
- Use 'tmp_path_factory' instead of doing it manually.
- Set up 'conftest.py' for importing fixtures automatically.
  • Loading branch information
Arav K. committed Jul 28, 2024
1 parent 87841e8 commit 2778716
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
13 changes: 6 additions & 7 deletions beets/test/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@


@pytest.fixture(scope="session")
def resource_dir() -> Path:
def resource_dir(pytestconfig: pytest.Config) -> Path:
"""
A fixture returning the resource directory for tests.
"""

return Path(__file__).parents[2] / "test" / "rsrc"
return pytestconfig.rootpath / "test" / "rsrc"


@pytest.fixture(scope="function")
@pytest.fixture
def config(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
Expand Down Expand Up @@ -67,9 +67,9 @@ def config(
beets.config._materialized = False


@pytest.fixture(scope="function")
@pytest.fixture
def library(
tmp_path: Path,
tmp_path_factory: pytest.TempPathFactory,
config: Configuration,
monkeypatch: pytest.MonkeyPatch,
) -> Iterator[Library]:
Expand All @@ -78,8 +78,7 @@ def library(
"""

# Beets needs a location to store library contents.
lib_dir = tmp_path / "lib_dir"
lib_dir.mkdir(exist_ok=False)
lib_dir = tmp_path_factory.mktemp("lib_dir")
monkeypatch.setenv("BEETSDIR", str(lib_dir))

# Create the Beets library object.
Expand Down
20 changes: 20 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This file is part of beets.
# Copyright 2024, Arav K.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""
Automatic configuration for `pytest`.
"""

# Pull in Beets' test fixtures.
pytest_plugins = "beets.test.fixtures"
5 changes: 1 addition & 4 deletions test/plugins/test_fish.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@
from beets.library import Library
from beetsplug.fish import FishPlugin

# Tell 'pytest' to pull in Beets' test fixtures.
pytest_plugins = "beets.test.fixtures"


@pytest.fixture(scope="function")
@pytest.fixture
def fish_plugin() -> FishPlugin:
"""
A fixture returning an instance of the `fish` plugin.
Expand Down

0 comments on commit 2778716

Please sign in to comment.