1+ from contextlib import nullcontext , asynccontextmanager
2+
13import pytest
4+ from async_asgi_testclient import TestClient
25
36from nonebug .app import App
7+ from nonebug .mixin .driver import set_global_client
48
59from . import NONEBOT_INIT_KWARGS , NONEBOT_START_LIFESPAN
610
711
12+ @asynccontextmanager
13+ async def lifespan_ctx ():
14+ import nonebot
15+ from nonebot import logger
16+ from nonebot .drivers import ASGIMixin
17+
18+ driver = nonebot .get_driver ()
19+
20+ if isinstance (driver , ASGIMixin ):
21+ # if the driver has an asgi application
22+ # use asgi lifespan to startup/shutdown
23+ ctx = TestClient (driver .asgi )
24+ set_global_client (ctx )
25+ else :
26+ ctx = driver ._lifespan
27+
28+ try :
29+ await ctx .__aenter__ ()
30+ except Exception as e :
31+ logger .opt (colors = True , exception = e ).error (
32+ "<r><bg #f8bbd0>Error occurred while running startup hook."
33+ "</bg #f8bbd0></r>"
34+ )
35+ raise
36+
37+ try :
38+ yield
39+ finally :
40+ try :
41+ await ctx .__aexit__ (None , None , None )
42+ except Exception as e :
43+ logger .opt (colors = True , exception = e ).error (
44+ "<r><bg #f8bbd0>Error occurred while running shutdown hook."
45+ "</bg #f8bbd0></r>"
46+ )
47+
48+
849@pytest .fixture (scope = "session" , autouse = True )
9- async def nonebug_init (request : pytest .FixtureRequest ): # noqa: PT004
50+ def _nonebot_init (request : pytest .FixtureRequest ):
1051 """
1152 Initialize nonebot before test case running.
1253 """
1354 import nonebot
14- from nonebot import logger
1555 from nonebot .matcher import matchers
1656
1757 from nonebug .provider import NoneBugProvider
1858
1959 nonebot .init (** request .config .stash .get (NONEBOT_INIT_KWARGS , {}))
2060 matchers .set_provider (NoneBugProvider )
2161
62+
63+ @pytest .fixture (scope = "session" , autouse = True )
64+ async def after_nonebot_init (_nonebot_init : None ): # noqa: PT004
65+ pass
66+
67+
68+ @pytest .fixture (scope = "session" , autouse = True )
69+ async def nonebug_init ( # noqa: PT004
70+ _nonebot_init : None , after_nonebot_init : None , request : pytest .FixtureRequest
71+ ):
2272 run_lifespan = request .config .stash .get (NONEBOT_START_LIFESPAN , True )
23- driver = nonebot .get_driver ()
24- if run_lifespan :
25- try :
26- await driver ._lifespan .startup ()
27- except Exception as e :
28- logger .opt (colors = True , exception = e ).error (
29- "<r><bg #f8bbd0>Error occurred while running startup hook."
30- "</bg #f8bbd0></r>"
31- )
32- raise
3373
34- try :
74+ ctx = lifespan_ctx () if run_lifespan else nullcontext ()
75+
76+ async with ctx :
3577 yield
36- finally :
37- if run_lifespan :
38- try :
39- await driver ._lifespan .shutdown ()
40- except Exception as e :
41- logger .opt (colors = True , exception = e ).error (
42- "<r><bg #f8bbd0>Error occurred while running shutdown hook."
43- "</bg #f8bbd0></r>"
44- )
4578
4679
4780@pytest .fixture (name = "app" )
@@ -53,4 +86,4 @@ def nonebug_app(nonebug_init) -> App:
5386 return App ()
5487
5588
56- __all__ = ["nonebug_init" , "nonebug_app" ]
89+ __all__ = ["after_nonebot_init" , " nonebug_init" , "nonebug_app" ]
0 commit comments