diff --git a/HISTORY.md b/HISTORY.md index 72bc5a3..9b5e7c9 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,13 @@ # History +## 0.6.0 + +- First version for working with dpytest>=2.X.X + Changes have been made in backend, factories, websocket +- Change the README +- Change the setup.py for python >= 3.8 +- Changing in doc + ## 0.5.3 - Docs: Add vertical spacing for functions & methods diff --git a/discord/ext/test/__init__.py b/discord/ext/test/__init__.py index 8ad4a3b..b69aaf0 100644 --- a/discord/ext/test/__init__.py +++ b/discord/ext/test/__init__.py @@ -3,7 +3,7 @@ __author__ = "Rune Tynan" __license__ = "MIT" __copyright__ = "Copyright 2018-2019 CraftSpider" -__version__ = "0.5.3" +__version__ = "0.6.0" from . import backend from .runner import * diff --git a/docs/conf.py b/docs/conf.py index cdcd83c..7f4bae5 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -22,7 +22,7 @@ author = 'CraftSpider' # The full version, including alpha/beta/rc tags -release = '0.5.3' +release = '0.6.0' # -- General configuration --------------------------------------------------- diff --git a/docs/tutorials/using_pytest.rst b/docs/tutorials/using_pytest.rst index f6b92b7..a4bd8bc 100644 --- a/docs/tutorials/using_pytest.rst +++ b/docs/tutorials/using_pytest.rst @@ -27,27 +27,50 @@ Putting all this together, we can rewrite our previous tests to look like this: .. code:: python + import discord + import discord.ext.commands as commands + from discord.ext.commands import Cog, command import pytest + import pytest_asyncio import discord.ext.test as dpytest - @pytest.fixture - def bot(event_loop): - bot = ... # However you create your bot, make sure to use loop=event_loop - dpytest.configure(bot) - return bot + class Misc(Cog): + @command() + async def ping(self, ctx): + await ctx.send("Pong !") + + @command() + async def echo(self, ctx, text: str): + await ctx.send(text) + + + @pytest_asyncio.fixture + async def bot(): + intents = discord.Intents.default() + intents.members = True + intents.message_content = True + b = commands.Bot(command_prefix="!", + intents=intents) + await b._async_setup_hook() # setup the loop + await b.add_cog(Misc()) + + dpytest.configure(b) + return b @pytest.mark.asyncio async def test_ping(bot): await dpytest.message("!ping") - assert dpytest.verify().message().contains().content("Ping:") + assert dpytest.verify().message().content("Pong !") @pytest.mark.asyncio - async def test_foo(bot): - await dpytest.message("!hello") - assert dpytest.verify().message().content("Hello World!") + async def test_echo(bot): + await dpytest.message("!echo Hello world") + assert dpytest.verify().message().contains().content("Hello") + + Much less writing the same code over and over again, and tests will be automatically run by pytest, then the results output in a nice pretty format once it's done. @@ -66,25 +89,44 @@ An example ``conftest.py`` might look like this: .. code:: python - import pytest + import glob + import os + import pytest_asyncio + import discord + import discord.ext.commands as commands import discord.ext.test as dpytest - @pytest.fixture - def bot(event_loop): - bot = ... # However you create your bot, make sure to use loop=event_loop - dpytest.configure(bot) - return bot + @pytest_asyncio.fixture + async def bot(): + intents = discord.Intents.default() + intents.members = True + intents.message_content = True + b = commands.Bot(command_prefix="!", + intents=intents) + await b._async_setup_hook() + dpytest.configure(b) + return b - def pytest_sessionfinish(): - # Clean up attachment files - files = glob.glob('./dpytest_*.dat') - for path in files: + @pytest_asyncio.fixture(autouse=True) + async def cleanup(): + yield + await dpytest.empty_queue() + + + def pytest_sessionfinish(session, exitstatus): + """ Code to execute after all tests. """ + + # dat files are created when using attachements + print("\n-------------------------\nClean dpytest_*.dat files") + fileList = glob.glob('./dpytest_*.dat') + for filePath in fileList: try: - os.remove(path) - except Exception as e: - print(f"Error while deleting file {path}: {e}") + os.remove(filePath) + except Exception: + print("Error while deleting file : ", filePath) + With that, you should be ready to use ``dpytest`` with your bot. @@ -96,13 +138,6 @@ Troubleshooting Make sure your tests take a parameter with the exact same name as the fixture, pytest runs them based on name, including capitalization. -- I get an instance of my bot, but it just gets stuck / doesn't do anything - when I ``await`` - -Make sure you passed ``event_loop`` to your bot when creating it. Pytest-asyncio -does not necessarily use the default event loop, so your bot may not actually -be running. - -------------------- This is currently the end of the tutorials. Take a look at the `Runner Documentation`_ to see all the things you can diff --git a/setup.cfg b/setup.cfg index 2ba087a..43f960d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.5.3 +current_version = 0.6.0 commit = True tag = True diff --git a/setup.py b/setup.py index 255b4d7..c26d99d 100644 --- a/setup.py +++ b/setup.py @@ -39,10 +39,10 @@ url="https://github.com/CraftSpider/dpytest", packages=["discord.ext.test"], classifiers=[ - "Programming Language :: Python :: 3.6", - "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Development Status :: 3 - Alpha", diff --git a/tests/conftest.py b/tests/conftest.py index 216ad79..61a860d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,22 +1,14 @@ import glob import os -import pytest import pytest_asyncio import discord import discord.ext.commands as commands -import discord.ext.test as test +import discord.ext.test as dpytest from discord.client import _LoopSentinel -@pytest.fixture -def client(event_loop): - c = discord.Client(loop=event_loop) - test.configure(c) - return c - - @pytest_asyncio.fixture -async def bot(request, event_loop): +async def bot(request): intents = discord.Intents.default() intents.members = True intents.message_content = True @@ -36,14 +28,14 @@ async def bot(request, event_loop): for extension in mark.args: await b.load_extension("tests.internal." + extension) - test.configure(b) + dpytest.configure(b) return b @pytest_asyncio.fixture(autouse=True) async def cleanup(): yield - await test.empty_queue() + await dpytest.empty_queue() def pytest_sessionfinish(session, exitstatus):