From 6d2424b3c4dfb3222b964e08c9b09d7875ee146f Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Sat, 1 Jan 2022 15:59:04 +0100 Subject: Refactored SoloTool integration tests to use fixtures --- solo_tool_integrationtest.py | 78 +++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 45 deletions(-) diff --git a/solo_tool_integrationtest.py b/solo_tool_integrationtest.py index 2fa923a..b9ce62e 100644 --- a/solo_tool_integrationtest.py +++ b/solo_tool_integrationtest.py @@ -1,12 +1,29 @@ -from solo_tool import SoloTool -from player_mock import Player as MockPlayer import pathlib import shutil +import pytest -def test_playerControls(): - mockPlayer = MockPlayer() - uut = SoloTool(mockPlayer) - +from solo_tool import SoloTool +from player_mock import Player as MockPlayer + +@pytest.fixture +def mockPlayer(): + return MockPlayer() + +@pytest.fixture +def uut(mockPlayer): + return SoloTool(mockPlayer) + +@pytest.fixture(autouse=True) +def copyTestFiles(tmp_path): + testFiles = [ + "test.flac", + "test.mp3", + "test_session.json" + ] + for f in testFiles: + shutil.copy(pathlib.Path(f), tmp_path) + +def test_playerControls(uut, mockPlayer): assert mockPlayer.state == MockPlayer.STOPPED assert uut.isPlaying() == False uut.play() @@ -31,14 +48,12 @@ def test_playerControls(): uut.setPlaybackVolume(0.5) assert mockPlayer.volume == 0.5 -def test_addAndSetSongs(): +def test_addAndSetSongs(uut, mockPlayer): songs = [ "test.flac", "test.mp3" ] - mockPlayer = MockPlayer() - uut = SoloTool(mockPlayer) - + for s in songs: uut.addSong(s) assert mockPlayer.currentSong == None @@ -47,13 +62,11 @@ def test_addAndSetSongs(): uut.setSong(i) assert mockPlayer.currentSong == songs[i] -def test_nextAndPreviousSong(): +def test_nextAndPreviousSong(uut, mockPlayer): songs = [ "test.flac", "test.mp3" ] - mockPlayer = MockPlayer() - uut = SoloTool(mockPlayer) for s in songs: uut.addSong(s) @@ -71,14 +84,12 @@ def test_nextAndPreviousSong(): uut.nextSong() assert mockPlayer.currentSong == songs[1] -def test_addAndSetAbLimits(): +def test_addAndSetAbLimits(uut, mockPlayer): song = "test.flac" abLimits = [ [0.2, 0.4], [0.1, 0.3] ] - mockPlayer = MockPlayer() - uut = SoloTool(mockPlayer) uut.addSong(song) uut.setSong(0) @@ -115,7 +126,7 @@ def test_addAndSetAbLimits(): uut.tick() assert mockPlayer.position == 0.1 -def test_multipleSongsAndAbLimits(): +def test_multipleSongsAndAbLimits(uut, mockPlayer): songs = [ "test.flac", "test.mp3" @@ -124,8 +135,6 @@ def test_multipleSongsAndAbLimits(): [0.2, 0.4], [0.5, 0.7] ] - mockPlayer = MockPlayer() - uut = SoloTool(mockPlayer) for s in songs: uut.addSong(s) @@ -148,13 +157,11 @@ def test_multipleSongsAndAbLimits(): uut.tick() assert mockPlayer.position == l[0] -def test_storeAbLimitsWithoutSong(): +def test_storeAbLimitsWithoutSong(uut, mockPlayer): song = "test.flac" abLimit = [0.2, 0.4] overflow = abLimit[1] + 0.1 default = 0.0 - mockPlayer = MockPlayer() - uut = SoloTool(mockPlayer) mockPlayer.position = overflow uut.setAbLimitEnable(True) @@ -197,17 +204,7 @@ def test_storeAbLimitsWithoutSong(): uut.tick() assert mockPlayer.position == abLimit[0] -def copyTestFiles(tmp_path): - testFiles = [ - "test.flac", - "test.mp3", - "test_session.json" - ] - for f in testFiles: - shutil.copy(pathlib.Path(f), tmp_path) - def test_loadAndSaveSession(tmp_path): - copyTestFiles(tmp_path) mockPlayer = MockPlayer() uut = SoloTool(mockPlayer) @@ -226,22 +223,17 @@ def test_loadAndSaveSession(tmp_path): assert loadedSession == savedSession -def test_addInexistentFile(): +def test_addInexistentFile(uut, mockPlayer): song = "not/a/real/file" - mockPlayer = MockPlayer() - uut = SoloTool(mockPlayer) - uut.addSong(song) uut.setSong(0) assert mockPlayer.currentSong == None -def test_getters(): +def test_getters(uut, mockPlayer): song = "test.flac" abLimit = [0.2, 0.4] - mockPlayer = MockPlayer() - uut = SoloTool(mockPlayer) uut.addSong(song) uut.setSong(0) @@ -257,15 +249,13 @@ def test_getters(): mockPlayer.position = 0.8 assert uut.getPlaybackPosition() == 0.8 -def test_setTemporaryLimits(): +def test_setTemporaryLimits(uut, mockPlayer): song = "test.flac" abLimits = [ [0.2, 0.4], [0.1, 0.4] ] overflow = 0.5 - mockPlayer = MockPlayer() - uut = SoloTool(mockPlayer) uut.setAbLimitEnable(True) mockPlayer.position = overflow @@ -278,12 +268,10 @@ def test_setTemporaryLimits(): uut.tick() assert mockPlayer.position == abLimits[1][0] -def test_jumpToA(): +def test_jumpToA(uut, mockPlayer): song = "test.flac" abLimits = (0.2, 0.4) initialPosition = 0.8 - mockPlayer = MockPlayer() - uut = SoloTool(mockPlayer) mockPlayer.position = initialPosition -- cgit v1.2.3