sanic-org / sanic-testing

Test clients for Sanic
https://sanic.dev/en/plugins/sanic-testing/getting-started.html
MIT License
31 stars 19 forks source link

[Bug] async test fails #45

Closed FlyinPancake closed 1 year ago

FlyinPancake commented 2 years ago

I tried to run the async test and it failed.

Error

_________________________________________________________ test_basic_asgi_client __________________________________________________________

app = Sanic(name="test_test")

    @pytest.mark.asyncio
    async def test_basic_asgi_client(app):
>       request, response = await app.asgi_client.get("/")

test_test.py:16: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.venv/lib64/python3.10/site-packages/httpx/_client.py:1751: in get
    return await self.request(
.venv/lib64/python3.10/site-packages/sanic_testing/testing.py:364: in request
    await self.sanic_app._startup()  # type: ignore
.venv/lib64/python3.10/site-packages/sanic/app.py:1513: in _startup
    self.ext._display()
.venv/lib64/python3.10/site-packages/sanic_ext/bootstrap.py:110: in _display
    f"  > {extension.name} {extension.render_label()}"
.venv/lib64/python3.10/site-packages/sanic_ext/extensions/base.py:56: in render_label
    label = self.label()
.venv/lib64/python3.10/site-packages/sanic_ext/extensions/openapi/extension.py:25: in label
    return self._make_url()
.venv/lib64/python3.10/site-packages/sanic_ext/extensions/openapi/extension.py:34: in _make_url
    else self.app.serve_location
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = Sanic(name="test_test")

    @property
    def serve_location(self) -> str:
>       server_settings = self.state.server_info[0].settings
E       IndexError: list index out of range

.venv/lib64/python3.10/site-packages/sanic/mixins/runner.py:574: IndexError

How to reproduce

  1. create new venv and install packages
  2. create test_test.py file with this contents
    
    import pytest
    from sanic import Sanic, response

@pytest.fixture def app(): sanic_app = Sanic(name)

@sanic_app.get("/")
def basic(request):
    return response.text("foo")

return sanic_app

@pytest.mark.asyncio async def test_basic_asgi_client(app): request, response = await app.asgi_client.get("/")

assert request.method.lower() == "get"
assert response.body == b"foo"
assert response.status == 200
3. run `pytest` and watch it fail

## Notes
- Without sanic_ext tesing is successful
- I can't just remove sanic_ext from my project 

---

# Packages

aiofiles==0.8.0 anyio==3.6.1 attrs==21.4.0 certifi==2022.6.15 h11==0.12.0 httpcore==0.15.0 httptools==0.4.0 httpx==0.23.0 idna==3.3 iniconfig==1.1.1 multidict==6.0.2 packaging==21.3 pluggy==1.0.0 py==1.11.0 pyparsing==3.0.9 pytest==7.1.2 pytest-asyncio==0.18.3 PyYAML==6.0 rfc3986==1.5.0 sanic==22.6.0 sanic-ext==22.6.2 sanic-routing==22.3.0 sanic-testing==22.6.0 sniffio==1.2.0 tomli==2.0.1 ujson==5.4.0 uvloop==0.16.0 websockets==10.3

ahopkins commented 2 years ago

Same as this: https://github.com/sanic-org/sanic-testing/pull/43

ahopkins commented 2 years ago

Will try and get that fix out shortly.

FlyinPancake commented 2 years ago

Thanks @ahopkins :heart:

Strangely ReusableClient worked for me although quite it was quite buggy

ahopkins commented 1 year ago

Should be resolved now