dpytest icon indicating copy to clipboard operation
dpytest copied to clipboard

document how to make a bot fixture

Open yobuntu opened this issue 5 years ago • 3 comments

this test pass:

@pytest.mark.asyncio
async def test_chat_muse():
    bot = commands.Bot(command_prefix='?')
    bot.add_cog(Settings(bot))
    bot.add_cog(Chat(bot))
    dpytest.configure(bot, 1, 1, 1)
    config = get_config()
    channel = config.channels[0]
    member = config.members[0]
    guild = config.guilds[0]

    await message(f"?chat muse {member.mention}", channel, member)

    dpytest.verify_message(f"muse set to {member.name}")

this test does not pass:

@pytest.fixture
def chat_bot():
    chat_bot = commands.Bot(command_prefix='?')
    chat_bot.add_cog(Settings(bot))
    chat_bot.add_cog(Chat(bot))
    return chat_bot

@pytest.mark.asyncio
async def test_chat_muse(chat_bot):
    dpytest.configure(bot, 1, 1, 1)
    config = get_config()
    channel = config.channels[0]
    member = config.members[0]
    guild = config.guilds[0]

    await message(f"?chat muse {member.mention}", channel, member)

    dpytest.verify_message(f"muse set to {member.name}")

I know this is not the place to ask for support (but then where is the right place ?).

I spent quite a lot of time looking the Talos code to get how to do it but i can't make it. From what i understand, the problem come from the bot created in the fixture is not running in the test, and so the message in the test never get an answer.

Do you plan to make some code exemples (i agree to help making some documentation because your discord test framework looks very promising, and, i look forward to be able to use it).

Thank you

yobuntu avatar Oct 22 '19 13:10 yobuntu

Currently, there's no separate support server or such, I may make one soon. From the code you posted, the line async def test_chat_muse(bot): won't invoke the fixture chat_bot, the parameter needs to be exactly chat_bot not bot. I will likely look to set up docs soon, but it's a big task while I'm busy with college.

CraftSpider avatar Oct 22 '19 13:10 CraftSpider

You are right, it was a mistake with my copy/pasting to make the minamal exemple, but even with the right fixture the second test don't pass (i edited my exemple code). Please don't wory or rush to answer me. I'm coding this bot on my spare time for fun. Good luck with the college !

yobuntu avatar Oct 22 '19 15:10 yobuntu

It has been a long time, but I personally figured out the issue recently while writing the tests for this library. The bot fixture should use the event_loop fixture and create the bot like Bot("!", loop=event_loop). This is because pytest_asyncio doens't necessarily use the default event loop, so when the bot gets default it will end up putting its tasks on the wrong event loop, so they never run. This will be included when I add a tutorial for pytest integration

CraftSpider avatar Apr 11 '20 02:04 CraftSpider

Lib and doc have been updated, I'm closing.

Sergeileduc avatar Dec 08 '22 08:12 Sergeileduc