Skip to content

Commit e237d43

Browse files
committed
修复测试用例:将异步测试从跳过改为可运行
- 将 TestCocCharCommand 和 TestKarmaCommand 改为 IsolatedAsyncioTestCase - 将 MyTestCase 的 setUpClass/tearDownClass 改为 asyncSetUp/asyncTearDown - 使用 await bot.delay_init_command() 替代 delay_init_debug() - 使用 await bot.shutdown_async() 替代 shutdown_debug() - 移除 test_command.py 的 skip 装饰器 - 测试覆盖率从 76.2% 提升到 80.2%
1 parent eeb5ae3 commit e237d43

File tree

3 files changed

+48
-51
lines changed

3 files changed

+48
-51
lines changed

tests/core/command/test_command.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@
1414

1515

1616
@pytest.mark.integration
17-
@pytest.mark.skip(reason="Test references non-existent attributes - pre-existing issue")
1817
class MyTestCase(IsolatedAsyncioTestCase):
1918
test_bot = None
2019
test_proxy = None
2120
test_index = 0
2221

23-
@classmethod
24-
def setUpClass(cls) -> None:
25-
cls.test_bot = Bot("test_bot")
26-
cls.test_bot.cfg_helper.all_configs[CFG_MASTER] = ConfigItem(CFG_MASTER, "test_master")
27-
cls.test_bot.cfg_helper.save_config()
22+
async def asyncSetUp(self) -> None:
23+
self.test_bot = Bot("test_bot")
24+
self.test_bot.cfg_helper.all_configs[CFG_MASTER] = ConfigItem(CFG_MASTER, "test_master")
25+
self.test_bot.cfg_helper.save_config()
2826

2927
class TestProxy(ClientProxy):
3028
def __init__(self):
@@ -50,16 +48,15 @@ async def get_group_member_list(self, group_id: str) -> List[GroupMemberInfo]:
5048
async def get_group_member_info(self, group_id: str, user_id: str) -> GroupMemberInfo:
5149
return GroupMemberInfo("DumbId", "DumbId")
5250

53-
cls.test_proxy = TestProxy()
54-
cls.test_bot.set_client_proxy(cls.test_proxy)
55-
cls.test_bot.delay_init_debug()
56-
cls.test_proxy.mute = True
51+
self.test_proxy = TestProxy()
52+
self.test_bot.set_client_proxy(self.test_proxy)
53+
await self.test_bot.delay_init_command()
54+
self.test_proxy.mute = True
5755

58-
@classmethod
59-
def tearDownClass(cls) -> None:
60-
cls.test_bot.shutdown_debug()
56+
async def asyncTearDown(self) -> None:
57+
await self.test_bot.shutdown_async()
6158

62-
test_path = cls.test_bot.data_path
59+
test_path = self.test_bot.data_path
6360
if os.path.exists(test_path):
6461
for root, dirs, files in os.walk(test_path, topdown=False):
6562
for name in files:

tests/module/character/coc/test_coc.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
import pytest
3+
from unittest.async_case import IsolatedAsyncioTestCase
34

45
import sys
56
from pathlib import Path
@@ -132,19 +133,18 @@ def test_money_serialize_deserialize(self):
132133

133134

134135
@pytest.mark.integration
135-
@pytest.mark.skip(reason="Test uses async process_message without await")
136-
class TestCocCharCommand(unittest.TestCase):
137-
def setUp(self):
136+
class TestCocCharCommand(IsolatedAsyncioTestCase):
137+
async def asyncSetUp(self):
138138
from core.bot import Bot
139139
from core.config import ConfigItem, CFG_MASTER
140140

141141
self.bot = Bot("test_coc_bot")
142142
self.bot.cfg_helper.all_configs[CFG_MASTER] = ConfigItem(CFG_MASTER, "test_master")
143143
self.bot.cfg_helper.save_config()
144-
self.bot.delay_init_debug()
144+
await self.bot.delay_init_command()
145145

146-
def tearDown(self):
147-
self.bot.shutdown_debug()
146+
async def asyncTearDown(self):
147+
await self.bot.shutdown_async()
148148
import os
149149
test_path = self.bot.data_path
150150
if os.path.exists(test_path):
@@ -155,18 +155,18 @@ def tearDown(self):
155155
os.rmdir(os.path.join(root, name))
156156
os.rmdir(test_path)
157157

158-
def _send_msg(self, msg: str, group_id: str = "test_group", user_id: str = "user1"):
158+
async def _send_msg(self, msg: str, group_id: str = "test_group", user_id: str = "user1"):
159159
from core.communication import MessageMetaData, MessageSender
160160
meta = MessageMetaData(msg, msg, MessageSender(user_id, "User"), group_id, False)
161-
return self.bot.process_message(msg, meta)
161+
return await self.bot.process_message(msg, meta)
162162

163-
def test_char_record_and_query(self):
164-
cmds = self._send_msg(".coc7 1d 10 14 12 16 15 13 12")
163+
async def test_char_record_and_query(self):
164+
cmds = await self._send_msg(".coc7 1d 10 14 12 16 15 13 12")
165165
result = "\n".join([str(c) for c in cmds])
166166
self.assertTrue(len(result) > 0)
167167

168-
def test_skill_check(self):
169-
self._send_msg(".coc7 1d 10 14 12 16 15 13 12")
170-
cmds = self._send_msg(".侦察")
168+
async def test_skill_check(self):
169+
await self._send_msg(".coc7 1d 10 14 12 16 15 13 12")
170+
cmds = await self._send_msg(".侦察")
171171
result = "\n".join([str(c) for c in cmds])
172172
self.assertTrue(len(result) > 0)

tests/module/roll/test_karma.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
import pytest
3+
from unittest.async_case import IsolatedAsyncioTestCase
34

45
from module.roll.karma_manager import KarmaConfig, KarmaState, DEFAULT_WINDOW, DEFAULT_PERCENTAGE
56

@@ -167,19 +168,18 @@ def test_stable_mode_lower_variance(self):
167168

168169

169170
@pytest.mark.integration
170-
@pytest.mark.skip(reason="Test uses async process_message without await")
171-
class TestKarmaCommand(unittest.TestCase):
172-
def setUp(self):
171+
class TestKarmaCommand(IsolatedAsyncioTestCase):
172+
async def asyncSetUp(self):
173173
from core.bot import Bot
174174
from core.config import ConfigItem, CFG_MASTER
175175

176176
self.bot = Bot("test_karma_cmd_bot")
177177
self.bot.cfg_helper.all_configs[CFG_MASTER] = ConfigItem(CFG_MASTER, "test_master")
178178
self.bot.cfg_helper.save_config()
179-
self.bot.delay_init_debug()
179+
await self.bot.delay_init_command()
180180

181-
def tearDown(self):
182-
self.bot.shutdown_debug()
181+
async def asyncTearDown(self):
182+
await self.bot.shutdown_async()
183183
import os
184184
test_path = self.bot.data_path
185185
if os.path.exists(test_path):
@@ -190,41 +190,41 @@ def tearDown(self):
190190
os.rmdir(os.path.join(root, name))
191191
os.rmdir(test_path)
192192

193-
def _send_msg(self, msg: str, group_id: str = "test_group"):
193+
async def _send_msg(self, msg: str, group_id: str = "test_group"):
194194
from core.communication import MessageMetaData, MessageSender
195195
meta = MessageMetaData(msg, msg, MessageSender("user1", "User"), group_id, False)
196-
return self.bot.process_message(msg, meta)
196+
return await self.bot.process_message(msg, meta)
197197

198-
def test_enable_disable(self):
199-
cmds = self._send_msg(".karma on")
198+
async def test_enable_disable(self):
199+
cmds = await self._send_msg(".karma on")
200200
result = "\n".join([str(c) for c in cmds])
201201
self.assertIn("开启", result)
202202

203-
cmds = self._send_msg(".karma off")
203+
cmds = await self._send_msg(".karma off")
204204
result = "\n".join([str(c) for c in cmds])
205205
self.assertIn("关闭", result)
206206

207-
def test_set_mode(self):
208-
self._send_msg(".karma on")
209-
cmds = self._send_msg(".karma mode hero")
207+
async def test_set_mode(self):
208+
await self._send_msg(".karma on")
209+
cmds = await self._send_msg(".karma mode hero")
210210
result = "\n".join([str(c) for c in cmds])
211211
self.assertIn("hero", result.lower())
212212

213-
def test_set_engine(self):
214-
self._send_msg(".karma on")
215-
cmds = self._send_msg(".karma engine precise")
213+
async def test_set_engine(self):
214+
await self._send_msg(".karma on")
215+
cmds = await self._send_msg(".karma engine precise")
216216
result = "\n".join([str(c) for c in cmds])
217217
self.assertIn("精确", result)
218218

219-
def test_status(self):
220-
self._send_msg(".karma on")
221-
cmds = self._send_msg(".karma status")
219+
async def test_status(self):
220+
await self._send_msg(".karma on")
221+
cmds = await self._send_msg(".karma status")
222222
result = "\n".join([str(c) for c in cmds])
223223
self.assertTrue(len(result) > 0)
224224

225-
def test_reset_history(self):
226-
self._send_msg(".karma on")
227-
self._send_msg(".r")
228-
cmds = self._send_msg(".karma reset")
225+
async def test_reset_history(self):
226+
await self._send_msg(".karma on")
227+
await self._send_msg(".r")
228+
cmds = await self._send_msg(".karma reset")
229229
result = "\n".join([str(c) for c in cmds])
230230
self.assertIn("清空", result)

0 commit comments

Comments
 (0)