yunstanford / pytest-sanic

a Pytest Plugin for Sanic.
http://pytest-sanic.readthedocs.io/en/latest
Apache License 2.0
135 stars 21 forks source link

fixture scope #15

Closed Pac0o closed 6 years ago

Pac0o commented 6 years ago

Hi, I was using pytest-sanic to test my app and was trying to use the same loop on all my tests but was unable to do it because when I tried to change the scope of the test_cli function this error appeared.

ScopeMismatch: You tried to access the 'function' scoped fixture 'app' with a 'module' scoped request object, involved factories tests/test_polygon.py:12: def test_cli(loop, app, test_client) .env/lib/python3.5/site-packages/pytest_sanic/plugin.py:39: def loop()

@pytest.yield_fixture(scope="module")
def app():
    yield sanicapp

@pytest.fixture(scope="module")
def test_cli(loop, app, test_client):
    return loop.run_until_complete(test_client(app, protocol=WebSocketProtocol))

What I ended up doing was modifying the code from plugin.py image

image

Is there any other way of doing this? I did this to improve the speed of the tests.

Thanks

yunstanford commented 6 years ago

@Pac0o yeah, it's a little tricky, but you can achieve that by wrapping it up. like

from py_benchmarks.server import create_app
from pytest_sanic.plugin import test_client as tc
from pytest_sanic.plugin import loop as l

@pytest.fixture(scope="module")
def app():
    # yield my_app
    return create_app()

@pytest.fixture(scope="module")
def test_client(loop):
    return next(tc(loop))

@pytest.fixture(scope="module")
def loop():
    return next(l())

@pytest.fixture(scope="module")
def test_cli(loop, app, test_client):
    return loop.run_until_complete(test_client(app))
Pac0o commented 6 years ago

Thank you!