-
Notifications
You must be signed in to change notification settings - Fork 19
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
Can't run multiple tests with same instance of ReusableClient #63
Comments
Maybe that I did't understand how to use 'ReusableClient' class in right way, If that , I will be very grateful to see more complex example in documentation or at least in tests. |
I'll post an example here and then to the docs later today. |
Any update on this? I am writing tests for setting cookies, the default I try to monkey-patch the application, replacing It worked before (sanic<=20.x.x, IIRC), basically it allows me to pass in |
I was able to create a Here is how I I did this # conftest.py
from my_app import create_app
@pytest.fixture
def config():
return {'DEBUG': True}
@pytest.fixture
def app(config):
Sanic.test_mode = True
return create_app(config)
@pytest.fixture
def test_cli(app, event_loop):
with ReusableClient(app, loop=event_loop) as cli:
try:
yield cli
finally:
event_loop.run_until_complete(cli._session.aclose()) # close request where There is one issue with the Here is a sample how this fixture can be used # test_auth.py
def test_auth_token_missing(test_cli):
req, resp = test_cli.get('/api/v1/sample.png')
assert resp.status == 401
assert resp.json == {
'details': 'Authentication credentials were not provided'
} You can also create test client with predefined headers @pytest.fixture
def auth_cli(app, token, event_loop):
with ReusableClient(
app,
loop=event_loop,
client_kwargs={'headers': {'Authorization': f'Bearer {token}'}}
) as cli:
try:
yield cli
finally:
event_loop.run_until_complete(cli._session.aclose()) # close request If you need to run something async you can use def test_reader_cache(app, event_loop, auth_cli):
req, resp = auth_cli.get('/api/v1/sample.png')
assert resp.status == 200
assert len(app.ctx.reader.cache) == 1
event_loop.run_until_complete(asyncio.sleep(0.4)) # make sure cleanup task has been called
assert len(app.ctx.reader.cache) == 0 |
How to reproduce
Error that I got
The text was updated successfully, but these errors were encountered: