nonebot / nonebug

NoneBot2 测试框架 / NoneBot2 test framework
https://nonebot.dev/docs/best-practice/testing/
MIT License
28 stars 6 forks source link

Fix: 修复测试全局 asgi driver lifespan 问题 #30

Closed yanyongyu closed 2 weeks ago

codecov[bot] commented 2 weeks ago

Codecov Report

Attention: Patch coverage is 82.97872% with 8 lines in your changes missing coverage. Please review.

Project coverage is 92.68%. Comparing base (5ab84d7) to head (11642e5). Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
nonebug/fixture.py 81.25% 6 Missing :warning:
nonebug/mixin/driver.py 86.66% 2 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #30 +/- ## ========================================== - Coverage 92.88% 92.68% -0.21% ========================================== Files 14 14 Lines 464 492 +28 ========================================== + Hits 431 456 +25 - Misses 33 36 +3 ``` | [Flag](https://app.codecov.io/gh/nonebot/nonebug/pull/30/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nonebot) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/nonebot/nonebug/pull/30/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nonebot) | `92.68% <82.97%> (-0.21%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=nonebot#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

StarHeartHunt commented 2 weeks ago
d:\DevEnv\miniconda3\envs\adapter-feishu\Lib\site-packages\pytest_asyncio\plugin.py:208: PytestDeprecationWarning: The configuration option "asyncio_default_fixture_loop_scope" is unset.
The event loop scope for asynchronous fixtures will default to the fixture caching scope. Future versions of pytest-asyncio will default the loop scope for asynchronous fixtures to function scope. Set the default fixture loop scope explicitly in order to avoid unexpected behavior in the future. Valid fixture loop scopes are: "function", "class", "module", "package", "session"

  warnings.warn(PytestDeprecationWarning(_DEFAULT_FIXTURE_LOOP_SCOPE_UNSET))
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.11.7, pytest-8.3.3, pluggy-1.5.0
rootdir: D:\PythonWorkspace\adapter-feishu
configfile: pyproject.toml
plugins: anyio-4.6.2.post1, nonebug-0.4.1, asyncio-0.24.0, cov-5.0.0
asyncio: mode=Mode.AUTO, default_loop_scope=None
collected 7 items

tests\test_feishu_bot.py ...                                                                                                                                       [ 42%]
tests\test_feishu_connection.py F                                                                                                                                  [ 57%]
tests\test_feishu_event.py .                                                                                                                                       [ 71%] 
tests\test_feishu_utils.py ..                                                                                                                                      [100%]

=============================================================================== FAILURES ================================================================================ 
_________________________________________________________________ test_http[/feishu/app_id_connection] __________________________________________________________________ 

app = <nonebug.app.App object at 0x0000019F3F4D3090>, endpoints = '/feishu/app_id_connection', server_url = URL('http://127.0.0.1:6541')

    @pytest.mark.asyncio
    @pytest.mark.parametrize("endpoints", [f"/feishu/{bot_id}"])
    async def test_http(app: App, endpoints: str, server_url: URL):
        with (Path(__file__).parent.joinpath("data", "payloads.json")).open("r") as f:
            test_payloads = json.load(f)

        adapter = nonebot.get_adapter(Adapter)
        adapter.feishu_config.feishu_api_base = str(server_url)  # type: ignore

>       async with app.test_server() as ctx:

tests\test_feishu_connection.py:26:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
D:\DevEnv\miniconda3\envs\adapter-feishu\Lib\site-packages\nonebug\base.py:17: in __aenter__
    await self.setup()
D:\DevEnv\miniconda3\envs\adapter-feishu\Lib\site-packages\nonebug\mixin\driver.py:44: in setup
    await self.stack.enter_async_context(self.client)
D:\DevEnv\miniconda3\envs\adapter-feishu\Lib\contextlib.py:650: in enter_async_context
    result = await _enter(cm)
D:\DevEnv\miniconda3\envs\adapter-feishu\Lib\site-packages\async_asgi_testclient\testing.py:91: in __aenter__
    await self.send_lifespan("startup")
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <async_asgi_testclient.testing.TestClient object at 0x0000019F3F4D3A90>, action = 'startup'

    async def send_lifespan(self, action):
        await self._lifespan_input_queue.put({"type": f"lifespan.{action}"})
        message = await receive(self._lifespan_output_queue, timeout=self.timeout)

        if isinstance(message, Message):
            raise Exception(f"{message.event} - {message.reason} - {message.task}")

        if message["type"] == f"lifespan.{action}.complete":
            pass
        elif message["type"] == f"lifespan.{action}.failed":
>           raise Exception(message)
E           Exception: {'type': 'lifespan.startup.failed', 'message': 'Traceback (most recent call last):\n  File "D:\\DevEnv\\miniconda3\\envs\\adapter-feishu\\Lib\\site-packages\\starlette\\routing.py", line 693, in lifespan\n    async with self.lifespan_context(app) as maybe_state:\n  File "D:\\DevEnv\\miniconda3\\envs\\adapter-feishu\\Lib\\contextlib.py", line 210, in __aenter__\n    return await anext(self.gen)\n           ^^^^^^^^^^^^^^^^^^^^^\n  File "D:\\DevEnv\\miniconda3\\envs\\adapter-feishu\\Lib\\site-packages\\nonebot\\drivers\\fastapi.py", line 155, in _lifespan_manager\n    await self._lifespan.startup()\n  File "D:\\DevEnv\\miniconda3\\envs\\adapter-feishu\\Lib\\site-packages\\nonebot\\internal\\driver\\_lifespan.py", line 61, in startup\n    self.task_group = anyio.create_task_group()\n    ^^^^^^^^^^^^^^^\n  File "D:\\DevEnv\\miniconda3\\envs\\adapter-feishu\\Lib\\site-packages\\nonebot\\internal\\driver\\_lifespan.py", line 34, in task_group\n    raise RuntimeError("Lifespan already started")\nRuntimeError: Lifespan already started\n'}

D:\DevEnv\miniconda3\envs\adapter-feishu\Lib\site-packages\async_asgi_testclient\testing.py:108: Exception

---------- coverage: platform win32, python 3.11.7-final-0 -----------
Name                                         Stmts   Miss  Cover   Missing
--------------------------------------------------------------------------
nonebot\adapters\feishu\__init__.py             18      0   100%
nonebot\adapters\feishu\adapter.py             175    102    42%   59-91, 95, 102, 111-117, 120, 123-136, 139-162, 165-198, 202-217, 220-276, 288, 291, 304-307, 311-312, 327-329, 346
nonebot\adapters\feishu\bot.py                 157     78    50%   24, 29-52, 67, 71, 74-75, 98, 109-110, 113-115, 118, 133, 162-179, 203, 216, 223, 235-242, 253-257, 271-275, 283, 286, 295-303, 316, 328-332, 341-355, 377, 393, 396-401
nonebot\adapters\feishu\config.py               13      0   100%
nonebot\adapters\feishu\event.py               236     25    89%   79, 83, 87, 91, 95, 99, 103, 107, 111, 132, 136, 140, 169, 173, 177, 180, 184, 192, 210, 214, 218, 222, 226, 230, 234
nonebot\adapters\feishu\exception.py            22      7    68%   11, 26-27, 35, 50-51, 57
nonebot\adapters\feishu\message.py             370    101    73%   22, 26, 30-33, 39, 47, 53, 57, 61, 65, 74-77, 90, 103, 109, 113, 120, 127, 134, 138, 142, 151, 163, 167, 171, 174-175, 189, 203, 217, 249, 269, 283, 297, 312, 329, 344, 359, 373, 424-453, 469, 485, 500, 516, 531, 545, 561, 570, 579, 590, 596, 604, 611, 614-639, 663, 680-688, 694-699
nonebot\adapters\feishu\models\__init__.py       2      0   100%
nonebot\adapters\feishu\models\api.py           21      0   100%
nonebot\adapters\feishu\models\common.py       381      6    98%   21-22, 131-132, 150-151
nonebot\adapters\feishu\utils.py                39      8    79%   17-27
--------------------------------------------------------------------------
TOTAL                                         1434    327    77%

======================================================================== short test summary info ======================================================================== 
FAILED tests/test_feishu_connection.py::test_http[/feishu/app_id_connection] - Exception: {'type': 'lifespan.startup.failed', 'message': 'Traceback (most recent call last):\n  File "D:\\DevEnv\\miniconda3\\envs\\adapter-feishu\\Lib\\site-packag...
====================================================================== 1 failed, 6 passed in 1.55s ======================================================================