【发布时间】:2021-06-02 04:48:26
【问题描述】:
我想使用 pytest 为我的应用程序创建一个简单的冒烟测试。这对我来说真的很简单,这就是我尝试的原因。问题是我注意到机器人不会对其他机器人的消息做出反应。我查看了代码(bot.py)并修改了 process_commands 只是为了进行冒烟测试。不幸的是,它仍然只适用于测试期间的人工消息。
编辑代码,完整测试(仅导入HASH和频道id)
from discord.ext import commands
import pytest
import threading
import time
from build_config import HASH, channel_id
bot = commands.Bot(command_prefix = '.', description="This is a test bot for stackoverflow!")
class Test_Message(commands.Cog):
@commands.command(pass_context = True)
async def message(self, ctx):
await ctx.send("text_message")
def run_bot():
bot.add_cog(Test_Message())
bot.run(HASH.DEBUG)
@pytest.fixture
def create_bot():
t1 = threading.Thread(target=run_bot, args=[])
t1.start()
class Test_Events:
successful = False
@staticmethod
@bot.event
async def on_ready():
channel = bot.get_channel(channel_id)
await channel.send('.message')
@staticmethod
@bot.event
async def on_message(message):
await bot.process_commands(message)
if (message.channel.name == "general" and message.clean_content == "text_message"):
Test_Events.successful = True
@staticmethod
@bot.event
async def process_commands(message):
ctx = await bot.get_context(message)
await bot.invoke(ctx)
@pytest.mark.asyncio
async def test_Smoke(create_bot):
must_end = time.time() + 60
while time.time() < must_end:
if Test_Events.successful:
break
time.sleep(1)
assert Test_Events.successful
基本上有人在Allow Discord Rewrite bot to respond to other bots 中将此标记为解决方案,但它对我不起作用。
编辑:所以我调试了 discord.py,不幸的是,get_context self._skip_check(message.author.id, self.user.id) 中至少还有另一个检查。
【问题讨论】:
-
你注意正确的缩进了吗?
-
我不确定你的意思。正确缩进的代码,它适用于用户命令。
-
您的事件在我看来不像是适当的缩进。据我了解,这些不起作用?
-
不。你不明白,这就是你缩进 .bot.event 的方式。如果人类不和谐用户在测试期间写入
.send_expected(这是 Event 中的命令),则上面的代码有效,但如果该命令仅由机器人在 on_ready (await Print.message_to_channel("bot", ".send_expected")) 处写入,则上述代码无效 -
你是如何传递这些函数的?如果这些真的是对象方法,那么这三个方法都需要一个“self”参数。如果这些被称为类静态方法 (Test_Events.on_ready),那么它们不会。
标签: python discord discord.py pytest