Closed ctmbl closed 1 year ago
You are entirely right.
We should automatically empty the queue between each test, but I don't have time right now to investigate how to write a teardown
@ctmbl actually, it works, with our conftest.py ! 😁
we use a cleanup fixture
@pytest_asyncio.fixture(autouse=True)
async def cleanup():
yield
await dpytest.empty_queue()
Actually the yield is not necessary
@pytest_asyncio.fixture(autouse=True)
async def cleanup():
# yield
await dpytest.empty_queue()
works just fine.
I also try to put the empty queue in the bot fixture
yield b
await dpytest.empty_queue()
and it works !
So, closing, I guess ?
feel free to reopen if you can't make it work.
@Sergeileduc
@ctmbl actually, it works, with our conftest.py !
oh my bad I didn't see this one, and actually this is how I fixed it: by adding await dpytest.empty_queue()
in the bot fixture.
And sure the yield is not necessary because emptying the queue at the setup of test B results in the same behavior that emptying it at the teardown of test A.
However, using autouse
fixture is considered as a bad habit because "explicit is better than implicit". And for a new user to dpytest
this kind of mistake results in overly difficult to understand test crashes.
A good solution to me would be to update the documentation to move it after the yield in the bot fixture, this makes sense as this is actually a cleanup after the test. Also, scoping the bot fixture to other scopes than function
will result in expected behavior (because the cleaning is done in the bot fixture).
Another really good one would be to implement it by default in dpytest
, in the bot config for example, but I don't know if this is easily doable.
I could propose a PR for either solution if you're interested in!
sure, you can PR with the bot fixture solution
....
yield b
await dpytest.empty_queue() # add a comment to explain why we do that
both in conftest.py and in the doc
you are probably right, the cleanup fixture is a lot of code, just for 1 line that can be written in the bot fixture.
So yeah
@Sergeileduc could you just reopen this if I open a PR that fix it?
I don't know if this is a bug or a feature but without explicitly saying it, the message queue is at "module" scope (or even more global) which when using pytest (for example) is really annoying.
I'll illustrate it with the example from dpytest doc (only slightly modified: I renamed the
bot
fixture toconfig_bot
: a weird habit of mine to always rename fixture object in test, feels like functions calls I guess :man_shrugging: )as expected this example pass:
However just modifying a bit
test_ping
(sending two commands) will crashtest_echo
which seems weird: a test shouldn't influence another one:because the AssertionError dump isn't really clear (related to #115) I'll use
dpytest.get_message()
to debug mytest_echo
(the one which has crashed):and we get:
Here we are: in
test_echo
that doesn't call!ping
tehre is a "Pong !" response in message queueI'm aware of the
dpytest.empty_queue
to clear the message queue, but I'd expect the configuration from dpytest to be fucntion scoped. Ideally I'd like to call thisdpytest.empty_queue
in test teardown: for example after yieldingb
in theconfig_bot
fixture but for some reason an async fixture can't yield and then teardown the test properly, at least I didn't manage to do it :confused:I'd really like your thoughts and help on that subject!