如何将夹具作为参数传递给另一个夹具

时间:2021-05-03 21:11:12

标签: python python-3.x unit-testing pytest fixtures

我想将一个 pytest.fixture 函数传递给另一个夹具函数的 param 参数。像这样:

@pytest.fixture()
def foo():
    return "foo"

@pytest.fixture()
def boo(foo):
    return foo + "boo"

@pytest.fixture()
def bar(foo):
    return foo + "bar"

@pytest.fixture(params=[boo, bar], ids=["boo_fixture", "bar_fixture"])
def final_fixture(request):
    return request.param

def _test(final_fixture):
    assert final_fixture.startswith('foo')

request.param 中的 final_fixture 返回该参数的函数对象,而不是设备(boobar)的返回值

<function boo at 0x7f2bfceg41f0>
<function bar at 0x7f2bfceg41f1>

那么,如何让 final_fixture 函数返回每个装置 param 的实际返回值?

1 个答案:

答案 0 :(得分:0)

感谢 Will 的 this 回答,我找到了一个对我有用的解决方案。

似乎您可以使用 from discord import utils import discord import asyncio class onMessage(commands.Cog): def __init__(self, bot): self.bot = bot @commands.Cog.listener() async def on_message(self, message): if message.author.bot: return if isinstance(message.channel, discord.DMChannel): guild = self.bot.get_guild(787409126908755978) categ = utils.get(guild.categories, name = "Modmail tickets") if not categ: overwrites = { guild.default_role : discord.PermissionOverwrite(read_messages = False), guild.me : discord.PermissionOverwrite(read_messages = True), guild.staff : discord.PermissionOverwrite(read_messages = True) } categ = await guild.create_category(name = "Modmail tickets", overwrites = overwrites) channel = utils.get(categ.channels, topic = str(message.author.id)) if not channel: channel = await categ.create_text_channel(name = f"{message.author.name}#{message.author.discriminator}", topic = str(message.author.id)) await channel.send(f"New modmail created by {message.author.mention}") await message.author.send('Hello, Thanks for Messaging VOID Modmail, Please State your Issue Or Question And Our Staff Will Be With You As Soon As Possible.') message.author.mention = self.member embed = discord.Embed(description = message.content, colour = 0x696969) embed.set_author(name = message.author, icon_url = message.author.avatar_url) await channel.send(embed = embed) elif isinstance(message.channel, discord.TextChannel): if message.content.startswith(self.bot.command_prefix): pass else: topic = message.channel.topic if topic: member = message.guild.get_member(int(topic)) if member: embed = discord.Embed(description = message.content, colour = 0x696969) embed.set_author(name = ('VOID Staff'), icon_url = message.author.avatar_url) await member.send(embed = embed) self.member = message.author.id @commands.command("close") async def close(self, ctx): if ctx.channel.category.name == "Modmail tickets": await ctx.send("Deleting the channel in 5 seconds!") await asyncio.sleep(5) await ctx.channel.delete() await self.member.send('This Ticket Has been closed, Feel Free To DM If More Help Is Needed') def setup(bot): bot.add_cog(onMessage(bot))``` 来获取夹具的返回值而不是函数对象。这是此工作的代码:

request.getfixturevalue

这是相关文档:http://doc.pytest.org/en/latest/builtin.html#_pytest.fixtures.FixtureRequest.getfixturevalue