I have a fairly specific use case with my project:
I'm using both Twisted (with asyncioreactor setup) and native asyncio awaits somewhat interchangeably, but when it came to testing, I've gotten into troubles with pytest_twisted's and pytest_asyncio's incompatibility.
The issue is that when using pytest_twisted it becomes impossible for pytest_asyncio to make a new loop for every test, since it's basically running inside a global twisted loop.
So basically, for my use case I would want to have pytest-twisted applied only in tests I need it to be applied and making a separate loop for every test and getting rid of it after test is done.
I've dealt with this by doing some tinkering, which resulted in:
- introducing
pytest.mark.twisted that is checked by pytest_pyfunc_call that skips if the mark is absent;
- adding a
--twisted-marked-only cmdopt that pytest_collection_modifyitems checks and if it's False (which it is by default to preserve current behaviour) adds the twisted mark to any test that doesn't have it already;
- adding a
--twisted-scope cmdopt and a twisted_greenlet_fixture_factory function that is called in pytest_configure that depending on the scope:
- behaves as if it wasn't there (except the resulting fixture isn't autouse) if
scope="session" (default);
- makes a fixture with the specified scope that
dels twisted.internet.reactor before and after creating a new reactor and mocks it with it's created reactor for the users otherwise;
- in
pytest_runtest_setup adding the twisted_greenlet fixture for every test that has the pytest.mark.twisted (like autouse, that you can opt out of :з ).
I'll make a pull request for it, but the real question is:
Is it of any use for anybody except me and could this the direction the project goes in?
I have a fairly specific use case with my project:
I'm using both Twisted (with asyncioreactor setup) and native asyncio awaits somewhat interchangeably, but when it came to testing, I've gotten into troubles with
pytest_twisted's andpytest_asyncio's incompatibility.The issue is that when using
pytest_twistedit becomes impossible forpytest_asyncioto make a new loop for every test, since it's basically running inside a global twisted loop.So basically, for my use case I would want to have pytest-twisted applied only in tests I need it to be applied and making a separate loop for every test and getting rid of it after test is done.
I've dealt with this by doing some tinkering, which resulted in:
pytest.mark.twistedthat is checked bypytest_pyfunc_callthat skips if the mark is absent;--twisted-marked-onlycmdopt thatpytest_collection_modifyitemschecks and if it'sFalse(which it is by default to preserve current behaviour) adds thetwistedmark to any test that doesn't have it already;--twisted-scopecmdopt and atwisted_greenlet_fixture_factoryfunction that is called inpytest_configurethat depending on the scope:scope="session"(default);delstwisted.internet.reactorbefore and after creating a new reactor and mocks it with it's created reactor for the users otherwise;pytest_runtest_setupadding thetwisted_greenletfixture for every test that has thepytest.mark.twisted(like autouse, that you can opt out of :з ).I'll make a pull request for it, but the real question is:
Is it of any use for anybody except me and could this the direction the project goes in?