问题描述
对于我正在测试的应用程序,我想创建一个autouse=True
固定装置,如果smtplib.SMTP.connect
尝试意外发送电子邮件,它会修补smtplib.SMTP.connect
失败的测试.
For an application I'm testing I'd like to create an autouse=True
fixture which monkeypatches smtplib.SMTP.connect
to fail tests if they try to send an email unexpectedly.
但是,在我确实希望测试能够发送电子邮件的情况下,我想改用其他夹具记录这些电子邮件(很可能是通过使用pytest-localserver
中的smtpserver
夹具,并猴子修补connect
方法来使用)该装置返回的主机/端口)
However, in cases where I do expect tests to send emails, I want to use a different fixture logging those emails instead (most likely by using the smtpserver
fixture from pytest-localserver
and monkeypatching the connect
method to use the host/port returned by that fixture)
当然,只有在另一个夹具(作为funcarg加载)之前执行autouse夹具时,此方法才起作用.有没有执行固定装置的特定顺序和/或有什么方法可以保证执行顺序?
Of course that can only work if the autouse fixture is executed before the other fixture (loaded as funcarg). Is there any specific order in which fixtures are executed and/or is there a way to guarantee the execution order?
推荐答案
控制灯具执行顺序的最简单方法是仅在后一个灯具中请求前一个灯具.因此,请确保b
在a
之前运行:
The easiest way to control the order in which fixtures are executed, is to just request the previous fixture in the later fixture. So to make sure b
runs before a
:
@pytest.fixture(autouse=True, scope="function")
def b():
pass
@pytest.fixture(scope="function")
def a(b):
pass
这篇关于pytest固定装置按什么顺序执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!