import asyncio import sys from pathlib import Path from unittest.mock import Mock import glob import os import pytest from pytest_mock import MockerFixture # Add source to PATH such that imports within src modules are properly resolved. SRC_PATH = str(Path(__file__).parent / '..' / 'src') sys.path.insert(0, SRC_PATH) from bot import Music @pytest.fixture(scope="session", autouse=True) def global_teardown(): yield logs = glob.glob('*.log') for log in logs: os.remove(log) @pytest.fixture def bot(mocker: MockerFixture): bot_mock = mocker.patch('discord.ext.commands.Bot', autospec=True) bot_mock.loop = Mock() yield bot_mock @pytest.fixture def mbot(bot): mbot_mock = Music(bot) yield mbot_mock mbot_mock.cog_unload() @pytest.fixture def ctx(mocker: MockerFixture): ctx_mock = mocker.patch('discord.ext.commands.Context', autospec=True) ctx_mock.voice_client.stop = lambda: ctx_mock.voice_client.play.call_args.kwargs["after"](None) return ctx_mock @pytest.fixture def event_loop(): loop = asyncio.get_event_loop() yield loop pending = asyncio.all_tasks(loop=loop) asyncio.ensure_future(asyncio.gather(*pending))