Skip to content

[Discussion] Customisibility for pytest-twisted #64

@tyerq

Description

@tyerq

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:

  1. introducing pytest.mark.twisted that is checked by pytest_pyfunc_call that skips if the mark is absent;
  2. 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;
  3. 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;
  4. 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions