Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration option for a custom event loop policy and/or custom event loop class? #20

Closed
leifurhauks opened this issue Apr 11, 2016 · 4 comments

Comments

@leifurhauks
Copy link

It would be useful when testing an application that uses a custom implementation of asyncio.AbstractEventLoop to be able to provide a custom event loop policy and/or custom event loop via a configuration option.

It is of course already possible to set the event loop policy globally with asyncio.set_event_loop_policy (e.g. in pytest_configure) or to override the event_loop fixture so that it returns a loop of the desired class, so perhaps this would be unnecessary.

@Tinche
Copy link
Member

Tinche commented Apr 11, 2016

I agree being able to test using a different event loop can be useful.

I'd argue the approach most in the spirit of pytest would be to override the event_loop fixture. This will make the pytest.mark.asyncio marker use the overriden loop as well. This should be documented in the README, along with an example.

@imposeren
Copy link

maybe something like fixture factory may be used?

def event_loop_fixture_factory(default_policy=None):
    @pytest.fixture
    def event_loop_fixture(request):
        """Create an instance of the default event loop for each test case."""
        policy = default_policy or asyncio.get_event_loop_policy()

        policy.get_event_loop().close()

        event_loop = policy.new_event_loop()
        policy.set_event_loop(event_loop)

        request.addfinalizer(event_loop.close)
        return event_loop

    return event_loop

event_loop = event_loop_fixture_factory()

@Tinche
Copy link
Member

Tinche commented May 10, 2016

Hi,

we've already sketched out a solution in #21 and there's an implementation in https://github.com/pytest-dev/pytest-asyncio/tree/feature/pluggable-event-loops. I just need to write the docs before merging it in and doing a release.

I'm pretty happy with the results.

@Tinche
Copy link
Member

Tinche commented Jun 2, 2016

I believe this is fixed in 0.4, open another issue for further discussion if you feel like it.

@Tinche Tinche closed this as completed Jun 2, 2016
smagafurov pushed a commit to smagafurov/pytest-asyncio that referenced this issue Apr 4, 2018
Merge transactions branch, still untested in depth, basic tests are passing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants